如何清理用作文件名的字符串?
我有一个例程可以将文件转换为不同的格式并保存它。 原始数据文件已编号,但我的例程根据原始数据中找到的内部名称为输出提供了一个文件名。
我尝试在整个目录上批量运行它,它工作得很好,直到我遇到一个内部名称中带有斜杠的文件。 哎呀! 如果它在这里这样做,它就可以轻松地在其他文件上这样做。 是否有 RTL(或 WinAPI)例程可以清理字符串并删除无效符号,以便可以安全地用作文件名?
I've got a routine that converts a file into a different format and saves it. The original datafiles were numbered, but my routine gives the output a filename based on an internal name found in the original.
I tried to batch-run it on a whole directory, and it worked fine until I hit one file whose internal name had a slash in it. Oops! And if it does that here, it could easily do it on other files. Is there an RTL (or WinAPI) routine somewhere that will sanitize a string and remove invalid symbols so it's safe to use as a filename?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 PathGetCharType 函数,PathCleanupSpec 函数 或以下技巧:
此代码将字符串分为几部分并使用MoveFile来验证每个部分。 对于无效字符或保留文件名(如“COM”),MoveFile 将失败;对于有效文件名,MoveFile 将返回成功或 ERROR_ALREADY_EXISTS。
PathCleanupSpec 位于 Win32API/JwaShlObj.pas 下的 Jedi Windows API 中
You can use PathGetCharType function, PathCleanupSpec function or the following trick:
This code divides string into parts and uses MoveFile to verify each part. MoveFile will fail for invalid characters or reserved file names (like 'COM') and return success or ERROR_ALREADY_EXISTS for valid file name.
PathCleanupSpec is in the Jedi Windows API under Win32API/JwaShlObj.pas
关于是否有 API 函数来清理文件名称(甚至检查其有效性)的问题 - 似乎没有。 引用 PathSearchAndQualify()函数:
因此,您只能从 文件名、路径、和命名空间 (Windows):
使用当前代码页中的几乎所有字符作为名称,包括 Unicode 字符和扩展字符集中的字符 (128–255),但以下字符除外:
< > : " / \ | ? *
请勿使用以下保留设备名称作为名称文件:
CON
、PRN
、AUX
、NUL
、COM1..COM9
,LPT1..LPT9
.还要避免这些名称后紧跟着扩展名; 例如,不建议使用
NUL.txt
。如果您知道您的程序只会写入 NTFS 文件系统,您可能可以确定没有文件系统不允许的其他字符,因此您只需检查文件名是否太长(使用MAX_PATH 常量)在所有无效字符被删除(或例如用下划线替换)之后。
程序还应确保文件名清理不会导致文件名冲突,并且它会默默地覆盖最终具有相同名称的其他文件。
Regarding the question whether there is any API function to sanitize a file a name (or even check for its validity) - there seems to be none. Quoting from the comment on the PathSearchAndQualify() function:
So you can only consult the rules for file name validity from File Names, Paths, and Namespaces (Windows):
Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
< > : " / \ | ? *
Do not use the following reserved device names for the name of a file:
CON
,PRN
,AUX
,NUL
,COM1..COM9
,LPT1..LPT9
.Also avoid these names followed immediately by an extension; for example,
NUL.txt
is not recommended.If you know that your program will only ever write to NTFS file systems you can probably be sure that there are no other characters that the file system does not allow, so you would only have to check that the file name is not too long (use the
MAX_PATH
constant) after all invalid chars have been removed (or replaced by underscores, for example).A program should also make sure that the file name sanitizing has not lead to file name conflicts and it silently overwrites other files which ended up with the same name.
对于其他阅读本文并想要使用 PathCleanupSpec 的人,我编写了这个测试例程,它似乎有效......网络上肯定缺乏示例。
您需要包含ShlObj.pas(不确定何时添加PathCleanupSpec,但我在Delphi 2010中对此进行了测试)
您还需要检查 XP sp2 或更高版本
For anyone else reading this and wanting to use PathCleanupSpec, I wrote this test routine which seems to work... there is a definate lack of examples on the 'net.
You need to include ShlObj.pas (not sure when PathCleanupSpec was added but I tested this in Delphi 2010)
You will also need to check for XP sp2 or higher
嗯,最简单的事情就是使用正则表达式和您最喜欢的语言版本的
gsub
来替换任何不是“单词字符”的内容。 在大多数具有类似 Perl 正则表达式的语言中,此字符类为“\w
”,否则作为简单选项为“[A-Za-z0-9]
”。特别是,与其他答案中的一些示例相比,您不想查找要删除的无效字符,而是查找要保留的有效字符。 如果您正在寻找无效的字符,那么您总是很容易受到新字符引入的影响,但如果您只寻找有效的字符,那么您的效率可能会稍微低一些(因为您替换了一个实际上并不存在的字符)需要),但至少你永远不会错。
现在,如果您想让新版本尽可能与旧版本一样,您可以考虑替换。 您可以替换一个或多个您认为可以的字符,而不是删除。 但这样做是一个足够有趣的问题,它可能是另一个问题的好主题。
Well, the easy thing is to use a regex and your favourite language's version of
gsub
to replace anything that's not a "word character." This character class would be "\w
" in most languages with Perl-like regexes, or "[A-Za-z0-9]
" as a simple option otherwise.Particularly, in contrast to some of the examples in other answers, you don't want to look for invalid characters to remove, but look for valid characters to keep. If you're looking for invalid characters, you're always vulnerable to the introduction of new characters, but if you're looking for only valid ones, you might be slightly less inefficient (in that you replaced a character you didn't really need to), but at least you'll never be wrong.
Now, if you want to make the new version as much like the old as possible, you might consider replacement. Instead of deleting, you can substitute a character or characters you know to be ok. But doing that is an interesting enough problem that it's probably a good topic for another question.
在现代 delphi 上尝试一下:
我还允许在文件名中包含德语变音符号或其他字符,例如 -、_、..。
Try this on a modern delphi:
I allows also to have german umlauts or other chars like -, _,.. in a filename.
使用此功能。 对我来说工作得很好
取回一级目录名称...
目的是使用 shelobj
use this function. work fine for me
the purpose is to get back ONE level of directory name
uses shelobj...