105

For example what is the alternative to this command without quotation marks:

CD "c:\Documents and Settings"

The full reason I don't want to use quotation marks is that this command DOES work:

   SVN add mypathname\*.*

but this command DOES NOT work :

   SVN add "mypathname\*.*"

The problem being when I change mypathname for a path with spaces in it I need to quote the whole thing. For example:

SVN add "c:\Documents and Settings\username\svn\*.*"

But when I try this I get the following error message:

svn: warning: 'c:\Documents and Settings\username\svn\*.*' not found
studiohack
  • 13,468
  • 19
  • 88
  • 118
David
  • 1,323
  • 3
  • 11
  • 12
  • 2
    you meant you have a good reason for 'not' wanting to use quotation marks. – Thomas May 04 '11 at 12:56
  • Why, is your quotation mark key broken? :) – slhck May 04 '11 at 12:57
  • @Thomas well spotted! – David May 04 '11 at 13:04
  • 3
    @slhck Don't worry my shift key and number 2 key are perfectly ok! I'm using a command that doesn't appear to allow wildcards when they apear inside quotation marks thats all! – David May 04 '11 at 13:06
  • have you tried SVN add "c:\Documents and Settings\username\svn\"*.* ? – KutscheraIT May 04 '11 at 13:45
  • Have you tried `SVN add "c:\Documents and Settings\username\svn\"*.*` or `SVN add "c:\Documents and Settings\username\svn"\*.*` ? – Mark Booth May 04 '11 at 13:48
  • 1
    @MarkBooth and @WeltenWanderer nice try but unfortunately this doesn't work. In all 3 cases I get an "svn: Error resolving case of ....." – David May 04 '11 at 14:00
  • Trying to work around the problem, you could use junctions (http://en.wikipedia.org/wiki/NTFS_junction_point) to create paths without spaces in them. – Mark Booth May 04 '11 at 14:13
  • 1
    Have you tried old 8.3 formatting of the name ( DOCUME~1 )? – Justin Pearce May 04 '11 at 15:11
  • Just to be pedantic, you don't need quotes with `cd`, even when there are spaces in the path. Couldn't you just use `cd` and then make a temporary %variable% from it, and use that? – paradroid May 04 '11 at 17:35

8 Answers8

67

It almost all works for me, but have you perhaps tried line5.. escaping the space with a caret symbol (^)

1 C:\Documents and Settings\user>cd ..

2 C:\Documents and Settings>cd ..

3 C:\>cd Documents and Settings

4 C:\Documents and Settings>cd..

5 C:\>cd Documents^ and^ Settings

6 C:\Documents and Settings>cd..

7 C:\>cd C:\documents and settings

8 C:\Documents and Settings>cd..

9 C:\>

Or e.g. below where the caret really makes all the difference.

Looks from below like the caret symbol may be your answer, see line 3 below.

1 C:\>"c:\Documents and Settings\a.bat"
gaga

2 C:\>c:\Documents and Settings\a.bat
'c:\Documents' is not recognized as an internal or external command,
operable program or batch file.

3 C:\>c:\Documents^ and^ Settings\a.bat
gaga

C:\>
barlop
  • 23,380
  • 43
  • 145
  • 225
  • since subversion is a programmer's tool, it may be appropriate for SO. here is a related question http://stackoverflow.com/questions/757435/how-to-escape-characters-in-subversion-managed-file-names it could be you use quotes or caret to get it to svn, then perhaps you use backslash before each space – barlop May 05 '11 at 08:31
  • you could also try cygwin's svn, if curious.. with some of the example answers in that question and see if any or variations of them work for you. – barlop May 05 '11 at 08:47
  • 5
    The caret symbol does directly answer my question so I'll accept your answer. Unfortunately the SVN command doesn't seem to recognise it! If I try it I get this error message "svn 'C:' is not a working copy". I have another way around it though and that's to simply 'cd' to the directory first then execute the SVN command once there. I can even chain the two commands together in a single line using the && operator: cd "c:\Documents and Settings\username\svn" && SVN add \*.* – David May 05 '11 at 08:55
  • in the CD example, caret isn't necessary, CD doesn't need space escaped – barlop Feb 07 '14 at 02:53
  • in the `C:\>c:\...` example(where I didn't use CD), caret did make a difference. – barlop Feb 10 '16 at 18:06
  • `^` (sadly) doesn't work with `copy` –  May 12 '17 at 13:10
  • 2
    @Chinggis6 interesting.. it doesn't work for DIR either. Both copy and dir are built in commands.. though so is CD. Though copy and dir take a filename as parameter. May be worth asking on dostips forum – barlop May 14 '17 at 05:28
  • Thanks for suggesting the carret ^. You cannot imagine how many options I tried. I use ssh from powershell on Windows, to run a command on another Windows Server. I tried escaping the double quotes " to make sure that the string was passed entirely. "", \" , """, ``", all the combinations were tried. Then I tried escaping the space with %20, with all type of comibinations of quotes... Your's is the solution. Here's the command: `ssh user@machine "C:\Program^ Files\MATLAB\R2021b\toolbox\parallel\bin\mjs -help"` "Reviving an old thread". Well, after 2 hours of newer solutions... – Lionel Trebuchon Jan 12 '23 at 18:39
44

I found that putting quotes around just one part of the location works. In your case:

SVN add C:\"Documents and Settings"\username\svn\*.*

Although this uses quotation marks, the important thing to notice is that the asterisks are outside the quotation marks, so they still function correctly.

Kit Johnson
  • 1,038
  • 2
  • 13
  • 24
  • 9
    also if one wants to be minimalistic, one can do quotes around just the space or just the chars containing the space, e.g. C:\>cd documents" and se"ttings (though re that cd example , one doesn't need to escape spaces) – barlop Jul 30 '13 at 11:28
  • 1
    Thanks a lot. I might have upvoted 4 or 5 times if possible.. – Abdul Saleem Jan 12 '15 at 20:25
  • @Sayka The question did ask "How do I escape spaces in command line in Windows **without using quotation marks?**" – barlop Apr 14 '18 at 08:13
  • 2
    @barlop Sure, but the reason for no quotation marks was that the asterisk doesn't do its job when it's inside the quotation marks. The solution I've given avoids this issue because the asterisks are outside the quotation marks. This answer doesn't abide by the **letter** of the question, but it abides by the **spirit** of the question (i.e., it works for the intended purpose). – Kit Johnson Apr 17 '18 at 05:46
31

Despite the answers giving the illusion that it works, the fact is you can't sneak in spaces into usual cmd arguments. This is easy to prove:

  1. Save "echo %1" as test.bat. This batch file will output the first argument which cmd passes us.

  2. Now, try and run test.bat, setting the value of %1 to foo bar. (Note that there's a space char between foo and bar.)

  3. Trial-and-error for a few years and realize that there's no way to do it. Folks will suggest to escape using ^, yet test.bat foo^ bar will not output foo bar.

So, there's no way to get the output foo bar, and the closest we can get is running test.bat foo" "bar which produces foo" "bar, or running test.bat "foo bar" which produces "foo bar".


Now, the reason the other answers appear to work is because cd does it's own additional parsing, diverging from the behavior of usual argument passing (the usual %1, %2, %3 and etc in typical batch files).

For example, consider the peculiar command:

cd c:\documents and settings \some folder with spaces

Why does it work? This is due to cd itself doing something equivalent of joining the 7 usual arguments into one logical one. According to cmd argument passing norms, we see 7 arguments:

  1. c:\documents
  2. and
  3. settings
  4. \some
  5. folder
  6. with
  7. spaces

It's as though cd has joined all the 7 arguments into one logical one, doing something akin to array.join(" "), which produces the path:

c:\documents and settings \some folder with spaces

Note that this behavior is peculiar to cd only (and some other functions). It has nothing to do with usual argument passing.


Indeed, cd has another peculiarity. Remember we stated above that we couldn't get the output foo bar? The closest output we can get is by running:

test.bat foo" "bar

which produces foo" "bar, or:

test.bat "foo bar"

which produces "foo bar", or:

test.bat "foo "bar

which produces "foo "bar, or:

test.bat foo" bar"

which produces foo" bar", or:

test.bat "foo b"ar

which produces "foo b"ar, or:

test.bat fo"o bar"

which produces fo"o bar", or:

test.bat fo"o ba"r

which produces fo"o ba"r, or:

test.bat "fo"o" bar"

which produces "fo"o" bar", or:

test.bat "f""o""o"" ""b""a""r":

which produces "f""o""o"" ""b""a""r", or even:

test.bat """"f"""o""""o"" ""ba"""r"""""""""":

which produces """"f"""o""""o"" ""ba"""r"""""""""".

All the above examples have one similarity, which is they'll produce foo bar after we trim off the " chars. cd's author must have realized this too... if we were to infer from cd's peculiar behavior which trims off all " it receives, allowing all of these commands to work:

  • cd c:\documents and settings

  • cd "c:\documents and settings"

  • cd "c:"\"documents and settings"

  • cd c:\"documents" "and" "settings"

  • cd c:\"docu"ments an"d set"tings"

  • cd c:"\"docu"ments an"d set"ti"""ngs

  • cd "c"":""\"docu"ments an"d set"ti"""ngs

  • cd "c"":""\"do""cu"me"nts a"n""d set"ti"""ngs

  • cd c"""":""""\"""d"""oc""""u"me"""""nt"s a"n""d set"""""""ti""""ngs

Pacerier
  • 26,733
  • 82
  • 197
  • 273
9

The short-filename appears to work like a breeze.

"E:\Progra~1\Java\Eclipse\eclipse.exe" -vmargs -Xms1024m -Xmx2048m

For boosting up the memory.. ;)

Milorulez
  • 91
  • 1
  • 1
3

For those talking about 8.3 replacement, please note the following two things:

  • 8.3 can be set to disabled (or enabled) on NTFS, so it may not always exist.
  • The 8.3 name of a file/folder can change without notice just by creating, renaming, or deleting other things in the same folder.

So c:\docume~1 can point to:

  • Nowhere (be invalid)
  • The folder you want
  • Another folder
  • A variable folder along time

It is not safe, unless you get the short name and use it in atomic operations.

Although it is very uncommon that they change, it is common that they do not exist.

If you want to know if 8.3 exists for a particular folder/file, test it with parameter /X on a dir command, or encapsulate it on a for to only get that part, etc.

For more about how to enable/disable 8.3 on NTFS, see this Microsoft support article:
https://support.microsoft.com/en-us/help/121007/how-to-disable-8-3-file-name-creation-on-ntfs-partitions

Pang
  • 937
  • 1
  • 9
  • 12
Claudio
  • 31
  • 1
2

Use the 8.3 short-filename. For example:

If exist c:\docume~1 goto :Ifoundthesucker
Synetech
  • 68,243
  • 36
  • 223
  • 356
0

Following up on @Pang's post, and others discussing short names and the pitfalls, here's way to dynamically resolve a long name to a short name:

for %A in ("C:\Some\long path\which must exist\to be resolved") do @echo %~sA

Then, obviously you can use the short name rather than surrounding quotes.

BuvinJ
  • 271
  • 3
  • 7
-1

You can also try

c:\'Documents and Settings'\username\'some space here'\svn\*.*

i.e. surround all folder names that contain spaces with single quotes (').

Kjara
  • 99