Computer Hope

Microsoft => Microsoft DOS => Topic started by: Visual Basic on March 20, 2011, 04:47:47 PM

Title: Passing Parameters to a batch file
Post by: Visual Basic on March 20, 2011, 04:47:47 PM
Hi all,
I have a master file which calls another batch file like below:

Call "C:\CmdDest\CopyFiles.cmd"

pause

The CopyFiles.cmd calls this:
xcopy "C:\Users\images\*.*" "C:\Program Files\images" /y
pause


xcopy "C:\Pics\*.*" "C:\Program Files\Pics" /y

pause

What I would like to do is instead of hard coding the  C:\Users\SunGard Branding\images\*.*, C:\Program Files\images etc I would like to pass these as Parameters from the Master file to the Child batch file...

Please help me...
Title: Re: Passing Parameters to a batch file
Post by: Dundir on March 20, 2011, 05:22:11 PM
Have you tried:

Call "C:\CmdDest\Copyfiles.Cmd 'C:\Program Files\Images' 'C:\Program Files\Pics' "

Batch parameters are saved to the variable %1,2,3,4,5,6,7,8,9 and for more than 9 you need to use SHIFT depending on OS. (i.e. OS+Command Prompt or MS-DOS)
and

xcopy "C:\Users\images\*.*" %1 /y
pause

xcopy "C:\Pics\*.*" %2 /y
pause
Title: Re: Passing Parameters to a batch file
Post by: Visual Basic on March 20, 2011, 06:58:58 PM
When I do this:


Call "C:\CmdDest\Copyfiles.Cmd 'C:\Program Files\Images' 'C:\Program Files\Pics' "


It says the filename, directory name or volume lable syntax is incorrect

please help
Title: Re: Passing Parameters to a batch file
Post by: Dundir on March 20, 2011, 08:13:26 PM
Call "CopyFiles.CMD" Param1 Param2

CopyFiles.CMD
echo %1,  %2

Output:
Param1

Looks like on my version it only passes the first variable Param1 to %1, don't know why it doesn't pass more than a single parameter.

Update: You Could pass a single string and have CopyFiles.CMD parse that into two variables after the fact with string manipulation but thats more of an advanced topic and I still haven't gotten all the kinks out short of trimming a known part of the string out using set str=%str:~4% to assuming the first four characters need to be trimmed out.
Title: Re: Passing Parameters to a batch file
Post by: oldun on March 21, 2011, 04:31:29 AM
Try:
Call "C:\CmdDest\Copyfiles.Cmd" "C:\Program Files\Images" "C:\Program Files\Pics"