Space in cmd windows

Space in cmd windows

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

Hello, I asked the following question in the Microsoft Answers community and was told to come and ask here. Hopefully someone here knows?

I want to write a simple batch file to keep log file output from a program I’m running. Unfortunately I’ve not done this in years and I’m rustier than I thought.

Basically, the program CW creates a file called «log.txt»

What I’d like to do is to delete a file called log10.txt, and the iteratively rename log9,txt to log 10.txt, log 8.txt to log9,txt. and so forth.

Once this renaming is done, I’d like to execute CW.txt.

A few complications. There is both a 32-bit and 64-bit version of the program and only one may exist.

So I thought I could use the system environment variable %PROCESSOR_ARCHITECTURE% to test if I’m running 64-bit.

Then I could use %ProgramFiles% or %ProgramFiles(x86)% to locate my executable.

Sounds easy in theory, but I’m stumbling — a lot.

I’m using cmd to open a window to test my batch script and the batch script is in a file called «testme.cmd.» I don’t know if it would make a difference if the batch file was «testme.bat»?

The full directory name for the 64-bit version of the program is:

C:\Program Files\KingComp\Cram Wiz 3\CW.exe

Trying to run a batch file to determine if the file exist:

if exist %ProgramFiles%\KingComp\Cram Wiz 3\CW.exe

causes an error: The system cannot find the path specified. I’ve tried varous combinations of quotation marks around parts or the whole of the command — all with no success. Searching yields a lot of results, but none seem to apply to my problem, except suggestions to determine the 8.3 filename and to use that. But I don’t know how to programmatically find the 8.3 directory structures.

Is there a way to use the Windows 7 environment variables and a path name that both will contain spaces and have a positive result?

I’m also not sure exactly what command processor or shell I’m using. All I know it’s not DOS anymore, Dorothy. 🙂

How do I use spaces in the Command Prompt?

How can I use spaces in the Windows Command Line?

11 Answers 11

Single quotation marks won’t do in that case. You have to add quotation marks around each path and also enclose the whole command in quotation marks:

I just figured out that for a case where the path involves the use of white space characters, for example, when I need to access the app xyz which location is :

To run this from windows cmd prompt, you need to use

If double quotes do not solve the issue then try e.g.

to get a list of alternative file or directory names. Example output:

Now use the short 8 character file or folder name in the 5th column, e.g. PROGRA

1.XML, in your commands. For instance:

Enclose the paths containing spaces with double quotes.

I prefer to enclose the command in () which is valid batch which makes it a bit easier to read:

Try to provide complex pathnames in double-quotes (and include file extensions at the end for files.)

CMD interprets text with double quotes («xyz») as one string and text within single quotes (‘xyz’) as a command. For example:

FOR %%A in (‘dir /b /s *.txt’) do (echo «%%A»)

And one good thing, cmd is not* case sensitive like bash. So «New fiLE.txt» and «new file.TXT» is alike to it.

Читайте также:  Драйвер для amd radeon hd 7870 windows 10

*Note: The %%A variables in above case is case-sensitive (%%A not equal to %%a).

this worked for me in a batch file

Just add Quotation Mark

Example:«C:\Users\User Name»

Hope it got Solved!

You should try using quotes.

Spaces in the Commend Prompt (in a VBA Shell command code line)

I had a very similar problem which ended up being a space in the command prompt when automating via VBA to get the contents from the command window into a text file. This Thread was one of many I caught along the way that didn’t quite get me the solution.

So this may help others with a similar problem: Since the syntax with quotes is always difficult to get right , I think showing some specific examples is always useful. The additional problem you get using the command prompt in VBA via the Shell thing, is that the code line often won’t error when something goes wrong: in fact a blink of the black commend window misleads into thinking something was done.

As example… say I have a Folder, with a text file in it like at

The space there in the folder name gives the problem.

Something like this would work, assuming the Folder, AlansFolder, exists

This won’t work. (It won’t error).

Including quote pairs around the path will make it work

( By the way, if the text file does not exist, then it will be made).

With the benefit of hindsight, we can see that my solution does tie up approximately with some already given..

Converting that code line to a manual given command we would have

That seems to work

This works also

This final form also works and ties up with the solution from sacra ….” You have to add quotation marks around each path and also enclose the whole command in quotation marks “ …..

Free space in a CMD shell

Is there a way to get the amount of free diskspace of a disk or a folder in a CMD without having to install some thirdparty applications?

I have a CMD that copies a big file to a given directory and could of course use the errorlevel return from the copy command, but then I have to wait for the time it takes to copy the file (eg. to that then the disk is full and the copy operation fails).

I would like to know before I start the copy if it is any idea at all. Tried the DU.EXE utility from Sysinternals, but that show occupied space only.

9 Answers 9

If you run » dir c:\ «, the last line will give you the free disk space.

Edit: Better solution: » fsutil volume diskfree c: «

A possible solution:

a more «advanced solution», for Windows Xp and beyond:

The Windows Management Instrumentation Command-line (WMIC) tool (Wmic.exe) can gather vast amounts of information about about a Windows Server 2003 as well as Windows XP or Vista. The tool accesses the underlying hardware by using Windows Management Instrumentation (WMI). Not for Windows 2000.

  • WMIC can see policy based restrictions as well. (even if ‘ dir ‘ will still do the job),
  • ‘ dir ‘ is locale dependent.

0,2%»^) Get FreeSpace /VALUE’) DO @SET freespace=%%S – Davor Josipovic Sep 15 ’13 at 9:32

Using this command you can find all partitions, size & free space: wmic logicaldisk get size, freespace, caption

You can avoid the commas by using /-C on the DIR command.

If you want to compare the available space to the space needed, you could do something like the following. I specified the number with thousands separator, then removed them. It is difficult to grasp the number without commas. The SET /A is nice, but it stops working with large numbers.

The following script will give you free bytes on the drive:

Читайте также:  Windows 10 экранная клавиатура как переключить язык

Note that this depends on the output of your dir command, which needs the last line containing the free space of the format 24 Dir(s) 34,071,691,264 bytes free . Specifically:

  • it must be the last line (or you can modify the for loop to detect the line explicitly rather than relying on setting bytesfree for every line).
  • the free space must be the third «word» (or you can change the tokens= bit to get a different word).
  • thousands separators are the , character (or you can change the substitution from comma to something else).

It doesn’t pollute your environment namespace, setting only the bytesfree variable on exit. If your dir output is different (eg, different locale or language settings), you will need to adjust the script.

How do I escape spaces in command line in Windows without using quotation marks?

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

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

but this command DOES NOT work :

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

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

1 )? – Justin Pearce May 4 ’11 at 15:11

7 Answers 7

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

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.

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

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

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:

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

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

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:

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:

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:

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:

which produces foo» «bar , or:

which produces «foo bar» , or:

which produces «foo «bar , or:

which produces foo» bar» , or:

which produces «foo b»ar , or:

which produces fo»o bar» , or:

which produces fo»o ba»r , or:

which produces «fo»o» bar» , or:

Читайте также:  Will virtualbox run on windows 10

which produces «f»»o»»o»» «»b»»a»»r» , or even:

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

Pass path with spaces as parameter to bat file

I have a simple bat script that copies files from an known directory to a directory given by the user. How can I pass the path (it might contain spaces) to my script and use it with the xcopy command?

In my code i have the following

If I simply call the script I get the following error:

8 Answers 8

Interesting one. I love collecting quotes about quotes handling in cmd/command.

Your particular scripts gets fixed by using %1 instead of «%1» .

By adding an ‘echo on’ ( or getting rid of an echo off ), you could have easily found that out.

1, as stated by @zikaS’ answer. – Diego Queiroz Apr 2 ’18 at 18:01

1 alone removes surrounding quotes. However since you can’t know whether the input parameter %1 has quotes or not, you should ensure by «%

1″ that they are added for sure. This is especially helpful when concatenating variables, e.g. convert.exe «%

1″ in the batch files. But I guess that’s what you meant. – Paul Groke Jun 11 ’12 at 19:12

I think the OP’s problem was that he wants to do BOTH of the following:

  • Pass a parameter which may contain spaces
  • Test whether the parameter is missing

As several posters have mentioned, to pass a parameter containing spaces, you must surround the actual parameter value with double quotes.

To test whether a parameter is missing, the method I always learned was:

However, if the actual parameter is quoted (as it must be if the value contains spaces), this becomes

which causes the «unexpected» error. If you instead use

then the error no longer occurs for quoted values. But in that case, the test no longer works when the value is missing — it becomes

To fix this, use any other characters (except ones with special meaning to DOS) instead of quotes in the test:

1″ would remove exterior quotations and allow for a real testing, because you never know what the user will type, like this or «this». – Jay Sep 11 ’09 at 12:05

1″ will work most of the time. However there are a few things you need to note for this:

  1. It’s not supported in Windows NT 4.0. You need Windows 2000 or later. (If you are coding a script compatible with old OS, beware.)
  2. It does not make parameters secure and sanitized. My observation is that there is no way to properly sanitize the command line arguments in CMD scripts.

To demonstrate second point, let me give an example:

Run with example.cmd dummy^&DIR . The ampersand is escaped here ( ^& ) to prevent shell from interpreting as a command delimiter, so that it becomes part of the argument passed to the script. The DIR is interpreted as a command inside the sub-shell running the script, where it shouldn’t.

Quoting it might work some time, but still insecure:

example2.cmd foo^»^&DIR^&^»bar will break it. DIR command will be run two times, one right after SET and the other right after the first ECHO. You see that the «%

1″ you think is quoted well gets unquoted by the argument itself.

So, no way to make parsing arguments secure.

Оцените статью