How to execute commands with variables as path in vbscript - vbscript

I can compile my visual basic project Project1.vbp into exe through this command
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C cd ""C:\Program Files (x86)\Microsoft Visual Studio\VB98"" & vb6/make ""C:\Users\Ankit\Desktop\New folder (5)\Project1.vbp"" ""C:\Users\Ankit\Desktop\New folder (5)\Project1.exe""")
But when I take
p="C:\Program Files (x86)\Microsoft Visual Studio\VB98"
m="C:\Users\Ankit\Desktop\New folder (5)\"
And try to execute this command
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C cd "p" & vb6/make "m"Project1.vbp"" "m"Project1.exe""")
It results in error.
kindly provide me some solution to correct this command to compile the project to exe.

You have to concatenate strings using & and also enclose your paths in " quotes. (Inside a string, quotes are escaped as "".)
Set d = objShell.Exec("cmd.exe /S /C cd """ & p & """ & vb6/make """ _
& m & "Project1.vbp"" """ & m & "Project1.exe""")

Related

quotes inside quotes with windows' cmd

I have the following command set as a custom URL protocol in windows registry:
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start C:\Program Files\uvnc bvba\UltraVNC\vncviewer.exe !s:_= !"
This is not working because the path to the exe file has a space.
Normally, I'd use quotes:
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start "C:\Program Files\uvnc bvba\UltraVNC\vncviewer.exe" !s:_= !"
but the command is already in quotes. I tried escaping the quotes with double quotes, but that did not work.
How can I achieve this?
Simplest work around is use the historic short folder name
That should be C:\PROGRA~1\UVNCBV~1\
check using dir /x /a:d C:\PROGRA~1\U*
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start C:\PROGRA~1\UVNCBV~1\UltraVNC\vncviewer.exe !s:_= !"

Extract archive with password that contain double quote

i want to extract an archive called test.rar with 7zip but nothing worked for me
Archive: test.rar
Password(with quote):"."
I tried
& "C:\Program Files (x86)\7-Zip\7z.exe" t -p'"".""' test.rar
& "C:\Program Files (x86)\7-Zip\7z.exe" t -p""."" test.rar
& "C:\Program Files (x86)\7-Zip\7z.exe" t "-p"."" test.rar
& "C:\Program Files (x86)\7-Zip\7z.exe" t -p'"."' test.rar
& "C:\Program Files (x86)\7-Zip\7z.exe" t -'p"."' test.rar
& "C:\Program Files (x86)\7-Zip\7z.exe" t -p'"".""' test.rar
Up to at least PowerShell 7.1, passing arguments with embedded " characters to external programs such as 7z.exe is fundamentally broken, unfortunately.
While there are workarounds, the simpler solution is to provide the password via stdin.
The post that Mark Tolonen links to shows this technique for cmd.exe, via <, its input-redirection operator, which PowerShell does not support, however.
Instead, use PowerShell's pipeline to supply data that an external program receives via stdin.
Assuming that the verbatim password is ".":
'"."' | & "C:\Program Files (x86)\7-Zip\7z.exe" t test.rar

VBScript Command window - passing a command syntax

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

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.

vbs cmd path space

I would like to be able to call the following cmd command from within a vbs script:
cmd Client\setupclient.exe /q /targetdir "c:\program files\Microsoft CRM"
I came up with the following vbs script:
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Run "cmd /c Client\setupclient.exe /q /targetdir c:\program files\Microsoft CRM", 1, true
As far as I am concerned, this would work properly if the targetdir had no spaces, e.g c:\bla. Then the app would be installed in that particular folder.
The obvious question is, how can I define the targetdir with spaces as the path location.
I tried to surround it with ' ' but that didn't work for me. Any suggestions?
Cheers
chris
Paths with spaces are typically enclosed in quote characters ("). In VBScript, to insert a quote character into a string you use double quotes (""). So, your code should look like this:
oShell.Run "cmd /c Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true
Also, I'm not sure if cmd /c is actually needed here, so it might work this way as well:
oShell.Run "Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true
I ended up with
AMPath = "E:\Program Files (x86)\Dropbox\Client\Dropbox.exe"
If FileSyst.Fileexists(AMPath) Then
AMPath = chr(34) & AMPath & chr(34)
OBJ_Shell.run (AMPath)
End If
1、If your OS supports 8.3 filename,you can try short filename:
cd c:\
dir /x
2017/04/17 20:53 <DIR> PROGRA~1 Program Files
2017/04/18 03:40 <DIR> PROGRA~2 Program Files (x86)
Then repalce C:\Program Files\ with PROGRA~1.
2、use two double-quotes within full path.
WScript.CreateObject("WScript.Shell").Run """C:\Program Files\DirName\FileName.exe"" /option1 value1 /option2 vaule2 argv3"
This is not exactly the problem described, in that the called program rather than a parameter contains a space. Googling "whshell.run does not work if filename contains blanks" got me here.
When the called program contains a space in its name, it needs to be triple quoted. (The starting and ending quotes define a string with blanks and the enclosed double quotes are mapped to single quotes within that string.) There are two working examples. The first uses triple quotes. The second effectively removes the blanks from the name. The non-working examples show what not to do (and what I tried first.)
' Drive D:\Program Files\Batch\Monitor.bat with no associated command window
Set WshShell = CreateObject("WScript.Shell")
' These methods work: (Select one)
Return = WshShell.Run("""D:\Program Files\Batch\Monitor.bat""", 0)
' Return = WshShell.Run("D:\.D-DISK\Monitor.bat", 0)
' Note: Here "D:\.D-DISK\Monitor.bat" is a symbolic link to
' "D:\Program Files\Batch\Monitor.bat"
' The following methods fail because of the space in the filename.
' WshShell.Run( chr(34) & D:\Program Files\Batch\Monitor.bat & Chr(34), 0 )
' Return = WshShell.Run("D:\Program Files\Batch\Monitor.bat", 0)
' Return = WshShell.Run(""D:\Program Files\Batch\Monitor.bat"", 0)
Set WshShell = Nothing

Resources