95

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?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
omg
  • 136,412
  • 142
  • 288
  • 348

14 Answers14

146

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.

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
  • 7
    can you explain why "c:\PROGRA~1" means the same as "c:\Program Files"? – omg May 21 '09 at 12:01
  • 2
    Can you be sure that the short name does never change, which would be important for a configuration file? – mghie May 21 '09 at 12:03
  • 1
    It's a Windows short file name (http://www.chami.com/tips/windows/122496W.html). It's an 8.3 filename for backward compatibility. – Dirk Vollmar May 21 '09 at 12:03
  • The "C:\PROGRA~1" is the way the Windows converts long names, i.e. names greater than the old MS-DOS 8.3 names. You can use the command DIR /X at the command prompt to see the shor name versions of your folders and files. – Paulo Santos May 21 '09 at 12:04
  • 3
    The short name might be different from your example, e.g. if you also have a folder named C:\ProgramFiles (without space) or alike you might get C:\PROGRA~2 for "C:\Program Files" – Dirk Vollmar May 21 '09 at 12:05
  • 1
    It's the same because ultimately Windows still uses the old 8.3 character name format. "Program Files" is an alias of "PROGRA~1". If you had another directory called "Progress Storage" that would have the short name "PROGRA~2" – ChrisF May 21 '09 at 12:05
  • Compatibility with old systems / applications that use the 8.3 filename from the days of MS DOS etc. PROGRA~1 is the first instance of a folder with the first 6 letters "Progra". If you had another folder in the same place called "Program Metadata" - that would be PROGRA~2 – Amadiere May 21 '09 at 12:05
  • 6
    @ChrisF: If only I could downvote your comment, I would. Where do you get the idea that "Windows still uses the old 8.3 character name format"? On the contrary, file systems can be configured to not use it at all. – mghie May 21 '09 at 12:06
  • 1
    It's really unlikely to move, because these folders are the first ones to be created when windows is installed. However, in the case of windows being installed to a HD that already has a folder c:\programmable, this might be problematic – spender May 21 '09 at 12:07
  • 4
    @mghie - all right maybe "use" was too strong a term. The format still available and Windows recognises it so in that sense it "uses" it. I would like to think that if you took it away Windows would still work, but I have the sneaking suspicion that there's still some old code that doesn't recognise long names lurking in the depths of the operating system. – ChrisF May 21 '09 at 12:12
  • Thank you for the tip. I was going crazy as I didn't know why it said the location didn't exist for me. What helped me was putting quotes around it. – Jonathan J. Pecany Jun 12 '20 at 20:41
  • @mghie I tried the following today under Windows 10, and it still "works": https://ein-eike.de/2014/11/25/dreckstool-windows-dateisystem/ Parts of Windows use 8.3 file names by default. – Eike Sep 28 '21 at 12:08
  • @Eike: Netter Artikel. However, as I wrote some 12 years ago, this behavior can (and probably should) be disabled. Run "fsutil behavior set disable8dot3 1" in an elevated prompt, restart your system and rerun your test. – mghie Sep 28 '21 at 17:35
49

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.

"%ProgramFiles%\theapp\app.exe"
"%ProgramFiles(x86)%\theapp\app.exe"

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

>set|findstr /i /r ".*program.*="
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files

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.

set | findstr /R "^Program"
set | findstr /R "^Common"
lit
  • 14,456
  • 10
  • 65
  • 119
31

Use the following notations:

  • For "C:\Program Files", use "C:\PROGRA~1"
  • For "C:\Program Files (x86)", use "C:\PROGRA~2"

Thanks @lit for your ideal answer in below comment:

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

:

MrDEV
  • 3,490
  • 1
  • 19
  • 20
  • 6
    Avoid doing this because there is no *guarantee* that they will have been abbreviated to ~1 and ~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. – ProfDFrancis Dec 27 '18 at 13:30
  • 2
    Yes, maybe, but it will work in most of cases. I think the ideal answer would be the highlighted one above, which was answered by lit. Thanks for your inputs anyways. – MrDEV Dec 27 '18 at 14:28
  • 1
    Thanks for the solution. – Blackvault Apr 09 '20 at 14:32
7

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.

harms
  • 8,728
  • 4
  • 30
  • 27
6

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;

c:\program files\Internet Explorer\iexplore.exe 

will not start Internet Explorer, but

"c:\program files\Internet Explorer\iexplore.exe" 

will.

Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
6

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.

Andrew Scagnelli
  • 1,584
  • 4
  • 18
  • 26
  • Using the variables is the correct way. Just because the executable is in a directory will not cause that directory to be the current working directory. The executable will have to do something before a relative directory can be used. – lit Aug 28 '17 at 16:51
2

No.

Sometimes you can quote the filename.

"C:\Program Files\Something"

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
2

I think that the other posts have answered the question, but just some interesting for your information (from the command prompt):

dir c:\ /ad /x

This will provide a listing of only directories and also provide their "Short names".

Richard
  • 142
  • 2
1

As an alternative to the other answers, you can try symbolic links.

Create the symbolic link first and install the application based on the link. (Depending on the case, this may be way easier to do, for instance when the application has n mentions of the target folder throughout its code)

A symbolic link will create something similar to a shortcut to a folder, but seen as an actual folder by other applications.

This is how you do it:

  • Run cmd as administrator
  • User this command: mklink /D "C:\LinkToProgramFiles" "C:\Program Files"

And then, you start using "C:\LinkToProgramFiles" in the applications that can't handle spaces. (This link can be seen in Windows Explorer as a folder with the symbol of a shortcut)


Be very careful not to create circular links if you start playing too much with this.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • There should be no need to go to links, symbolic or otherwise. It is unlikely that this would resolve the underlying problem. – lit Apr 20 '17 at 13:51
  • It DOES solve my problems with python, scipy and theano installations. In fact it was definitely the easiest and most safe option (not to say the only). Since the question states nothing about the program, it can indeed be a suitable answer. – Daniel Möller Apr 20 '17 at 17:34
  • I am glad you have a solution for your problems. I hope that `omg` is not trying to use a poorly written program that does not handle space and other special characters in pathnames. I am sorry you did, but I never had problems with Python. – lit Apr 20 '17 at 17:58
1

You can use the following methods to specify C:\Program Files without a space in it for programs that can't handle spaces in file paths:

'Path to Continuum Reports Subdirectory - Note use DOS equivalent (no spaces)
RepPath = "c:\progra~1\continuum_reports\" or
RepPath = C:\Program Files\Continuum_Reports  'si es para 64 bits.

' Path to Continuum Reports Subdirectory - Note use DOS equivalent (no spaces)
RepPath = "c:\progra~2\continuum_reports\" 'or
RepPath = C:\Program Files (x86)\Continuum_Reports  'si es para 32 bits.
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
dye
  • 27
  • 1
1

You could try to use:

C:\PROGRA~1
vobject
  • 5,290
  • 3
  • 29
  • 21
0

Try surrounding the path in quotes. i.e "C:\Program Files\Appname\config.file"

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
-3

You can just create a folder ProgramFiles at local D or local C to install those apps that can be install to a folder name which has a SPACES / Characters on it.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 5
    This is, to be honest, a very bad and silly answer. Creating a new directory for one file/application? And what if he cannot control where the file is located?? – Visual Vincent Mar 12 '16 at 16:10
-3

Either use the generated short name (C:\Progra~1) or surround the path with quotation marks.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 1
    The "shortname" might well be incorrect or not even supported on the file system. This answer should be deleted. – lit Jun 16 '18 at 13:58
  • @lit I merely suggest to use the generated short name, not that is should be hard coded. It may be that the short name mechanism is not available on all platforms where .NET runs these days, but keep in mind that the answer is nine years old. The answer that you provided is a good one, but you could really work on your attitude towards others. That said, this answer provides no value here, so it will eventually be deleted. – Fredrik Mörk Jun 18 '18 at 14:18