[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vbscript argument from the command line containing quotes and spaces

Posted on 2011-09-27
6
Medium Priority
?
1,432 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

834 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