1

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)
Community
  • 1
  • 1
Deadmano
  • 217
  • 1
  • 7
  • 15

2 Answers2

0

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Can both of you look at my code above and suggest how I can implement either yours or the answer below into my code to get the desired effect? :) – Deadmano Oct 26 '13 at 10:51
  • Hi there @Ansgar Wiechers, and thank you for the reply! :) I'm basically just trying to make (convert) a script in VB Script that will ask the user the source directory they'd like backed up, and then asking them for the output directory. Obviously it needs to copy the parent folder itself, and back it up (mirrored) in another location. Later on (or now, if it is easy to implement) I'd like to either allow multiple directories to be selected in one go, or have a drag and drop feature to drop the folders you'd like backed up in... That's pretty much what I'd like to do. :) – Deadmano Oct 28 '13 at 08:48
  • By the way, I tested your code above, and while it does the job (I can just add the "browse backup location") the only issue is it has no progress report or reporting, so I'm not sure how hard that will be to implement as opposed to Robocopy that comes with those features (switches etc) and can be easily called upon... – Deadmano Oct 28 '13 at 08:50
0

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Can both of you look at my code above and suggest how I can implement either yours or the answer below into my code to get the desired effect? :) – Deadmano Oct 26 '13 at 10:51
  • Hi there, thanks for the reply! So my current code is: http://pastebin.com/TESywtpQ With your fix implemented, however, now it does something weird I can't figure out! It replicates it's parent folder perfectly, however, it adds an "E" folder inside the parent folder, and inside that "E" folder is the content, whereas the content should just go into the parent folder... Example, you want C:\Test it will backup to; Location:\Test\E\{CONTENT} Any reason why, and how to fix this? – Deadmano Oct 28 '13 at 08:26
  • Try to place a space before the /e switch – MC ND Oct 28 '13 at 08:52
  • I love how simply these issues can be, hahaha, wow, I was looking left right and center!! Man, this has definitely opened up my eyes and will help for future projects, thank you SO much!!! I REALLY appreciate it mate! Hope you have an awesome day further!! :) – Deadmano Oct 28 '13 at 09:13