magdmartin
magdmartin

Reputation: 1787

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.

Upvotes: 28

Views: 95717

Answers (3)

uingtea
uingtea

Reputation: 6554

if it only accept 1 paramenter use "%*" you don't need to quote the parameter

myapp.cmd

@echo off
echo "%*"

test it

myapp single parameter with space

Upvotes: 2

Mali
Mali

Reputation: 2700

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

Upvotes: 44

Murtuza Kabul
Murtuza Kabul

Reputation: 6524

what you can do is

>batch.bat "ab cd.txt"

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

Upvotes: 13

Related Questions