Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a little assistance with my Robocopy Syntax…

Status
Not open for further replies.

Pyro777

MIS
Jul 14, 2006
47
US
I have a quick question about my syntax for the below script. I have set a script to ask the end user if they want to backup their files, it maps a network drive to their home directory, and I am trying to use Robocopy to copy all of their files at first, then only copy new/changed files. Everything looks to be running ok, except for robocopy, i get a popup command box then nothing. My script does not show any errors when ran through primalscript but I know that my Syntax for Robocopy is probably wrong.. Can anyone help ???

'File backup Script

Dim backupdriveletter,objnetwork,WshNetwork,strUser,fso,backupfolder,stime,objshell,objFSO
Dim spath,dpath,s0,strpath,WSHShell

'Declarations
Set fso = WScript.CreateObject("Scripting.FileSystemObject") 'Standard File System Object
Set WshNetwork = WScript.CreateObject("WScript.Network") 'Standard Network Object
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

spath = "C:\Documents and Settings\" & strUser & "\My Documents " 'Source Path we will be copying

dpath = backupdriveletter & ":\" & backupfolder 'Destination Path

s0 = "/MIR /XA:SH /XD appData /XJD /R:5 /W:15 /V /NP /M /LOG+:Backup.txt" ' backup switches for Robocopy, including the /m switch to only copy new/modified files



strUser = WshNetwork.UserName 'Pulls User Name, used to find user's profile folder
BackupDriveLetter = "Q" 'The Drive Letter for the users subdirectory on the Application Server.


'Asks to start the backup ******************************************************************************
X=MsgBox(" Good Afternoon todays date is " & Date & " **** Are you currently logged into VPN ?? **** ",36,"My Documents BackUp Utility")

If X = 6 Then 'If MsgBox is answered YES
X=MsgBox("Please press OK to proceed with your file backup",48,"My Documents BackUp Utility")
StartBackup() 'Start main Subroutine
Else

X=MsgBox("Please make sure you first login to VPN before trying again ",48,"My Documents BackUp Utility")
WScript.Quit 'If MsgBox is answered NO
End If

'Main Program ****************************************
Sub StartBackup()
'Check for Backup Drive
If (Not fso.DriveExists(BackupDriveLetter)) Then
Set objnetwork = WScript.CreateObject("WScript.Network")

' You must change the server name to the server where the GroupMarketing users home directory resides
objNetwork.MapNetworkDrive "Q:" , "\\HomeServer\users\" & strUser
End If

' Check to see if the My Documents Path exhists ********************************************************
Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName

strPath = "Q:\My Documents"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strPath) Then
Wscript.Echo "The folder exists."
Else
backupfolder = "\My Documents"
FSO.CreateFolder(backupdriveletter & ":\" & backupfolder) 'Create the backupdoc folder
End If
' ******************************************************************************************************
Set WSHShell = CreateObject("Wscript.Shell")
Call WshShell.Run("cmd.exe /c C:\robocopy.exe " & spath & " " & dpath & "-" & ".txt" & " " & s0)

End Sub
 
I haven't messed with Robocopy yet.

However, I suggest taking your command and feeding it into a wscript.echo prior to the Call WshShell.RUN line...and just double check the syntax is the way you want it.

Try:
Code:
wscript.echo "cmd.exe /c C:\robocopy.exe " & spath & " " & dpath & "-" & ".txt" & " " & s0
Call WshShell.Run("cmd.exe /c C:\robocopy.exe " & spath & " " & dpath & "-" & ".txt" & " " & s0)

End Sub

If everything looks good, try researching through Google on how to use Robocopy.


Also, looking at your code try this:
Code:
'File backup Script

Dim backupdriveletter,objnetwork,WshNetwork,strUser,fso,backupfolder,stime,objshell,objFSO
Dim spath,dpath,s0,strpath,WSHShell 

'Declarations
Set fso = WScript.CreateObject("Scripting.FileSystemObject") 'Standard File System Object
Set WshNetwork = WScript.CreateObject("WScript.Network")  'Standard Network Object
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")

strUser = WshNetwork.UserName 'Pulls User Name, used to find user's profile folder
spath = "C:\Documents and Settings\" & strUser & "\My Documents " 'Source Path we will be copying 

s0 = "/MIR /XA:SH /XD appData /XJD /R:5 /W:15 /V /NP /M /LOG+:Backup.txt" ' backup switches for Robocopy, including the /m switch to only copy new/modified files







'Asks to start the backup ******************************************************************************
X=MsgBox(" Good Afternoon todays date is " & Date & " **** Are you currently logged into VPN ?? **** ",36,"My Documents BackUp Utility")

If X = 6 Then 'If MsgBox is answered YES
 X=MsgBox("Please press OK to proceed with your file backup",48,"My Documents BackUp Utility")
 StartBackup() 'Start main Subroutine
Else
 
X=MsgBox("Please make sure you first login to VPN before trying again ",48,"My Documents BackUp Utility")
 WScript.Quit 'If MsgBox is answered NO
 End If

'Main Program ****************************************
Sub StartBackup()
'Check for Backup Drive
 If (Not fso.DriveExists(BackupDriveLetter)) Then 
Set objnetwork = WScript.CreateObject("WScript.Network") 

' You must change the server name to the server where the GroupMarketing users home directory resides
objNetwork.MapNetworkDrive "Q:" , "\\HomeServer\users\" & strUser
 End If

' Check to see if the My Documents Path exhists ********************************************************
Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName

strPath = "Q:\My Documents"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strPath) Then
    Wscript.Echo "The folder exists."
Else
backupfolder = "\My Documents"

BackupDriveLetter = "Q"   'The Drive Letter for the users subdirectory on the Application Server.
dpath = backupdriveletter & ":\" & backupfolder 'Destination Path

FSO.CreateFolder(backupdriveletter & ":\" & backupfolder)             'Create the backupdoc folder
End If
' ******************************************************************************************************
Set WSHShell = CreateObject("Wscript.Shell")
Call WshShell.Run("cmd.exe /c C:\robocopy.exe " & spath & " " & dpath & "-" & ".txt" & " " & s0)

End Sub

V/r,

SPC Key
United States Army
 
Also to add for future refference.

As far as I know if you are feeding a variable into a command, you need to initlize the variable first prior to the command.

BAD EXAMPLE:
Code:
wscript.echo strText
strText = "What I want to echo!"
RESULT WILL BE: "" < A blank echo box.

GOOD EXAMPLE:
Code:
strText = "What I want to echo!"
wscript.echo strText
RESULT WILL BE: "What I want to echo!"

Another set of examples.

BAD EXAMPLE:
Code:
strOne = strTwo & strThree
wscript.echo strOne
strTwo = "What I want to echo!"
strThree = "What I want to echo!"
RESULT WILL BE: "" < A blank echo box.

GOOD EXAMPLE:
Code:
strTwo = "What I want to echo!"
strThree = "What I want to echo!"
strOne = strTwo & strThree
wscript.echo strOne
RESULT WILL BE: "What I want to echo!What I want to echo!"


I say all that becuase I see your below line...
Code:
dpath = backupdriveletter & ":\" & backupfolder 'Destination Path

...tries to add "backupfolder" to the "dpath" string before the "StartBackup" sub function has even initilized the "backupfolder" variable.

V/r,

SPC Key
United States Army
 
Call WshShell.Run("cmd.exe /c C:\robocopy.exe [!]""[/!]" & spath & "[!]""[/!] [!]""[/!]" & dpath & "-" & ".txt[!]""[/!]" & " " & s0)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Also...you may want to remove the extra "&" sign right before and after text.

Call WshShell.Run("cmd.exe /c C:\robocopy.exe """ & spath & """ """ & dpath & "-.txt"" " & s0)

V/r,

SPC Key
United States Army
 
Keyster86 and PHV... You two rock... Thank you for helping me with my syntax and for your tips. Robocopy was still being a pain with the syntax but I finally got the Syntax figured out. I also changed part of this sctipt to check if the network drive is already mapped, if not it maps the drive. This is a great script for any users that travel and use VPN to connect to your domain and access their user folder on the server. Anyways thanks to the both of you !!! Here is the final product:

'File backup Script
Dim backupdriveletter,objnetwork,WshNetwork,strUser,fso,backupfolder,stime,objshell,objFSO
Dim spath,dpath,s0,strpath,WSHShell

'Declarations
Set fso = WScript.CreateObject("Scripting.FileSystemObject") 'Standard File System Object
Set WshNetwork = WScript.CreateObject("WScript.Network") 'Standard Network Object
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strUser = WshNetwork.UserName 'Pulls User Name, used to find user's profile folder
spath = "C:\Documents and Settings\" & strUser & "\My Documents\\" 'Source Path we will be copying

s0 = "/MIR /XA:SH /XD appData /XJ /R:5 /W:15 /V /NP /M /LOG+:Backup.txt" ' backup switches for Robocopy, including the /m switch to only copy new/modified files

'Asks to start the backup ******************************************************************************
X=MsgBox(" Good Afternoon todays date is " & Date & " **** Are you currently logged into VPN ?? **** ",36,"My Documents BackUp Utility")

If X = 6 Then 'If MsgBox is answered YES
X=MsgBox("Please press OK to proceed with your file backup",48,"My Documents BackUp Utility")
StartBackup() 'Start main Subroutine
Else

X=MsgBox("Please make sure you first login to VPN before trying again ",48,"My Documents BackUp Utility")
WScript.Quit 'If MsgBox is answered NO
End If

'Main Program ****************************************
Sub StartBackup()
BackupDriveLetter = "Q" 'The Drive Letter for the users subdirectory on the Application Server.

'Check for Backup Drive
If (fso.DriveExists(BackupDriveLetter)) Then
Wscript.Echo "You are connected to VPN and your home directory is currently connected"
Else
Set objnetwork = WScript.CreateObject("WScript.Network")

' NOTE:!!! YOU MUST CHANGE THE SERVER NAME to the server where the GroupMarketing users home directory resides
objNetwork.MapNetworkDrive "Q:" , "\\SERVER NAME\users\" & strUser
End If

' Check to see if the My Documents Path exhists ********************************************************
Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName

strPath = "Q:\My Documents\\"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strPath) Then
Wscript.Echo "The folder exists."
Else
backupfolder = "\My Documents"


FSO.CreateFolder(backupdriveletter & ":\" & backupfolder) 'Create the backupdoc folder
End If
' ******************************************************************************************************
dpath = backupdriveletter & ":\" & backupfolder 'Destination Path
Set WSHShell = CreateObject("Wscript.Shell")
' wscript.echo "cmd.exe /c C:\robocopy.exe """ & spath & """ """ & strpath & """ " & s0
Call WshShell.Run("cmd.exe /c C:\robocopy.exe """ & spath & """ """ & strpath & """ " & s0)

End Sub
 
Some clean up:
Code:
'File backup Script
'Declarations
Dim BackupDriveLetter, WshNetwork, strUser, objShell, objFSO
Dim spath, dpath, s0
Set WshNetwork = WScript.CreateObject("WScript.Network")  'Standard Network Object
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 'Standard File System Object

strUser = WshNetwork.UserName 'Pulls User Name, used to find user's profile folder
spath = "C:\Documents and Settings\" & strUser & "\My Documents\" 'Source Path we will be copying 
s0 = "/MIR /XA:SH /XD appData /XJ /R:5 /W:15 /V /NP /M /LOG+:Backup.txt" ' backup switches for Robocopy, including the /m switch to only copy new/modified files

'Asks to start the backup 
X=MsgBox(" Good Afternoon todays date is " & Date & " **** Are you currently logged into VPN ?? **** ",36,"My Documents BackUp Utility")
If X = 6 Then 'If MsgBox is answered YES
  X=MsgBox("Please press OK to proceed with your file backup",48,"My Documents BackUp Utility")
  StartBackup() 'Start main Subroutine
Else
  X=MsgBox("Please make sure you first login to VPN before trying again ",48,"My Documents BackUp Utility")
  WScript.Quit 'If MsgBox is answered NO
End If

'Main Program ****************************************
Sub StartBackup()
BackupDriveLetter = "Q"   'The Drive Letter for the users subdirectory on the Application Server.
'Check for Backup Drive
If objFSO.DriveExists(BackupDriveLetter) Then 
  Wscript.Echo "You are connected to VPN and your home directory is currently connected"
Else     
  ' NOTE:!!!  YOU MUST CHANGE THE SERVER NAME to the server where the GroupMarketing users home directory resides
  WshNetwork.MapNetworkDrive BackupDriveLetter & ":", "\\SERVER NAME\users\" & strUser
End If
' Check to see if the My Documents Path exhists 
dpath = BackupDriveLetter & ":\My Documents\"
If objFSO.FolderExists(dpath) Then
  Wscript.Echo "The folder exists."
Else
  objFSO.CreateFolder(dpath) 'Create the backupdoc folder
End If

objShell.Run "cmd.exe /c C:\robocopy.exe """ & spath & """ """ & dpath & """ " & s0) 
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top