VBscript Robocopy Syntax - vbscript

I am having a problem with what I think is a syntax error in VBscript regarding running robocopy.
The following is a code snippet of what I now using to try to run robocopy:
Dim Command2
sLocalDestinationPath = "C:\Script\files\outzips\"
sFinalDestinationPath = "C:\CopyTestFolder\"
Command2 = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath
The thing is that the command does not produce any errors, but it also does not copy any files from the local path to the final path. It runs perfectly fine when executed from the command line. Any help would be greatly appreciated because this simple command is keeping me from finishing the rest of this script.
I also have it echoing out the command and the command matches exactly what I put in the command line.
Thank you, if you need anymore explanation just let me know.

You don't say how you are trying to 'run' Robocopy, but I presume it is via WScript.Shell.Run().
I don't happen to have Robocopy handy, but I did work up an example using Windows XCopy. Perhaps you can adapt my simple XCopy example to gain more insight into your problem with Robocopy.
Option Explicit
' XCOPY doesn't Like trailing slashes in folder names
Const sLocalDestinationPath = "C:\Script\files\outzips"
Const sFinalDestinationPath = "C:\CopyTestFolder"
Dim Command2 : Command2 = _
"XCOPY" _
& " " & sLocalDestinationPath _
& " " & sFinalDestinationPath _
& " /E /I /Y" _
& ""
Dim oSh : Set oSh = CreateObject("WScript.Shell")
WScript.Echo "Cmd: [" & Command2 & "]"
On Error Resume Next
Dim nRetVal : nRetval = oSh.Run(Command2, 0, True)
If Err Then
WScript.Echo "An exception occurred:" _
& vbNewLine & "Number: [" & Hex(Err.Number) & "]" _
& vbNewLine & "Description: [" & Err.Description & "]" _
& ""
Else
If nRetVal Then
WScript.Echo "Copy error: [" & nRetVal & "]"
Else
WScript.Echo "Copy succeeded."
End If
End If
Set oSh = Nothing
' XCOPY options:
'
' /E Copies directories and subdirectories, including empty ones.
' Same as /S /E. May be used to modify /T.
'
' /I If destination does not exist and copying more than one file,
' assumes that destination must be a directory.
'
' /Y Suppresses prompting to confirm you want to overwrite an
' existing destination file.
' End

Related

Escaping spaces in folder path

I have a backup script the copies from one server to another via scheduled task. Most of the folders copy ok. However, there is one folder that has a space in the name and it blows the whole thing up.
This runs on the destination server (pulls data in).
I've tried various escape patterns, and they all fail.
(vars are dimmed, code truncated)
sArchiveFolder = "D:\Backup\" & year(now) & "-" & month(now) & "-" & day(now) & "\"
sDataFolder = "\\Server\Share\System Library"
sDestFolder = sArchiveFolder & "System Library\"
Call subCopyFolder(fso, objShell, sDataFolder, sDestFolder)
sub subCopyFolder(fso, objShell, sDataFolder, sArchiveFolder)
dim iCounter, excludedDirs
if not(fso.folderexists(sArchiveFolder)) then
fso.createfolder(sArchiveFolder)
excludedDirs = " /XD Logs"
if(right(sDataFolder,7)="Library") then
'this fails
'sDataFolder = """"&sDataFolder&""""
'sArchiveFolder = """"&sArchiveFolder&""""
'so does this
'sDataFolder = chr(34)&sDataFolder&chr(34)
'sArchiveFolder = chr(34)&sArchiveFolder&chr(34)
end if
Dim sRoboCopyCommand
sRoboCopyCommand = "robocopy " & sDataFolder & " " & sArchiveFolder & " /E "& excludedDirs &" /R:5 /W:1 /log+:log.txt"
objShell.Run (sRoboCopyCommand)
end sub
How do I properly escape this? I also tried putting the literal quotes in the robocopy command line itself and that broke the folders that don't need the quotes too.
As noted in the code, I tried the "4 quotes method" and it does not work within the robocopy command line.
with 4 quotes method:
(stripped out private stuff not relevant to issue, ie full paths and other eXcludeD fodlers)
For posteriority: apparently trailing backslashes in source or destination path mess up robocopy's parameter handling, so the paths need to be specified without them:
sArchiveFolder = "D:\Backup\" & year(now) & "-" & month(now) & "-" & day(now)
sDataFolder = "\\Server\Share\System Library"
sDestFolder = sArchiveFolder & "\System Library"
...
sRoboCopyCommand = "robocopy """ & sDataFolder & """ """ & sArchiveFolder & _
""" /E " & excludedDirs & " /R:5 /W:1 /log+:log.txt"
objShell.Run sRoboCopyCommand

invalid Directory name used in property "CurrentDirectory" vbscript

I'm trying to compile au3 script through vbscript which located in another directory, so I used "CurrentDirectory" property to change the working directory from script directory to au3 file directory using this code
drivepath = "K"
strTempTarget = "New Folder"
filename = "gate.jpg"
IconName = "102.ico"
Comm = "cmd /c " & "Aut2Exe.exe /in " & filename & ".au3" & " /out " & filename & ".exe" & " /icon " & IconName
Path = """" & drivepath & "\" & strTempTarget & """"
MsgBox(Path)
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = Path
objShell.Run(Comm), 0, True
But I got an error "invalid file name or directory name"
since there is an empty space in "strTempTarget" value I should put a double quotes at the start and at the end of directory name "Path" I even tried to use ASCII : something like
Path = Chr(34) & drivepath & "\" & strTempTarget & Chr(34)
but the same error keep raising
So how to make this script work fine ?
The shell needs quoted pathes, because it uses space as separator; .CurrentDirectory 'knows' the whole string (included any spaces) is meant to be a folder path; so don't quote the string for .CurrentDirectory.
Evidence:
>> Set objShell = WScript.CreateObject("WScript.Shell")
>> Path = """C:\Documents and Settings"""
>> objShell.CurrentDirectory = Path
>>
Error Number: -2147024773
Error Description:
>> Path = "C:\Documents and Settings"
>> objShell.CurrentDirectory = Path
>>
>> <-- no news are good news

How to pass more than one variable in .bat file using VBS

I have a vbs code like:
Message4 = "Please enter Check Out Path"
Title4 = "Check Out Path "
variable1 = InputBox(Message4, Title4,"", 4500, 4500)
Message5 = "Please enter SVN URL"
Title5 = "SVN URL "
variable2 = InputBox(Message5, Title5, "", 4500, 4500)
Folder\batchfile.bat"""
objWshell.Run "batxhfile.bat"
and also batch file named batchfile.bat
#echo off
Color 42
echo. [ SVN Updater ]
set Checkout_path=checkout path
set SVN=C:\Program Files\TortoiseSVN\bin
set svn_url=SVN path
echo. Updating %Checkout_path% to SVN...
"%SVN%\TortoiseProc.exe" /command:checkout /url:"%svn_url%" /path:"%Checkout_path%" /closeonend:2
echo. done.
echo.
echo. Operation complete.
Now i want to pass the value of variable1 and variable2 from vbs code to batch file in the place of checkout path and svn path i have tried lots of method but no success uptill plz help.
I agree why you wouldn't simply call 'TortoiseProc.exe' from a VBS, however you may have you reasons for calling via a batch script. Simply pass arguments into a batch from a VBS:
`Const ERR_SUCCESS = 0
Const BAT_SCRIPT = "<folder>\batchfile.bat"
Dim oWSH, sCmd, iRC, sVar1, sVar2
Set oWSH = WScipt.CreateObjects("WScript.Shell")
Do While sVar1 <> ""
sVar1 = InputBox("Please enter Check Out Path", "Check Out Path", 4500, 4500)
Loop
Do While sVar2 <> ""
sVar2 = InputBox("Please enter SVN URL", "SVN URL", 4500, 4500)
Loop
sCmd = "cmd.exe /c """ & BAT_SCRIPT & "" "" & sVar1 & "" "" & sVar2 & """"
WScript.Echo "COMMAND: sCmd"
On Error Resume Next
iRC = oWSH.Run(sCmd, 0, True)
If iRC <> ERR_SUCCESS And Err.Number <> ERR_SUCCESS Then
MsgBox "ERROR: [" & Err.Source & "] " & CStr(Err.Number) & " - " & Err.Description & vbCrLf & _
" '" & BAT_SCRIPT "' return code = '" & CStr(iRC) & "' from command:" & vbCrLf & _
" COMMAND: " & sCmd, vbOkOnly, "Batch Script Error"
End If
Err.Clear
Set oWSH = Nothing`
You're result batch script command should be called like the following:
cmd.exe /c "<folder>\batchfile.bat" "<var1>" "<var2>"
Ensure your batch script gathers arguments, removes string double quotes and validates correctly:
`set Checkout_path=%1
set Checkout_path=%Checkout_path:~1,-1%
set svn_url=%2
set svn_url=%svn_url:~1,-1%
if /i "%Checkout_path%" ne "" (
exit /b 99
)
if /i "%svn_url%" ne "" (
exit /b 99
)`
Hope this helps ;)

VBscript to zip log files with 7zip

Would like someone to take a look at my script and tell me where I am messing up.
It is a script to zip log files and then I would like to move them into a new folder that is going to be shared over a network. Right now I am just trying to get the part where it zips up the files using 7zip correctly.
I am very new to VB (like 2 days) so having some syntax problems I think.
Script is found below, thank you in advance for all advice and help
Option Explicit
WScript.Echo "Press to start zipping log files."
Dim objFile, objPath, objFolder, Command, PathLogs, RetVal
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell = CreateObject("WScript.Shell")
PathLogs = "C:\Testscripts\testfolder\" 'This path just has some test logs
' Loop through the logs and zip and move each file (if required, you could just move files with an '.log' extension)
Set objPath = objFSO.GetFolder(PathLogs)
For Each objFile In objPath.Files
If (LCase(objfso.GetExtensionName(objFile)) = "log") Then
Wscript.Echo objFile.Name
' zip and move files
'Command = """C:\Program Files\7-zip\7z.exe"" -m -ex """ & PathLogs & \objFile.Name objfso.GetBaseName(objFile) & "*.zip"" """ & PathLogs & objFile.Name & """"
Command = ""C:\Program Files\7-zip\7z.exe"" a -m -ex " & PathLogs & "" & objFile.Name & ".zip " & PathLogs & "" & objFile.Name & "
WScript.Echo "Command: " & Command
RetVal = objShell.Run(Command,0,true)
End If
Next
WScript.Echo "Zip Successful."
You have your quotes wrong. To use a quote inside a string, you have to duplicate the quote.
Command = """C:\Program Files\7-zip\7z.exe"" a -m -ex " _ 'this is the first part of the string
& PathLogs & objFile.Name & ".zip " & PathLogs & objFile.Name
If your Logfile or PathLogs can contain spaces they must be quoted as well.

Psexec not outputting to log file in VB script

I have a VB script which needs to run psexec to launch an app called md5 on a remote server. Md5 generates a hash key of a file and takes one parameter - the file path\name. I need to retrieve the has key that is generated to store in a variable. Below is the code I am using:
Set objShell = CreateObject("Wscript.Shell")
strcomputer = "remotecomputer"
tempDest = "C:\somedir"
filename = "somefile"
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop
The MD5 command is run however nothing is written to the log file. When I copy and paste strCommand (substituting all the variables for the actual data) into a cmd prompt and run it, it successfully writes the output of Md5 to the log file.
At the end of the day I just need the output of Md5, if anyone knows a better way than writing it to a log file please let me know. I have already tried using objExecObject.StdOut.Readall() to try and catch the output which resulted in random failures - sometimes it would catch the output, sometimes it wouldn't, without changing anything in the script.
Just a guess: Are you sure about what the current directory is when the script is running? Try giving an absolute path to the log file and see if it helps.
I found a solution for this. Instead of using the following code:
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop
I used this instead:
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
objShell.Run "%comspec% /c " & strCommand, 0, true
The script is now redirecting to log.txt properly.

Resources