我知道/在Linux中是非法的,下面这些在Windows中是非法的 (我认为)*。" / \ []:;|,

我还遗漏了什么?

然而,我需要一份全面的指南,一份考虑到各种因素的指南 双字节字符。链接到外部资源对我来说很好。

我需要首先在文件系统上创建一个目录,其名称可能是 包含禁用字符,所以我计划将这些字符替换为 下划线。然后,我需要将这个目录及其内容写入一个zip文件 (使用Java),因此关于zip目录名称的任何其他建议 不胜感激。


当前回答

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接受了它。

其他回答

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接受了它。

禁止文件名字符的“全面指南”在Windows上不起作用,因为它保留了文件名和字符。是的,像这样的角色 *”?还有一些名字是禁止使用的,但是有无数个名字是只由有效字符组成的,是禁止使用的。例如,空格和点是有效的文件名字符,但仅由这些字符组成的名称是禁止的。

Windows不区分大写字母和小写字母,因此如果已经存在名为a的文件夹,则不能创建名为a的文件夹。更糟糕的是,像PRN和CON这样看似允许的名字,以及许多其他的名字,是被保留和不允许的。Windows也有一些长度限制;在一个文件夹中有效的文件名如果移到另一个文件夹中可能会失效。的规则 命名文件和文件夹 都在微软文档里。

一般来说,不能使用用户生成的文本创建Windows目录名。如果您希望允许用户任意命名,则必须创建安全的名称,如A、AB、A2等,将用户生成的名称及其等效路径存储在应用程序数据文件中,并在应用程序中执行路径映射。

如果您绝对必须允许用户生成文件夹名,那么判断它们是否无效的唯一方法是捕获异常并假定名称无效。即使这样也充满了危险,因为为拒绝访问、脱机驱动器和驱动器空间外抛出的异常与为无效名称抛出的异常重叠。你正在打开一个巨大的伤害罐。

在Windows中创建internet快捷方式时,为了创建文件名,它会跳过非法字符,除了正斜杠被转换为减号。

虽然唯一非法的Unix字符可能是/和NULL,但应该考虑到命令行解释。

例如,虽然在Unix中将文件命名为1>&2或2>&1是合法的,但在命令行中使用这样的文件名可能会被误解。

类似地,也可以将文件命名为$PATH,但当试图从命令行访问它时,shell将把$PATH转换为其变量值。

这对我来说在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中的=等其他遗留内容。