Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use spaces in bat script, without quotes

Tags:

batch-file

I need to run

start cmd.exe /k "C:\Program Files (x86)\Mozilla Firefox\sdk\bin\activate & cd d:\home\name\addon\ & cfx run"

The problem is that first path has spaces (Program Files). I can't use quotes, because they are used for full command already.

How can I use spaces in this command?

like image 463
Qiao Avatar asked Jan 06 '13 15:01

Qiao


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" .

What is @echo off in Bat?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

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.

How do I escape special characters in CMD?

The Windows command-line interpreter uses a caret character ( ^ ) to escape reserved characters that have special meanings (in particular: & , | , ( , ) , < , > , ^ ).


2 Answers

Who says you can't add quotes around the exe path when the /C command is already quoted? It works fine ! :-)

start cmd.exe /k ""C:\Program Files (x86)\Mozilla Firefox\sdk\bin\activate" & cd d:\home\name\addon\ & cfx run"

The outer quotes around the /C command are removed prior to execution, leaving only the quotes around your exe path. You can read about how CMD process quotes in the help. Simply type CMD /? or HELP CMD from a command prompt. It does get confusing.

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

Your command has more than 2 quotes, so option 2 is followed.

The only time the above won't work is if your exe name contains a special character like &, for example this&that.exe. That causes a problem because the exe name is not quoted when the START command is initially parsed. That can be fixed by escaping the problem character within the file name.

start cmd.exe /k ""this^&that.exe" & echo something else"
like image 161
dbenham Avatar answered Sep 30 '22 16:09

dbenham


You can try with the old DOS-writing style, where every path-segment has maximal 8 chars like:

C:\Progra~1\Mozill~1\

the ~1 is from the alphabetical order:

C:\DisIsMyDirAlpha
C:\DisIsMyDirBeta
C:\DisIsMyDirGamma

are these:

C:\DisIsM~1
C:\DisIsM~2
C:\DisIsM~3
like image 45
fdiv_bug Avatar answered Sep 30 '22 16:09

fdiv_bug