Windows 命令行和批处理文件 : potential problems with unescaped special characters in filenames?

标签 windows batch-file cmd special-characters filenames

到目前为止,我假设 Windows 不允许所有在命令行中有意义的特殊字符。但是,完全有可能有一个名为“file%sometext%”的文件

如果在批处理脚本或类似命令中调用此文件名,空变量 %sometext% 将被扩展为空,文件名将被解析为"file"。

是否有任何其他合法文件名可能会以这种方式在批处理脚本中引起问题?

最佳答案

Windows NT 是一个运行其他操作系统的操作系统。无论操作系统执行文件操作有什么规则,都是适用的规则。

这些是 Windows 的规则。 Unix 程序将遵循 Unix 的规则。可以在 Unix 中创建无法在 Windows 中打开的文件,反之亦然。

File Name Conventions

Although each file system can have specific rules about the formation of individual components in a directory or file name, all file systems follow the same general conventions: a base file name and an optional extension, separated by a period. For example, the MS-DOS FAT file system supports 8 characters for the base file name and 3 characters for the extension. This is known as an 8.3 file name. The FAT file system and NTFS support file names that can be up to 255 characters long. This is known as a long file name. To get an MS-DOS file name given a long file name, use the GetShortPathName function. To get the full path of a file, use the GetFullPathName function.

Both file systems use the backslash (\) character to separate directory names and the file name when forming a path.

General rules for applications creating names for directories and files or processing names supplied by the user include the following:

Use any character in the current code page for a name, but do not use a path separator, a character in the range 0 through 31, or any character explicitly disallowed by the file system. A name can contain characters in the extended character set (128–255).

Use the backslash (\), the forward slash (/), or both to separate components in a path. No other character is acceptable as a path separator. Note that UNC names must adhere to the following format: \\server\share. Use a period (.) as a directory component in a path to represent the current directory.

Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory. Use a period (.) to separate the base file name from the extension in a directory name or file name.

Do not use the following characters in directory names or file names, because they are reserved:

< > : " / \ |

Do not use device names, such as aux, con, lpt1, and prn, as file names or directory names.

Process a path as a null-terminated string. The maximum length for a path, including a trailing backslash, is given by MAX_PATH. The Unicode versions of several functions permit paths that exceed the MAX_PATH length if the path has the "\\?\" prefix. The "\\?\" tells the function to turn off path parsing. However, each component in the path cannot be more than MAX_PATH characters long. Use the "\\?\" prefix with paths for local storage devices and the "\\?\UNC\" prefix with paths having the Universal Naming Convention (UNC) format. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\bill_g_1\hotstuff\coolapps" is seen as "\\bill_g_1\hotstuff\coolapps".

Do not assume case sensitivity. Consider names such as OSCAR, Oscar, and oscar to be the same.

The following reserved words cannot be used as the name of a file: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also, reserved words followed by an extension—for example, NUL.tx7—are invalid file names.

By following the rules listed in this section, an application can create valid names for files and directories regardless of the file system in use.

Backslashes (\) are used as element dividers in paths (dividing the file name from the path to it, or directories from one another in a path). You cannot use them in file or directory names. They may be required as part of volume names (for example, "C:\").

Platform SDK Release: August 2001

关于Windows 命令行和批处理文件 : potential problems with unescaped special characters in filenames?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082508/

相关文章:

c# - xbox kinect 的 C# 库是否适用于 windows kinect SDK?

windows - 将 OpenBLAS 链接到 MinGW

c++ - 尝试使用 Bonjour 运行演示应用程序,但无法发现在同一主机上运行的服务器

batch-file - 批处理文件,命令的语法不正确

if-statement - 如何在批处理文件中的 "If"中使用多个条件?

windows - 如何在 Windows 上与 NTP 服务器同步时间

java - 在cmd中停止多个maven构建

windows - BAT文件在当前目录下打开CMD

c - 如何从 native C DLL 显示简单的非模式窗口

mysql - 用 10 000 个值填充 MySQL 数据库