Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vbscript argument from the command line containing quotes and spaces

Posted on 2011-09-27
6
Medium Priority
?
1,396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
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…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

618 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