3

I'm trying to convert a BATCH file I am still working on (question at Robocopy | Mirror Destination Including Source Parent Folder).

I've made some progress, and the reason I moved to VB is to add a bit more functionality, like adding a dialog box to ask the user to browse for a folder they'd like to backup...

Now the code I currently have (only partially converted from my original .bat file);

Dim Command1

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Example", 1, "c:\Programs")
If objFolder Is Nothing Then
    Wscript.Quit
End If
wscript.Echo "folder: " & objFolder.title & " Path: " & objFolder.self.path

sCmd = "%windir%\System32\Robocopy.exe "
sDate = Day(Now) & "-" & Month(Now) & "-" & Year(Now)
sTime = Hour(Now) & "-" & Minute(Now) & "-" & Second(Now)
sSource = objFolder & " "
sDestination = "Backups\"& Year(Now) &"\"& Month(Now) &"\"& Day(Now) &"\ "
sLogDir = "Backups\Logs\"& Year(Now) &"\"& Month(Now) &"\"& Day(Now) &"\ "
sSwitches = "/SEC /E /Log:"& sTime &".txt"

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

My issue is that this is what happens according to the log file;

Source = G:\test\delete\
Dest = G:\test\Backups\2013\10\23\

Meanwhile the true source is;

C:\Users\User\Desktop\delete

So what I'd like to try to figure out is why it is affixing "G:\test", the folder the .vbs is being run from, to its source.

All in all, my goal is to just have Robocopy copy files, but the source is based on user input (hence the select a folder option). I would also like to add a "destination" option, that you specify where to backup to... But that is really optional, I'm sure I can figure that out if I get this first issue sorted.

Thanks in advance for any and all assistance!

Community
  • 1
  • 1
Deadmano
  • 217
  • 1
  • 7
  • 15
  • 2
    Maybe `sSource = objFolder.self.Path & " "` – MC ND Oct 23 '13 at 10:07
  • 1
    Thank you, you're a star!!! However, it only seems to be working with folders that have no spaces in them... :( If I have a folder name with spaces, like "Big Pictures" or something, it just does nothing. – Deadmano Oct 23 '13 at 11:10
  • 2
    Sorry. `sSource = Chr(34) & objFolder.self.Path & Chr(34) & " "` . When calling robocopy if source contains spaces, quotes are needed to enclose paths. – MC ND Oct 23 '13 at 11:17
  • 1
    That, once again, worked like a charm! I seriously need to get researching and figuring these things out, although, to some credit I did try initially double quotes "" "" and did read about the Chr(34) to cancel it out. :) Now if I can just figure out why under Windows 8 when trying to copy any file, it gets Access Denied? UAC isn't enabled, does Win8 do some funky stuff with scripts? Or is there anything I need to add to ensure that it works properly, since there isn't a "Run as Admin" context-menu option. :( – Deadmano Oct 23 '13 at 11:22
  • 2
    You should investigate what is giving you Access Denied. It can be source file read, destination file write, access control change (you are using /SEC), write of log file, ... – MC ND Oct 23 '13 at 11:36
  • 1
    Thanks for this, it was due to the /SEC switch. :) All sorted now, and figured out how to do a "Browse For Backup Folder" and "Browse For Backup Location", yay!! :D Now the only issue I'm having trouble converting is where the "Logs" folder gets put... I want it to be in the root of where they choose the backup destination under "Backup Logs/HH:MM:SS.txt" however I just can't get it working... Here's that line: `sSwitches = "/E /Log:"& Chr(34) & objDest.self.Path & Chr(34) & sTime &".txt" ` based on the destination; `sDestination = Chr(34) & objDest.self.Path & Chr(34) & " " ` – Deadmano Oct 23 '13 at 12:11
  • 1
    quotes ( chr(34) ) must enclose complete path, including file name, not only directory in which you place the log. – MC ND Oct 23 '13 at 12:13

1 Answers1

1

Well if it saves you any time...

RoboCopy GUI exists.

http://technet.microsoft.com/en-us/magazine/2006.11.utilityspotlight.aspx

This is a simple utilization of VB Script.

See this for prompting the user for folder selection. VBScript to open a dialog to select a filepath

Once you get the jist of how to use the code recommended by the other user on this link. A brief intro to my madness... Prompt the folder you wish to save and spit it out to a batch called Input then prompt again where to save the backup. Then call that batch file like this:

Call input.bat

Before your RoboCopy lines.

So lets determine how to leverage the vb code.

'Open Windows Shell Script object
    Set wShell=CreateObject("WScript.Shell")
'Executes the MS HTML Application exe to leverage capabilities to select a file.
    Set iExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
'Sets First Prompt for source reference
    srcepath = iExec.StdOut.ReadLine
    Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
'Sets Second Prompt for destination reference
    destpath = oExec.StdOut.ReadLine
    Set wShell=Nothing

Simple VBS adjustments to save the input.bat from the prompting.

'This will open the new bat file as txt and write.
    const forwriting = 2
    set fso = CreateObject("Scripting.FileSystemObject")
    set output = fso.OpenTextFile("input.bat", ForWriting, True)
    output.writeline "set srcepath=" & srcepath
    output.writeline "set destpath=" & destpath

So instead of converting your batch, leverage the tools you have available and make it cost effective for your effort.

Community
  • 1
  • 1
Steve Kline
  • 805
  • 4
  • 11