I am trying to run the following code in vbscript:
ReturnCode = WshShell.Run("C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe", 0, True)
I get an error when I run this script saying it cannot find the file. I think the problem is spaces in the path, but I don't want to reinstall this application to a different path. How do I get around this?
EDIT: Also, I need to be able to put arguments after the executable. Do the arguments go inside the quotes or outside?
You can get around this by surrounding the path in quotes. But to do so, you need to escape them correctly(with "), so:
ReturnCode = WshShell.Run("""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", 0, True)
EDIT: Keep the path in double quotes and add around them as necessary:
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe"" argumentGoesHere"
Put the executable inside of double-quotes:
ReturnCode = WshShell.Run( _
"""C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe""", _
0, True)
I've never been a big fan of multiple quotes appearing in my code, though it is definitely a solution that works.
What I prefer to do to make my code (to me) more readable is to use chr(34) (the ASCII version of the quotation mark) when I'm adding quotes designed to surround a file name or other string that must be encased in quotes. It's more typing, but to me it avoids the potential confusion that a line like """x y"" ""z 1 2""" can cause.
For the example used by the OP, it would look like this:
ReturnCode = WshShell.Run(chr(34) & "C:\Program Files\Virtutech\Simics 4.2\Simics 4.2.9\x86-win32\bin\simics.exe" & chr(34), 0, True)
The reason I like this is maybe a little clearer when it comes to a path where you're throwing in command line arguments. For example, when you look at this:
"""C:\Program Files\Some Vendor\Application\program.exe"" -file ""data file.txt"""
It's kind of hard to see what all those quotes are and to figure out what quotes are around what.
Compare that to:
chr(34) & "C:\Program Files\Some Vendor\Application\program.exe" & chr(34) & _
" -file" & chr(34) & "data file.txt" & chr(34)
To my eye, the chr(34) becomes a way to easily visually differentiate between quotation marks that are part of a string definition (e.g., "data file.txt") and those which are needed for Windows to understand the path/filename properly (which show as chr(34) in the example).
If you're consistent about using chr(34) to mean "this is a quotation mark I need in order for Windows to understand the next item in the code" and the normal quotation mark to specify the beginning and ending of a string value, it can even make debugging issues a little easier.
But ultimately each person should do what works for them. My approach takes more typing and might confuse someone who doesn't know what chr() is. The other takes less typing but requires you to do a bit more mental parsing of the string. Neither is really right or wrong.
put three double quotes around the path.
Related
I am a new bee for vbScript. I have a script that I am calling with an arguments which contains double quote. Following is the call
.vbs "a" 2 where variable a is JO"N
My VbScript:
If WScript.Arguments.Count > 0 Then
MsgBox "NAME:" + WScript.Arguments.Item(0)
MsgBox "NAME:" + WScript.Arguments.Item(1)
Else
MsgBox "Please pass a parameter to this script"
End if
*Note this works when a = JON but does not work when a= JO"N. I tried putting escape characters" (Example: "WScript.Arguments.Item(0)") but it does not work.
You can't.
At least, as the WScript argument parser handles and removes all quotes from the values in Arguments collection, you can not use the default argument handling for this task.
note: And we are leaving out of the problem if the final command you are running when calling your script (you have not included how/from where you make the call) will or not have problems because the additional quote interferes argument quoting rules.
You need to use some workaround to get the double quote to reach the script. Some approachs could be:
Replace the quote and any other problematic character (use some kind of escape sequence) before calling the script and revert the process inside your script.
Save the value you want to pass into an environment variable (how to make it depends on how you are callign the script) and then retrieve the value from your script (ex. here).
Use WMI to retrieve the full command line used to start the script, including all the quotes (ex. here) and write your own argument parser routine.
From my point of view, I would use the second option.
This will work: Chr(34) & Wscript.Arguments.Item(0) & Chr(34). Here Chr(34) function returns a double quote using its ASCII code 34.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have the following code that displays a MsgBox using two env variables:
Set wshShell = CreateObject("WScript.Shell")
Title = wshShell.ExpandEnvironmentStrings("%Title%")
Text = wshShell.ExpandEnvironmentStrings("%Text%")
x = MsgBox(Text, 4144, Title)
Whilst the code works I wish t have a new line character in the message. I have read the following which discusses this scenario:
How to use \n new line in VB msgbox() ...?
However when I sen the env variable to the following it is displayed literally.
"This is the first line" & vbCrLf & "and this is the second line"
Just in case the code above is unclear...
The env variables %Title% and %Text% are set with values like in these batch statements:
set Title="This is a title"
set Text="This is the first line" & vbCrLf & "and this is the second line"
The code reads and displays these env variables in a message box.
The expanded environment string is still a string, so VBScript doesn't evaluate it as VBScript code without you telling it to do so.
x = MsgBox(Eval(Text), 4144, Eval(Title))
However, Eval is evil and should be avoided.
A better approach would be to define your environment variables using a placeholder for the newlines (e.g. \n) and then replace the placeholders with actual newlines:
x = MsgBox(Replace(Text, "\n", vbNewLine), 4144, Replace(Title, "\n", vbNewLine))
I wanted to ask this as I looked and it's specific and couldn't find other threads on it.
I want to make a regex that will Capture everything that would lie between two quotations and the quotations as well surrounding.
like: "insert whatever string here (which can include " "'s)"
basically I want a regex line that would take the quotations AND everything in between them (can be anything).
So a line with quotations and anything that lies inside of it.
I can't seem to figure this out.
I think you are just having a problem with the single and double quotes. Use this:
%q{like: "insert whatever string here (which can include " "'s)"}[/".*"/]
From the regex side of things, you could try this:
str = %q{uncaptured " captured " " /captured " /uncaptured}
str[/".*"/]
#=> "" captured " " /captured ""
For a non-regex solution, you just find the first and last index and collect the substring in between:
str[str.index('"')..str.rindex('"')]
I want to write a vb or bat script that goes through a file and finds and replaces any quotation mark not followed or proceeded by a comma with a double quote.
Edit: solved
Ok so I got it I needed to use back references.
Incase anyone else needs something similar here is a little example script replacing " not followed or proceed by a comma with Doubleqoute
dim rp, file
set re = new RegExp
re.Pattern="([^,])("")([^,])"
re.Global=True
s="It is alive ""IT IS ALIVE"","
MsgBox re.Replace(s,"$1 DoubleQuote $3")
Ok so I got it I needed to use back references.
Incase anyone else needs something similar here is a little example script replacing " not followed or proceed by a comma with Doubleqoute
dim rp, file
set re = new RegExp
re.Pattern="([^,])("")([^,])"
re.Global=True
s="It is alive ""IT IS ALIVE"","
MsgBox re.Replace(s,"$1 DoubleQuote $3")
EDIT: Also the above does not work with one letter words in order to make it work with one letter you have to use look forward and look back functions.
I have some files of fixed line size, fixed field size that I need to extract information from. Nornmally, I'd use Cygwin (cut et al), but that's not an option in this case due to (boneheaded) management policies I can't change. It has to be done using standard XP toolset included with Windows.
I need to extract the 10 characters at offset 7 and 4 characters at offset 22 (zero-based), and output them to a file but with a slight twist:
The first field may have a negative, positive, or no sign (at the start or end). The sign should be moved to the front, or removed totally if it's positive.
The second field should have leading and trailing spaces removed.
For example:
1 2 3 <- ignore (these lines not in file,)
0123456789012345678901234567890123456789 <- ignore ( here only for info.)
xxxxxxx 15.22-yyyyyABCDzzzzzzzzzzz...
xxxxxxx 122.00+yyyyy XX zzzzzzzzzzz...
xxxxxxx 9yyyyyYYY zzzzzzzzzzz...
should produce (< indicates end of line):
-15.22,ABCD<
122.00,XX<
9,YYY<
If you working with modern windows, you are not restricted to cmd.exe commands natively, you can use vbscript. If your policy is not to use vbscript either, then I guess you should sack your management :)
Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
strFirstLine = objFile.ReadLine
Do Until objFile.AtEndOfStream
strLine= objFile.ReadLine
var1 = Mid(strLine,10) ' do substring from position 10 onwards
' var2 = Mid (strLine,<pos>,<length>) ' get next offset and save to var2
WScript.Echo var1 & var2 ' print them out.
Loop
Basically, to "cut" characters of a string, you use Mid() function. please look at the vbscript documentation to find out more.
Save the above as test.vbs and, on the command line, do
c:\test> cscript /nologo test.vbs > newfile
Of course, "substring" can also be done with pure cmd.exe but I will leave it to some others to guide you.
Update by Pax: Based on this answer, I came up with the following which will be a good start:
option explicit
dim objFs, objFile, strLine, value1, value2
if wscript.arguments.count < 1 then
wscript.echo "Usage: process <input-file>"
wscript.quit
end if
set objFs=createObject("Scripting.FileSystemObject")
set objFile = objFs.openTextFile(wscript.arguments.item(0))
do until objFile.atEndOfStream
strLine= objFile.readLine
value1 = trim(mid(strLine, 8, 10))
value2 = trim(mid(strLine, 23, 4))
if right(value1,1) = "-" then value1 = "-" & left(value1,len(value1)-1)
if right(value1,1) = "+" then value1 = left(value1,len(value1)-1)
if left(value1,1) = "+" then value1 = mid(value1,2)
wscript.echo value1 & "," & value2
loop
This matches all the requirements we had. We can make the offsets and lengths into command-line arguments later.
End update.
This site has some pointers on how to extract substrings in cmd.exe: http://www.dostips.com/DtTipsStringManipulation.php
That site suggests that you can use
%varname:~2,3%
to subscript a variable. This seems to fill your needs, except you now have to get each line into a variable.
Next you want to look at the ghastly for loop syntax and if and branching (you can goto :labels in batch).
This stuff is all rather ugly, but if you really have to go there...
Here is a page in SO on looping through files and doing stuff to them: How do you loop through each line in a text file using a windows batch file?
Here's a small script (needs to be in a BAT/CMD file) expanding on what Daren Thomas suggested:
#echo off
setlocal
set key=HKCU\Software\Microsoft\Windows\CurrentVersion\Ext\Stats\{D27CDB6E-AE6D-11CF-96B8-444553540000}\iexplore\AllowedDomains
echo.
echo Liste der fuer Flash im IE zugelassenen Domaenen:
echo =================================================
for /f "usebackq tokens=11 delims=\" %%l in (`call reg query "%key%" /s`) do echo. %%l
echo.
endlocal
The FOR loop is the central part. Note the use of command modifiers in double quotes. I specify tokens=11 because I'm only interested in the subkeys of AllowedDomains, which is at position 10.
Be sure to read the help in for /?. Daren is right when he says this stuff is all rather ugly. And it easily breaks down on modification. There's a lot of non-intuitive subtleties with cmd.exe script syntax.
By the way, the GUID is the COM class ID for the Shockwave Flash Add-on. It's been around since at least 2001 so might well continue to be relevant for the foreseeable future. The purpose of the script is to list the domains where Flash, which I prefer to block by default, has been allowed.