0
votes

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:

picture of MsgBox sRoboCopyCommand (stripped out private stuff not relevant to issue, ie full paths and other eXcludeD fodlers)

1
As noted in the code, I tired that "4 quotes" method, and it does not work within the robocopy command. - BReal14
How exactly did it "not work". Your robocopy command apparently creates a log: what does it say? - Ansgar Wiechers
Did you try running the script outside the scheduled task? - Ansgar Wiechers
Hmm... try removing the trailing backslash from both source and destination path. - Ansgar Wiechers

1 Answers

1
votes

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