Escaping spaces in folder path - vbscript

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

Related

How do I use 7-zip command line in vb script to delete file from zip folder?

I have this vb script:
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
'Run the 7-zip command line instruction via thw WshShell to delete files in Tableau Export Packaged Workbook
desfile = "C:\Tableau_Dashboards\Partner_Life_Cycle\ALL_PARTNERS\twbx\Partner_Life_Cycle_ALL_PARTNERS.zip"
srfile = "C:\Tableau_Dashboards\Partner_Life_Cycle\ALL_PARTNERS\twbx\Data"
strCommand = "C:\Program Files\7-Zip\7z.exe d -tzip & desfile & srfile & "
' Run 7-Zip in shell
WshShell.Run strCommand,0,true
WScript.Sleep 5000
I get the error "Line 15: The system cannot find the file specified"
Line 15 is the WshShell.Run strCommand,0,true part.
How can I fix this?
Thanks
Rick
In your code, the concatenation operator & will be treated as a part of the string since you are not closing the previous string with a quote.
It should be:
strCommand = "C:\Program Files\7-Zip\7z.exe d -tzip" & " " & desfile & " " & srfile
(With space added between the paths.)
As the path to the exe contains space (Program Files), it will have to be enclosed in quotes as: "C:\Program Files\7-Zip\7z.exe"
To get a quote within a string in vbscript, the quote has to be doubled .
strCommand = """C:\Program Files\7-Zip\7z.exe"" d -tzip" & " " & desfile & " " & srfile

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

Call shell with spaces in file path

The code below unzips a .zip file using winzip. However, if the FolderPath contains a blank, I get non-trappable error "Failed to open document.", without an error number. How can I unzip this file with Shell if the FolderPath contains a blank? Thank you,
FolderPath = ThisWorkbook.Path & "\"
Unzipfile = Dir(FolderPath & "*.zip")
While UnzipFile <> ""
if InStr(1, UnzipFile, ".zip") > 0 Then
ShellStr = "C:\Program Files\WinZip\winzip32 e- " & FolderPath & UnzipFile & " " & FolderPath
Call Shell(ShellStr, vbHide)
You need to quote paths with spaces
ShellStr = """C:\Program Files\WinZip\winzip32.exe"" e- """ & _
FolderPath & UnzipFile & """ """ & FolderPath & """"

VBscript Robocopy Syntax

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

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.

Resources