windows 如何为无法处理文件路径中的空格的程序指定没有空格的 C:\Program Files?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/892555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:23:21  来源:igfitidea点击:

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

windows

提问by omg

A configuration file needs position of another file,

一个配置文件需要另一个文件的位置,

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

但该文件位于“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?

有没有另一种方法来指定没有空间的位置?

回答by Boris Guéry

you should be able to use

你应该可以使用

  • "c:\Program Files"(note the quotes)
  • c:\PROGRA~1(the short name notation)
  • “c:\Program Files”(注意引号)
  • c:\PROGRA~1(简称表示法)

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

尝试 c:\> dir /x (在 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.

这将显示为非 8dot3 文件名生成的短名称。格式为/N,在长名称之前插入短名称。如果不存在短名称,则在其位置显示空白。

回答by lit

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

永远不要硬编码这个位置。使用环境变量%ProgramFiles%%ProgramFiles(x86)%.

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

指定这些时,请始终引用,因为 Microsoft 可能在其中放置了空格或其他特殊字符。

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

此外,目录可能以您不知道的语言表示。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.

使用这些命令来查找机器上的值。不要将它们硬编码到程序或 .bat 或 .cmd 文件脚本中。使用变量。

set | findstr /R "^Program"
set | findstr /R "^Common"

回答by MrDEV

Use the following notations:

使用以下符号:

  • For "C:\Program Files", use "C:\PROGRA~1"
  • For "C:\Program Files (x86)", use "C:\PROGRA~2"
  • 对于“ C:\Program Files”,使用“ C:\PROGRA~1
  • 对于“ C:\Program Files (x86)”,使用“ C:\PROGRA~2

Thanks @lit for your ideal answer in below comment:

感谢@lit 在下面的评论中为您提供理想的答案:

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

使用环境变量%ProgramFiles%%ProgramFiles(x86)%

:

回答by harms

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.

我认为那些建议使用 C:\PROGRA~1 名称的人收到反对票的原因是因为这些名称被视为 Windows 的遗留功能,最好被遗忘,它也可能不稳定,至少在不同的安装之间,尽管可能不在同一台机器。

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.

此外,正如有人在对另一个答案的评论中指出的那样,Windows 可以配置为在文件系统中根本没有 8.3 旧名称。

回答by Andrew Scagnelli

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.

Windows shell(假设您使用的是 CMD.exe)使用 %ProgramFiles% 指向 Program Files 文件夹,无论它在哪里。由于默认的 Windows 文件打开器会考虑这样的环境变量,如果程序编写得很好,它应该支持这一点。

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.

此外,可能值得使用相对地址。如果您使用的程序安装正确,它应该已经在 Program Files 文件夹中,因此您可以将配置文件称为 .\config_file.txt 如果它与程序位于同一目录中,或者 ..\ other_program\config_file.txt 如果它位于与其他程序不同的目录中。这不仅适用于 Windows,而且适用于几乎所有现代操作系统,如果您正确设置了“开始于”框,或者直接从其文件夹运行它,它将正常工作。

回答by Steve Cooper

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

应该有办法c:\program files直接使用完整路径。通常,它涉及将字符串封装在引号中。例如,在 windows 命令行上;

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

will not start Internet Explorer, but

不会启动 Internet Explorer,但

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

will.

将要。

回答by vobject

You could try to use:

你可以尝试使用:

C:\PROGRA~1

回答by S.Lott

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.

有些程序会容忍引号。由于您没有提供任何特定程序,因此无法判断引号是否适合您。

回答by Jacob Schoen

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

尝试用引号将路径括起来。即“C:\Program Files\Appname\config.file”

回答by dye

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:

对于无法处理文件路径中的空格的程序,您可以使用以下方法指定 C:\Program Files 中没有空格:

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