mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 How To Quote Like A Pro

Author Message
ebgreen

  • Total Posts : 8088
  • Scores: 95
  • Reward points : 0
  • Joined: 7/12/2005
  • Status: offline
How To Quote Like A Pro Wednesday, March 03, 2010 4:46 AM (permalink)
3
The Rules
In VBScript, there are several activities that can lead to the need to produce strings that have double quotes (") embedded in them. Most often this comes about due to the need to run an external command where spaces are present in a path.

First let's talk about the quoting rules in VBScript. A single double quote starts a string literal and another single double quote ends a string literal. So this code:

WScript.Echo "This is a string literal"

Would output this:

This is a string literal

To have a single double quote appear in a string you uses two double quotes (I know this sounds confusing). So this code:

WScript.Echo "This has a "" double quote in the middle"

Would output:

This has a " double quote in the middle

This means that if you want a string to have double quotes at the beginning and end, you need to start and end it with triple double quotes:

WScript.Echo """This has double quotes at the beginning and end"""

Output:

"This has double quotes at the beginning and end"

In that last example, the first and last double quotes (the ones in red) start and end the string. The others (the ones in black) make the double quotes appear in the string itself.

Making it easy
It is entirely possible to do the quoting using multiple quotes all stacked up to get whatever quoting you need. I'm lazy though and I hate having to think about how many quotes I need to get the result I want. Instead, I use a function to do the quoting for me:

Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function

All this function does is take a string in and return that string with double quotes around it. Chr(34) is simply using the ASCII value of the double quote character instead of using "". I just find it less confusing to read. So now if I want to put double quotes around something I just include the function in my script then:

WScript.Echo qq("This has double quotes at the beginning and end")

Output:

"This has double quotes at the beginning and end"

Troubleshooting Quoting Problems
As I said earlier, the most common reason to need oddball quoting is paths with spaces. When you are trying to use .Run or .Exec and you are getting file not found errors, nine times out of ten it is a problem with quoting and spaces in paths. Let's work with an example:

Scenario:
    You want the users to be able to enter the path and name of a zip file and you will output a list of the files in the archive. For this I wouls use 7-zip which is my favorite compression utility and has a good command line interface.

Process:
    First I would work out the code to get the input from the user:

Option Explicit
Dim strArchivePath

strArchivePath = InputBox("Enter the path to the archive")

Note that for brevity's sake I'm going to leave out all the checking that you should be doing here to make sure that what the user enters is really a valid path to a valid file.

Next, I would try to build the command:

Option Explicit
Dim strArchivePath
Dim strCMD

strArchivePath = InputBox("Enter the path to the archive")
strCMD = "C:\Program Files\7-zip\7z.exe l " & strArchivePath
WScript.Echo strCMD

Notice that I stop developing my script and run it at this point. The key here is the echo. That echo shows you the command that you will be trying to run. This is critical. At this point you want to start playing with the quoting to get it to look the way that you want. I know that the path to the 7-zip exe has a space in it (thanks microsoft). So I know that needs to be quoted. I am going to assume that the user is evil and will at some point enter a path with a space in it, so I want to quote that too. So the next thing I do is to add my quoting function and quote those parts:

Option Explicit
Dim strArchivePath
Dim strCMD

strArchivePath = InputBox("Enter the path to the archive")
strCMD = qq("C:\Program Files\7-zip\7z.exe") & " l " & qq(strArchivePath)
WScript.Echo strCMD



Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function

So now it looks like my command should be right. At this point I would verify that it actually is right by copying the output from the echo statement and pasting it into a command prompt to see if it really does run. In this case it did work for me so I would comment out the echo and go on with developing the rest of the script to actually run the command.

The key concept to take away form all this is to work out your command well before you ever try to run it in the script.

As always post comments or questions here if I missed something or anything was unclear.

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm
#1
    Fredledingue

    • Total Posts : 572
    • Scores: 0
    • Reward points : 0
    • Joined: 5/9/2005
    • Location: Europe
    • Status: offline
    Re:How To Quote Like A Pro Sunday, May 09, 2010 3:00 AM (permalink)
    0
    Everyone it's good habits. IMO I find a little bit overkill to create a function for doublequotes and I don't see how it make the code clearer.
    I also don't how & Chr(34) & is less confusing than & """" &.
    Question of habit of course, but 34 doesn't means anything to me, while visualy """" strikes me more.
    Is it realy difficult to remeber that & """" & means ", that """ measn " at the beginning or the end of a string and that "" within a string means " within a string?
    What's easier? This
    strCMD = qq("C:\Program Files\7-zip\7z.exe") & " | " & qq(strArchivePath) 

    or this
    strCMD = """C:\Program Files\7-zip\7z.exe"" |""" & strArchivePath & """" 

    It's realy a question of starting to use and recognize visualy multiple quotes.
    Fred
    #2
      ebgreen

      • Total Posts : 8088
      • Scores: 95
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      Re:How To Quote Like A Pro Monday, May 10, 2010 2:08 AM (permalink)
      0
      You're right it is just habits.
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
      #3
        EncinoMan

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 5/15/2011
        • Status: offline
        Re:How To Quote Like A Pro Tuesday, June 14, 2011 10:15 PM (permalink)
        0
        I had starting using vbs years ago to do some automation tasks at work. As the need was gone so were my days of learning vbs. Recently I have a need again. So I started diving back in. I have been trying to run a private program using WshShell.run and have had no luck until now. I knew about Quotes and all the """"xxxx """ xxx"""" BS""" but could still not get this working. ebgreen reminded me of some simple basics. Echo what you think you know. As it turns out, the command I was so sure I knew how it would run, was not as I thought. I miss placed a " or two. This is a very good article to remind or show somebody new to vbs how to troubleshoot it. It's very simple and that's why I think many of us overlook or forget about it. Thank you for posting this. It has proven very useful for me and is appreciated!
         
        Carlos
        #4
          cry1978

          • Total Posts : 2
          • Scores: 0
          • Reward points : 0
          • Joined: 6/15/2011
          • Location: redding ca
          • Status: offline
          Re:How To Quote Like A Pro Wednesday, June 15, 2011 1:58 PM (permalink)
          0
          WScript is not declared, why didnt you declare it, and I only get a blank form, nothing written in it?
           
          are we supposed to make an input and output box, and then put this code in it?
           
           
          #5
            Wakawaka

            • Total Posts : 451
            • Scores: 23
            • Reward points : 0
            • Joined: 8/27/2009
            • Status: offline
            Re:How To Quote Like A Pro Thursday, June 30, 2011 7:18 AM (permalink)
            0
            cry1978


            WScript is not declared, why didnt you declare it, and I only get a blank form, nothing written in it? 


            WScript is globally accessible so the methods are already exposed to every script.  It is pretty much the root namespace for the whole VBS language.
             
            What code are you using?  If you just use his "qq" function without any other code, nothing would happen.  Functions and subs need to be called in order to execute the code inside of them. 
            This is out of scope of this topic, so if you want more help, please post it on the WSH & Client Side VBScript board.
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 1 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.8
              mbt shoes www.wileywilson.com