VBScript Command window - passing a command syntax - vbscript

I'm trying to get a command to run in a cmd vindow via VBS. Just like this answer:
How to keep the VBScript command window open during execution
The command I'm trying to issue is this, as written it works in a .cmd file.
"\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec" /x86 /f "\\path\folder\folder with space\Import.dtsx"
I've been unable to get it to work in the syntax from the above answer:
objShell.run "%comspec% /c ""SomeProgram.exe -R & pause""", 1, True
Figure it's a double quote issue, but I can't find it.
(I have to use the whole path to dtexec to force usage of the 16bit version.)
Followup: =======================================================
This works:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f c:\temp\Import.dtsx & Pause", 1, True
This does not:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f ""c:\temp\temp two\Import.dtsx"" & Pause", 1, True
nor this:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec /x86 /f c:\temp\temp two\Import.dtsx"" & Pause", 1, True
It's that space in the filename argument that is hosing it.

You don't need pause, just tell CMD to keep the window open after the command finishes (/k) instead of closing it (/c):
objShell.Run "%comspec% /k program.exe -R", 1, True
Nested double quotes are only required when you have a path with spaces in it, e.g.:
objShell.Run "%comspec% /k ""C:\some folder\program.exe"" -R", 1, True
Edit: If arguments in your commandline are paths with spaces as well you need quotes around each path and another set of quotes around the entire statement:
objShell.Run "%comspec% /c """"C:\some folder\program.exe"" /p ""foo bar"" & pause""", 1, True

Related

Echo Code into a File using VBScript

I am writing a VBScript which I want to use to echo the code of another VBScript into an output file.
However, I am unable to write some of the characters to the output file using this method.
If I use the command line method:
cmd.exe /c "#echo "hello"">output.vbs
This works and the string: "hello" is written to the output file.
However, when I do the same using a VBScript, it does not work.
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
So, is there a way I can echo it into another file retaining the double quotes?
Thanks.
Your quoting is wrong.
Change this:
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
into this:
objShell.Run "%comspec% /c #echo ""hello"">output.vbs"

Running command line: can't find file

I'm trying to run a few commands from VBScript:
Dim objShell
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd /c C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts"
objshell.Run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
WScript.Sleep 500
wshshell.SendKeys "{ENTER}"
But I get this error
biarimport.vbs(4, 1) (null): The system cannot find the file specified.
It seems pretty obvious that either lcm_cli.bat or LCMBiar_Import.property file is not there but it's not the case it's all there and it works fine if I directly run it through CMD.
Running a statement
objShell.run "cmd /c C:\some\folder"
does not change the working directory to that folder. It just spawns a CMD instance, which throws an error that the command C:\some\folder isn't recognized and then closes immediately (/c).
Because the working directory didn't change, the subsequent statment runs in the wrong working directory and thus can't find lcm_cli.bat:
objshell.run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
Either use the CurrentDirectory property for changing the working directory:
objShell.CurrentDirectory "C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts"
objshell.Run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
or run the batch script with its full path:
objShell.Run """C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts\lcm_cli.bat"" -lcmproperty C:\LCMBiar_Import.property"
Note that any path containing spaces must be put in nested double quotes when used in a Run statement (bot not with the CurrentDirectory property). Also, you don't need cmd /c for starting batch scripts. It's only required if you use CMD built-ins like dir or I/O redirection.

How can I get a one liner xcopy command to run in vbscript?

I have an xcopy that cannot run correctly in a .bat (batch) file because of some Norwegian characters.
The line is:
xcopy /I /V /H /R /C /Y /K /O /X "G:\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF" "F:\CAD\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
If I run it from a batch file it fails as "0 files copied" because it translates the source file incorrectly due to the character set.
I've tried changing the charsets using chcp as well as using Notepad++ and encoding it with various charsets but it still fails due to a limitation I guess in batch processing in Windows.
So my question is, how can I get the line above to execute in a vbscript and record the xcopy output?
I tried:
set objShell = CreateObject("wscript.Shell")
strSource = "G:\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
strDest = "F:\CAD\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
command = "xcopy /I /V /H /R /C /Y /K /O /X " & chr(34) & strSource & chr(34) &chr(32) & chr(34) & strDest & chr(34) & " > F:\testvbs.log 2>&1"
objshell.run command
and then execute "cscript test.vbs"
But unfortunately this doesn't work despite the fact that if I change the objshell.run to wscript.echo it does show the proper output for the "command" variable...meaning it shows the right syntax.
So...any programmers know how I can properly run that original xcopy line in a vbscript? If you know how to run it in a batch file properly with the encoding, that's fine too...I just haven't figured that out yet.
Next script works with combined Norwegian-Czech folder and file names under under next conditions:
save the script UTF-16LE encoded with the 0xFFFE byte order mark, and
run the script as administrator!
Here's commented code snippet:
' save the script UTF-16LE encoded with the 0xFFFE byte order mark
' run the script as administrator!
' consider //U option: Use Unicode for redirected I/O from the console
' cscript //U 30767978.vbs
'
option explicit
dim objShell, strSource, strDest, command, cmd, xx
set objShell = CreateObject("wscript.Shell")
strSource = "D:\test\éíáýžřčšě\a Ø1000XØ90 b.txt"
strDest = "D:\test\ěščřžýáíé\a Ø1000XØ90 b.txt"
' chcp does not matter: `dir` as well as `xcopy` work for all
cmd = "%comspec% /U /C chcp 65000 & "
cmd = "%comspec% /U /C chcp 65001 & "
cmd = "%comspec% /U /C chcp 437 & "
cmd = "%comspec% /U /C chcp 852 & "
cmd = "%comspec% /U /C chcp 1250 & "
' chcp supposedly matters for redirected output
command = "dir " & """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)
command = "xcopy /I /V /H /R /C /-Y /K /O /X " _
& """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)
Wscript.Echo xx, cmd & command
While the accepted answer by JosefZ works well, I found that I could handle this much easier by simply taking my lines of xCopy and moving them from the .bat file to a Powershell .ps1 file and running it. Powershell doesn't balk at the character sets like Batch files do for some reason.
So I installed Powershell on the 2003 server and ran the script that way and it worked well.
I accepted JosefZ's answer since it answered the actual question, but posting this here in case others come across the same issue and want to utilize Powershell instead.

WScript v/s CScript

I have query related to this topic. Here is my script. I'm using below script to edit users on HP ILo board, it works fine, no error at all.
BUT here I can see cmd prompt, how do I run it in silent mode, i.e I do not want to see any cmd prompt, because I use BMC, so let it run in background, I will check the output later.
As you said I can simply change WScript to CSrcipt. But that does not work.
Any help please, please let me know where to modify.
Set wshShell = WScript.CreateObject ("WScript.Shell")
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User1.xml /l log1.txt > output1.txt"
WScript.Sleep 1*60*1000
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User2.xml /l log2.txt > output2.txt"
Set wshShell = Nothing
Wscript.quit
Regards
Use the second parameter of the .Run method
intWindowStyle Optional. Integer value indicating the appearance of
the program's window. Note that not all programs make use of this
information.
As I know nothing about BMC, I'd start with minimized (7, 6) before I'd try hidden (0).

VBS How to call cmd.exe using a string variable with spaces

I need to call the following:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /c copy /y C:\input\" & WScript.Arguments(0) & " C:\output", 0
where the input argument may be "File Name.txt". I have seen countless examples of people doing the same thing using double quotes for a hard coded file location, but nothing using an input argument or variable. What syntax is required so that the command line receives:
copy /y "C:\input\File Name.txt" C:\output
and not
copy /y C:\input\File Name.txt C:\output
for an arbitrary file name?
Embed the needed quotes (escaped via doubling) in the surrounding literals:
WshShell.Run "cmd /c copy /y ""C:\input\" & WScript.Arguments(0) & """ C:\output", 0
background, further reading

Resources