我知道/在Linux中是非法的,下面这些在Windows中是非法的 (我认为)*。" / \ []:;|,
我还遗漏了什么?
然而,我需要一份全面的指南,一份考虑到各种因素的指南 双字节字符。链接到外部资源对我来说很好。
我需要首先在文件系统上创建一个目录,其名称可能是 包含禁用字符,所以我计划将这些字符替换为 下划线。然后,我需要将这个目录及其内容写入一个zip文件 (使用Java),因此关于zip目录名称的任何其他建议 不胜感激。
我知道/在Linux中是非法的,下面这些在Windows中是非法的 (我认为)*。" / \ []:;|,
我还遗漏了什么?
然而,我需要一份全面的指南,一份考虑到各种因素的指南 双字节字符。链接到外部资源对我来说很好。
我需要首先在文件系统上创建一个目录,其名称可能是 包含禁用字符,所以我计划将这些字符替换为 下划线。然后,我需要将这个目录及其内容写入一个zip文件 (使用Java),因此关于zip目录名称的任何其他建议 不胜感激。
当前回答
下面是一个基于Christopher Oezbek的答案的windows c#实现
containsFolder布尔值使它更加复杂,但希望涵盖所有内容
/// <summary>
/// This will replace invalid chars with underscores, there are also some reserved words that it adds underscore to
/// </summary>
/// <remarks>
/// https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
/// </remarks>
/// <param name="containsFolder">Pass in true if filename represents a folder\file (passing true will allow slash)</param>
public static string EscapeFilename_Windows(string filename, bool containsFolder = false)
{
StringBuilder builder = new StringBuilder(filename.Length + 12);
int index = 0;
// Allow colon if it's part of the drive letter
if (containsFolder)
{
Match match = Regex.Match(filename, @"^\s*[A-Z]:\\", RegexOptions.IgnoreCase);
if (match.Success)
{
builder.Append(match.Value);
index = match.Length;
}
}
// Character substitutions
for (int cntr = index; cntr < filename.Length; cntr++)
{
char c = filename[cntr];
switch (c)
{
case '\u0000':
case '\u0001':
case '\u0002':
case '\u0003':
case '\u0004':
case '\u0005':
case '\u0006':
case '\u0007':
case '\u0008':
case '\u0009':
case '\u000A':
case '\u000B':
case '\u000C':
case '\u000D':
case '\u000E':
case '\u000F':
case '\u0010':
case '\u0011':
case '\u0012':
case '\u0013':
case '\u0014':
case '\u0015':
case '\u0016':
case '\u0017':
case '\u0018':
case '\u0019':
case '\u001A':
case '\u001B':
case '\u001C':
case '\u001D':
case '\u001E':
case '\u001F':
case '<':
case '>':
case ':':
case '"':
case '/':
case '|':
case '?':
case '*':
builder.Append('_');
break;
case '\\':
builder.Append(containsFolder ? c : '_');
break;
default:
builder.Append(c);
break;
}
}
string built = builder.ToString();
if (built == "")
{
return "_";
}
if (built.EndsWith(" ") || built.EndsWith("."))
{
built = built.Substring(0, built.Length - 1) + "_";
}
// These are reserved names, in either the folder or file name, but they are fine if following a dot
// CON, PRN, AUX, NUL, COM0 .. COM9, LPT0 .. LPT9
builder = new StringBuilder(built.Length + 12);
index = 0;
foreach (Match match in Regex.Matches(built, @"(^|\\)\s*(?<bad>CON|PRN|AUX|NUL|COM\d|LPT\d)\s*(\.|\\|$)", RegexOptions.IgnoreCase))
{
Group group = match.Groups["bad"];
if (group.Index > index)
{
builder.Append(built.Substring(index, match.Index - index + 1));
}
builder.Append(group.Value);
builder.Append("_"); // putting an underscore after this keyword is enough to make it acceptable
index = group.Index + group.Length;
}
if (index == 0)
{
return built;
}
if (index < built.Length - 1)
{
builder.Append(built.Substring(index));
}
return builder.ToString();
}
其他回答
这对我来说在Python中已经足够好了:
def fix_filename(name, max_length=255):
"""
Replace invalid characters on Linux/Windows/MacOS with underscores.
List from https://stackoverflow.com/a/31976060/819417
Trailing spaces & periods are ignored on Windows.
>>> fix_filename(" COM1 ")
'_ COM1 _'
>>> fix_filename("COM10")
'COM10'
>>> fix_filename("COM1,")
'COM1,'
>>> fix_filename("COM1.txt")
'_.txt'
>>> all('_' == fix_filename(chr(i)) for i in list(range(32)))
True
"""
return re.sub(r'[/\\:|<>"?*\0-\x1f]|^(AUX|COM[1-9]|CON|LPT[1-9]|NUL|PRN)(?![^.])|^\s|[\s.]$', "_", name[:max_length], flags=re.IGNORECASE)
还可以查看这个过时的列表,以获得FAT32中的=等其他遗留内容。
我也有同样的需求,正在寻找推荐信或标准推荐信,偶然发现了这条线索。我目前在文件和目录名中应该避免的字符黑名单是:
$CharactersInvalidForFileName = {
"pound" -> "#",
"left angle bracket" -> "<",
"dollar sign" -> "$",
"plus sign" -> "+",
"percent" -> "%",
"right angle bracket" -> ">",
"exclamation point" -> "!",
"backtick" -> "`",
"ampersand" -> "&",
"asterisk" -> "*",
"single quotes" -> "“",
"pipe" -> "|",
"left bracket" -> "{",
"question mark" -> "?",
"double quotes" -> "”",
"equal sign" -> "=",
"right bracket" -> "}",
"forward slash" -> "/",
"colon" -> ":",
"back slash" -> "\\",
"lank spaces" -> "b",
"at sign" -> "@"
};
I always assumed that banned characters in Windows filenames meant that all exotic characters would also be outlawed. The inability to use ?, / and : in particular irked me. One day I discovered that it was virtually only those chars which were banned. Other Unicode characters may be used. So the nearest Unicode characters to the banned ones I could find were identified and MS Word macros were made for them as Alt+?, Alt+: etc. Now I form the filename in Word, using the substitute chars, and copy it to the Windows filename. So far I have had no problems.
下面是替换字符(Alt +十进制Unicode):
⃰ ⇔ Alt8432 ⁄ ⇔ Alt8260 ⃥ ⇔ Alt8421 ∣ ⇔ Alt8739 ⵦ ⇔ Alt11622 ⮚ ⇔ Alt11162 ‽ ⇔ Alt8253 ፡ ⇔ Alt4961 ‵‵ ⇔ Alt8246 “ ⇔ Alt8243
作为测试,我用所有这些字符组成了一个文件名,Windows接受了它。
在Unix shell中,您几乎可以用单引号引用每个字符。除了单引号本身之外,您不能表示控制字符,因为\没有展开。从带引号的字符串中访问单引号本身是可能的,因为您可以用单引号和双引号连接字符串,如'I' ' ' ' 'm',它可以用于访问名为"I'm"的文件(这里也可以使用双引号)。
因此应该避免所有控制字符,因为它们很难在shell中输入。其余部分仍然很有趣,特别是以破折号开头的文件,因为大多数命令将这些文件视为选项,除非您在前面有两个破折号,或者您使用./指定它们,这也隐藏了开头的-。
如果你想要更好,不要使用shell和典型命令使用的任何字符作为语法元素,有时依赖于位置,例如,你仍然可以使用-,但不能作为第一个字符;与.相同,只有当你想要使用它(“隐藏文件”)时,你才能将它作为第一个字符。如果您是恶意的,您的文件名是VT100转义序列;-),因此ls会使输出乱码。
. net框架系统。IO对于无效的文件系统字符提供如下功能:
路径。GetInvalidFileNameChars 路径。GetInvalidPathChars
这些函数应该根据. net运行时所在的平台返回适当的结果。也就是说,这些函数的文档页中的备注说:
方法返回的数组不保证包含 文件和目录中无效的完整字符集 的名字。完整的无效字符集可能因文件系统而异。