Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass Command Line Parameters with space in Batch File

I need to pass parameter line AB CD to a batch file from the command line. This parameter refer to a file name.

If I use use AB CD the script just pick the first part and return Unable to find the file AB.txt

If I put quote around my parameters like "AB CD" the I got

"AB CD".txt 
Illegal characters in path.
like image 355
magdmartin Avatar asked Sep 05 '13 14:09

magdmartin


People also ask

How do you handle spaces in a batch file?

When you send arguments, those with poison or space characters need to be doublequoted. Inside your batch file, if you no longer need the surrounding doublequotes, you'd remove them by using %~5 instead of %5 . Additionally the recommended syntax for the set command is Set "VariableName=VariableValue" .

How do you pass a path with spaces in CMD?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


2 Answers

you can use %~1 instead of %1

e.g a test.bat with :

echo %~1 

then a call to test "abc de" will display :

abc de 
like image 171
Mali Avatar answered Oct 12 '22 22:10

Mali


what you can do is

>batch.bat "ab cd.txt"

When the parameters contain whitespace, you can enclose them in quotes.

like image 41
Murtuza Kabul Avatar answered Oct 13 '22 00:10

Murtuza Kabul