Solved

vbscript argument from the command line containing quotes and spaces

Posted on 2011-09-27
6
1,288 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 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