Converting Robocopy Batch To VB Script

送分小仙女□ 提交于 2020-01-14 19:53:10

问题


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!


回答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.



来源:https://stackoverflow.com/questions/19538203/converting-robocopy-batch-to-vb-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!