27

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.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
magdmartin
  • 1,712
  • 3
  • 20
  • 43

3 Answers3

43

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
Mali
  • 2,990
  • 18
  • 18
  • that what I found from this question: http://stackoverflow.com/questions/473117/pass-path-with-spaces-as-parameter-to-bat-file – magdmartin Sep 05 '13 at 15:13
13

what you can do is

>batch.bat "ab cd.txt"

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

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • two things, I can not add the extension in my parameters as it is use somewhere in my batch script else without the file extension. Second when I add quote, there are added in the file name (see last example of my questions) – magdmartin Sep 05 '13 at 15:03
  • Well, in that case, I guess the issue is not with passing parameters, it is with the programming in batch file. You can check this by creating a simple batch file with this simple line echo %1, this will echo the full name "ab cd". It means that you are passing the parameters correctly but there is somewhere in the batch file it is not treated properly. Somewhere, it is trying to recreate the file name with breaking it apart and adding extension. – Murtuza Kabul Sep 05 '13 at 15:06
  • agree. I actually need to use %~1 instead of %1 in my script – magdmartin Sep 05 '13 at 15:11
  • 1
    You should either use %~p1 for path or %~n1 for file name, not just %~1 – Murtuza Kabul Sep 05 '13 at 15:16
  • 1
    or a combination of these – Murtuza Kabul Sep 05 '13 at 15:16
2

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
uingtea
  • 6,002
  • 2
  • 26
  • 40