How to run VBScript from command line - cmd

I am a beginner in VBScript.
I need execute in VBScript this command line:
C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL
I googled it & got to know that we can run VBScript from command line by executing below command and I have tried this:
Set oShell = Wscript.CreateObject("WScript.Shell")
oShell.run "cmd /k ""C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL", 1, True
But the script is stopped because in the path of the file my.ini there are some folder with space in the name, that is Program Files and MySQL Server 5.5
How to do resolve this?

You can save the Filepath in a Variable and run it.
Set oShell = Wscript.CreateObject("WScript.Shell")
Dim path = "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL"
oShell.run "cmd /k " path , 1, True
Good Luck
Jonas

First : To make life easier just use this function to double quotes your strings and debug your command with wscript.echo or a MsgBox before executing it !
SQL_CMD = "cmd /k "& DblQuote("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld.exe") &_
"--defaults-file="& DblQuote("C:\Program Files\MySQL\MySQL Server 5.5\my.ini") & " MySQL"
wscript.echo SQL_CMD
'--------------------------------------
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'--------------------------------------

Related

vbs text concatenation not working with wscript.arguments

I made a js script to convert text to speech and running a command line with vbs to keep the console window hidden. I am calling via a command so Im trying to use the parameters passed in to it.
I tried using the '+' operator like in most other languages but it didn't work, then I tried the '&' operator but to no success :(.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c node audio.js" + WScript.Arguments.Item(0)
oShell.Run strArgs, 0, false
This
C:\Users\eh
type 56861329.vbs
WScript.Echo "cmd /c node audio.js" + WScript.Arguments.Item(0)
C:\Users\eh
cscript 56861329.vbs MissingSpace
cmd /c node audio.jsMissingSpace
should show the real problem. As #Geert said, & is the VBScript operator for concatenation and should be used.
I forgot a space I feel so dumb

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 to run a cmd file using vbscript? [duplicate]

I need to run a command to copy a file from one location to another through Command Prompt using a vbs file. this is what I have however it keeps throwing an error at me.
'Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C copy "S:Claims\Sound.wav" "C:\WINDOWS\Media\Sound.wav"
Set oShell = Nothing'
The error i get is:
'Script: C:\******\command.vbs
Char: 30
Error: Expected end of statement
Code: 80040401
Source: Microsoft VBScript compilation error'
Please help :)
The problem is on this line:
oShell.run "cmd.exe /C copy "S:Claims\Sound.wav" "C:\WINDOWS\Media\Sound.wav"
Your first quote next to "S:Claims" ends the string; you need to escape the quotes around your files with a second quote, like this:
oShell.run "cmd.exe /C copy ""S:\Claims\Sound.wav"" ""C:\WINDOWS\Media\Sound.wav"" "
You also have a typo in S:Claims\Sound.wav, should be S:\Claims\Sound.wav.
I also assume the apostrophe before Dim oShell and after Set oShell = Nothing are typos as well.
Set oShell = CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C copy ""S:Claims\Sound.wav"" ""C:\WINDOWS\Media\Sound.wav"" "

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.

Resources