Solved

vbscript argument from the command line containing quotes and spaces

Posted on 2011-09-27
6
1,214 Views
Last Modified: 2012-05-12
I have created a vbscript which runs the robocopy command and requires a command line argument containing the folders to exclude from the robocopy job to it however at present its not working.

If I was running robocopy from the command line I would type in the folders to exclude in the format shown below.

robocopy /XD "Program Files" "Windows" "Recycler"

The problem I have is passing the "program files" "windows" "recycler" bit all as one single argument/variable.

My knowledge of vbscript is very limited and I have spent hours searching the web for answer without any success. Any help is very much appreciated.

Thanks in advnace.
0
Comment
Question by:kackland
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:jawa29
ID: 36709040
Hi

Normally when running command line arguments from within a VBS you just need to double up the quote marks.

For Example

Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "robocopy /XD ""Program Files"" ""Windows"" ""Recycler""",,True

Jawa29
0
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 36709513
If I understand correctly you are running a VBscript and you put in parameters on the command line for the script.  Then you want to pass all those parameters to the line that calls robocopy.  Is this correct?

Since I don't have your full script, I will just post a piece to get the parameters.  If you want to post your full script, we can try and help you modify if needed.


To handled parameters try some like this:
Option Explicit
Dim i, strParameter

' Loop through all parameters and store in one variable with qoutes
For i = 0 to WScript.Arguments.Count - 1
	strParameter = strParameter & """" & WScript.Arguments(i) & """ "
Next
' Remove last space
strParameter = left(strParameter, len(strParameter) - 1)

msgbox "Parameter=" & strParameter

Open in new window


This loops through all parameters passed to the vbscript and puts them in the strParameter variable with quote marks around each variable.  Please let us know if you need additional help.

-Bear
0
 

Author Comment

by:kackland
ID: 36710070
Thank-you both for your quick responses. Here is the code.

Set objShell=WScript.CreateObject("WScript.Shell")
'(Do you even need FSO?) Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")

' Determining the Day
strDayNumber = Weekday(date())
strDayName = WeekdayName(strDayNumber)

' Defining the Backup Type
If strDayName = Friday Then
	strBackupType = "_Full"
Else
	strBackupType = "_Incremental"
End If

' Defining the variables and arguments
strSource = Wscript.Arguments(0)
strTargetPath = Wscript.Arguments(1)
strTargetFullPath = strTargetPath & strDayName & strBackupType
strFolderExclusions = Chr(34) & "Backup Logs" & Chr(34) & " " & Wscript.Arguments(2)
strLogFile = strSource & "Backup Logs\" & strDayName & "_" & strBackupType & ".log"
strFullParms = " /M /S /E /V /NP /ZB /XD " & strFolderExclusions & " /R:3 /W:10 /LOG:" & Chr(34) & strLogFile & Chr(34) ' Remove the archive attribute after copying all files.
strIncrementalParms = " /A /S /E /V /NP /ZB /XD " & strFolderExclusions & " /R:3 /W:10 /LOG:" & Chr(34) & strLogFile & Chr(34) ' Only copies files with archive attribute set.

' Full and incremental robocopy commands
If DayName = Friday Then
	Set fso = CreateObject("Scripting.FileSystemObject") 
	If fso.FolderExists(strTargetFullPath) Then 
		fso.DeleteFolder strTargetFullPath
	End If
	' objShell.Run "robocopy " & strSource & " " & strTargetFullPath & strFullParms
	MsgBox "robocopy " & Chr(34) & strSource & Chr(34) & " " & Chr(34) & strTargetFullPath & Chr(34) & strFullParms
Else
	If fso.FolderExists(strTargetFullPath) Then 
		fso.DeleteFolder strTargetFullPath
	End If
	' objShell.Run "robocopy " & strSource & " " & strTargetFullPath & strIncrementalParms
	MsgBox "robocopy " & Chr(34) & strSource & Chr(34) & " " & Chr(34) & strTargetFullPath & Chr(34) & strIncrementalParms
End If

Open in new window

0
The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 250 total points
ID: 36712043
Try this and let us know how it works:
Dim i, strFolderExclusions

Set objShell=WScript.CreateObject("WScript.Shell")
'(Do you even need FSO?) Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")

' Determining the Day
strDayNumber = Weekday(date())
strDayName = WeekdayName(strDayNumber)

' Defining the Backup Type
If strDayName = Friday Then
	strBackupType = "_Full"
Else
	strBackupType = "_Incremental"
End If

' Defining the variables and arguments
strSource = Wscript.Arguments(0)
strTargetPath = Wscript.Arguments(1)
strTargetFullPath = strTargetPath & strDayName & strBackupType
strFolderExclusions = Chr(34) & "Backup Logs" & Chr(34) & " " 

' Loop through all parameters and store in one variable with qoutes
For i = 2 to WScript.Arguments.Count - 1
	strFolderExclusions = strFolderExclusions & """" & WScript.Arguments(i) & """ "
Next
' Remove last space
strFolderExclusions = left(strFolderExclusions, len(strFolderExclusions) - 1)

strLogFile = strSource & "Backup Logs\" & strDayName & "_" & strBackupType & ".log"
strFullParms = " /M /S /E /V /NP /ZB /XD " & strFolderExclusions & " /R:3 /W:10 /LOG:" & Chr(34) & strLogFile & Chr(34) ' Remove the archive attribute after copying all files.
strIncrementalParms = " /A /S /E /V /NP /ZB /XD " & strFolderExclusions & " /R:3 /W:10 /LOG:" & Chr(34) & strLogFile & Chr(34) ' Only copies files with archive attribute set.

' Full and incremental robocopy commands
If DayName = Friday Then
	Set fso = CreateObject("Scripting.FileSystemObject") 
	If fso.FolderExists(strTargetFullPath) Then 
		fso.DeleteFolder strTargetFullPath
	End If
	' objShell.Run "robocopy " & strSource & " " & strTargetFullPath & strFullParms
	MsgBox "robocopy " & Chr(34) & strSource & Chr(34) & " " & Chr(34) & strTargetFullPath & Chr(34) & strFullParms
Else
	If fso.FolderExists(strTargetFullPath) Then 
		fso.DeleteFolder strTargetFullPath
	End If
	' objShell.Run "robocopy " & strSource & " " & strTargetFullPath & strIncrementalParms
	MsgBox "robocopy " & Chr(34) & strSource & Chr(34) & " " & Chr(34) & strTargetFullPath & Chr(34) & strIncrementalParms
End If

Open in new window

0
 

Author Comment

by:kackland
ID: 36716346
Excellent that sorted it perfectly.
Massive thank you to ltlbearand3!!! Points awarded.
0
 

Author Closing Comment

by:kackland
ID: 36716351
Brilliant!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now