Solved

vbscript argument from the command line containing quotes and spaces

Posted on 2011-09-27
6
1,190 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

743 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

13 Experts available now in Live!

Get 1:1 Help Now