Question

I used to use a line in Robocopy that would allow me to copy all folders in a folder INCLUDING the parent folder, I.E all files/folders in the Blackberry folder INCLUDING the Blackberry folder itself, else without it it would just copy the files within and dump them in the backup location...

The code used was;

for %%a in ("%source%") do SET destination="Backups\%date%\%%~nxa"

Now in VB Script I've got; sSource = Chr(34) & objFolder.self.Path & Chr(34) & " "

So how would I go about having VB Script (which still calls Robocopy) use the above so that when it backs up it will include the PARENT folder as well?

This was the code I had; Converting Robocopy Batch To VB Script

Thanks in advance!

EDIT: The current content of my script file;

Dim BrowseBackupSource

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Please browse to the folder you would like to backup.", 1, "C:\")
If objFolder Is Nothing Then
    Wscript.Quit
End If
wscript.Echo "folder: " & objFolder.title & " Path: " & objFolder.self.path

Dim BrowseBackupLocation

Set objShell = CreateObject("Shell.Application")
Set objDest = objShell.BrowseForFolder(0, "Please browse to the folder you would like to save the backup to.", 1, "C:\")
If objDest Is Nothing Then
    Wscript.Quit
End If
wscript.Echo "folder: " & objDest.title & " Path: " & objDest.self.path

sCmd = "%windir%\System32\Robocopy.exe "
sDate = Day(Now) & "-" & Month(Now) & "-" & Year(Now)
sTime = Hour(Now) & "-" & Minute(Now) & "-" & Second(Now)
sSource = Chr(34) & objFolder.self.Path & Chr(34) & " "
sDestination = Chr(34) & objDest.self.Path & Chr(34) & " "
sSwitches = "/E /Log:"& sTime &".txt"

Set objShell = CreateObject("Wscript.Shell")
objShell.Run(sCmd & sSource & sDestination & sSwitches)
Was it helpful?

Solution

Well, if you need the path to the parent folder to set as root of copy:

dim parentFolderPath

    parentFolderPath = WScript.CreateObject("Scripting.FileSystemObject").GetFolder(objFolder.self.Path).ParentFolder.Path

EDIT

You need the name of the selected source directory added to the path of the selected destination so

Set fso = WScript.CreateObject("Scripting.FileSystemObject")
sSourceFolderName = fso.GetFolder(objFolder.self.Path).Name
sDestination = Chr(34) & objDest.self.Path & "\" & sSourceFolderName & Chr(34)

Robocopy will handle the target directory creation

OTHER TIPS

If you want to create a copy of a particular folder for backup, why don't you simply copy that folder to the backup destination and be done with it?

Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")

dst = "C:\backups\" & Year(Now) & "\" & Month(Now) & "\" & Day(Now)
CreatePath dst

Set fldr = app.BrowseForFolder(0, "Example", 1, "c:\Programs")
fso.CopyFolder fldr.Self.Path, dst & "\", True

Sub CreatePath(p)
  If Not fso.FolderExists(p) Then
    CreatePath fso.GetParentFolderName(p)
    fso.CreateFolder p
  End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top