Windows file path with spaces

Windows CMD : Loop FOR with file containing space in his path

I’m trying to retrieve the size of two files to compare the size of eachothers. The problem is, one of the two files contain a space in his path, the code is like that :

The size of the second file is correctly set, but the size of the first file is not retrieved. I’ve been searching an answer for a day now, but can’t find any one (maybe i’m not searching correctly, sorry in this case)

Do you have an idea to get the size of the first file ?

1 Answer 1

Do not use option /F which is for parsing one or more strings read from a file or specified directly or read from captured output of a command executed in a separate command process in background.

Use just command FOR without any option to process the specified file.

Read answer on How to set environment variables with spaces? why it is better to use the syntax set «variable=value» and enclose the entire string including one or more environment variable references in double quotes.

Read next answer on Where does GOTO :EOF return to? The two IF commands are necessary as SET «file1Size=%%

zA» results in SET «file1Size=» on first file not existing which means the environment variable file1Size is not defined respectively deleted after the FOR command line.

The IF condition comparing the two file sizes would result in an exit of batch file processing because of a syntax error if either file1Size or file2Size does not exist on reaching this command line.

Please note that Windows command interpreter can handle only file sizes less than 2 GiB in the IF condition.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

setx setting PATH with spaces

I am trying to update the system Path variable in a win32 shell script but only if a value is not present.

My current version looks something like this:

The problem I am having is with

This doesn’t work, but if I change the quotes to single quote

It does work but the Path ends at the first occurrence of Program Files. I.e abc;def;Program

Is there any way to overcome this?

7 Answers 7

Actually there is an easier way.. it seems to work but it is somewhat ugly and non-intuitive:

Notice that there is ONLY one double-quote and it is at the beginning .. if you put one at the end, it ends up in the path string (without the one at the beginning).

dp0 (with spaces in the .bat path) – Remi Arnaud Mar 15 ’17 at 23:18

I am a bit late but I was looking for something similar and was unable to find it so I figured it out on my own. I was trying to set variable to launch VSC# from Command Line and the exe file for VSC# was present at a path containg space similar to your case. So the solution for me was to set variable using the following:

Single quotes never work in Windows. Period.

Your code does work for me with the double quotes, actually.

Читайте также:  Вернуть настройки звука для windows

I found this question because I was looking for something a little bit different. I need to put quotes into the thing I’m setting. This isn’t exactly what the OP is asking about but may be related if the problem is embedded quotation marks.

I found that this, while «correct», doesn’t work because the user of this variable gets tripped by the space in the path so I needed to put quotes in there.

worked just fine so I thought I’d try the normal way of escaping quotes on Windows:

but this didn’t work, resulting in the error:

So I just opened system settings in the control panel and set the value in the environment variables GUI. Works fine now, I can type set svn at the command line and it shows quoted as I need it to be.

If you really need to set your environment variable programatically you’ll need to look at banging on the registry yourself. This is pretty straight forward in both C and Python (and probably in other languages too). It doesn’t seem as though setx can do it for you though.

Spaces and Parenthesis in windows PATH variable screws up batch files

So, my path variable (System->Adv Settings->Env Vars->System->PATH) is set to:

Although, obviously, without the newlines.

Notice the lines containing PathWithSpaces — the first has no spaces, the second has a space, and the third has a space followed by a parenthesis.

Now, notice the output of this batch file:

or specifically the line:

So, what is this bullshit?

  • Directory in path that is properly escaped with quotes, but with no spaces = fine
  • Directory in path that is properly escaped with quotes, and has spaces but no parenthesis = fine
  • Directory in path that is properly escaped with quotes, and has spaces and has a parenthesis = ERROR

Whats going on here? How can I fix this? I’ll probably resort to a junction point to let my tools still work as workaround, but if you have any insight into this, please let me know 🙂

8 Answers 8

This can happen if there are unescaped parentheses in a line inside a «block» (which also uses parentheses for delimiting).

You can usually fix it by turning on delayed expansion and use variables with !var! instead of %var% . There isn’t much more advice I could give without seeing the code.

Note for Windows users on 64-bit systems

1 = ‘Program Files’ Progra

2 = ‘Program Files(x86)’

There should either (a) not be any quotes in the MS-Windows PATH environmental variable (PATH command) or (b) there should be quotes surrounding the entire expression following the (SET command). Unfortunately, this is not very well documented by MS, although they do state that if quotes are used, they will be included in the value of the variable (Windows XP Command Line Reference).

This can cause problems that are inconsistent and therefore difficult to diagnose. For example if your path includes «C:\Python27», your machine will say «‘python’ is not recognized as an internal or external command, operable program or batch file.» when you try to execute python. However some libraries may still be available.

You do not need to «escape» spaces or parentheses. If you need to escape special characters, then put quotes around the entire expression, including the variable name.

or you can use parentheses too.

Note, double quotes must come in pairs.

However, there probably are not any characters that are valid pathnames, that would cause a problem with the SET command.

The solution they suggest is to use delayed expansion.

Читайте также:  Linux and flight simulators

For setting a path in an if block, rather than using SET PATH= , you should probably use the PATH command.

For other variables, another solution may be to use quotes, but around the whole thing:

Joey in his answer says

This can happen if there are unescaped parentheses in a line inside a «block» (which also uses parentheses for delimiting).

and that’s true. If there are unescaped parentheses one should, well escape them. That’s what I did; I replaced

and this solved the problem.

I’ve experienced something similar. Microsoft explains the issue here: http://support.microsoft.com/kb/329308

Basically, instead of changing the Path variable via System->Adv Settings->Env Vars->System->PATH, try

In Windows 8 I’ve found very little success with any of these methods. Parentheses do not work, quotes work, but the «path» you modify this way isn’t the path that gets used for locating executables, instead cmd still seems to be using the system path it inherited when you opened the window.

example: after determining the processor architecture, I want to add a couple of paths to the PATH environment variable. Actually, even just adding them temporarily would work since I only need them while a batch file is running. But that doesn’t even work.

echo %path% displays the system PATH at the time the cmd was launched.

set path=»%path%;%programfiles(x86)%\company\program\subdir» works but now %path% contains everything surrounded by quotes, and if I try to run a program in subdir from somewhere else, it fails. Using parentheses around the whole thing instead of quotes does not work.

Another thing I’ve noticed is that the same command will work if entered interactively in cmd , but not if encountered in a batch file. That’s frightening. Yet another oddity is the intermittent loss of the last character of an environment variable’s value! Another inconsistency is with third party programs: some can handle a %var% as a parameter, others don’t.

How do I specify C:\Program Files without a space in it for programs that can’t handle spaces in file paths?

A configuration file needs position of another file,

but that file is located in «C:\Program Files»,

and the path with space in it is not recognized,

Is there another way to specify the location without space in it?

14 Answers 14

you should be able to use

  • «c:\Program Files» (note the quotes)
  • c:\PROGRA

1 (the short name notation)

Try c:\> dir /x (in dos shell)

This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.

1″ means the same as «c:\Program Files»? – omg May 21 ’09 at 12:01

2 for «C:\Program Files» – Dirk Vollmar May 21 ’09 at 12:05

Never hardcode this location. Use the environment variables %ProgramFiles% or %ProgramFiles(x86)% .

When specifying these, always quote because Microsoft may have put spaces or other special characters in them.

In addition, the directory might be expressed in a language you do not know. http://www.samlogic.net/articles/program-files-folder-different-languages.htm

Use these commands to find the values on a machine. DO NOT hardcode them into a program or .bat or .cmd file script. Use the variable.

Use the following notations:

    For «C:\Program Files«, use «C:\PROGRA

1»
For «C:\Program Files (x86)«, use «C:\PROGRA

Thanks @lit for your ideal answer in below comment:

Use the environment variables %ProgramFiles% and %ProgramFiles(x86)%

2 respectively. If someone has installed windows onto a system that already had a folder called, for example, «C:\Programmers», then that would take up the short name «C:\PROGRA

1″ so «C:\Program Files» would be «C:\PROGRA

2″, etc. – Eureka Dec 27 ’18 at 13:30

I think the reason those suggesting using the C:\PROGRA

1 name have received downvotes is because those names are seen as a legacy feature of Windows best forgotten, which may also be unstable, at least between different installations, although probably not on the same machine.

Also, as someone pointed out in a comment to another answer, Windows can be configured not to have the 8.3 legacy names in the filesystem at all.

The Windows shell (assuming you’re using CMD.exe) uses %ProgramFiles% to point to the Program Files folder, no matter where it is. Since the default Windows file opener accounts for environment variables like this, if the program was well-written, it should support this.

Also, it could be worth using relative addresses. If the program you’re using is installed correctly, it should already be in the Program Files folder, so you could just refer to the configuration file as .\config_file.txt if its in the same directory as the program, or ..\other_program\config_file.txt if its in a directory different than the other program. This would apply not only on Windows but on almost every modern operating system, and will work properly if you have the «Start In» box properly set, or you run it directly from its folder.

There should be a way to use the full c:\program files path directly. Often, it involves encapulating the string in quotes. For instance, on the windows command line;

will not start Internet Explorer, but

Sometimes you can quote the filename.

Some programs will tolerate the quotes. Since you didn’t provide any specific program, it’s impossible to tell if quotes will work for you.

Using a command line with spaces in path name

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

Читайте также:  Linux mint как выбрать ядро при загрузке
Оцените статью