如何清理字符串以用作文件名?

时间:2021-11-16 06:48:10

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?

我试图在一个完整的目录上批量运行它,它工作正常,直到我点击一个内部名称中有斜杠的文件。哎呀!如果它在这里这样做,它可以很容易地在其他文件上。是否存在RTL(或WinAPI)例程,它将清理字符串并删除无效符号,以便可以安全地用作文件名?

9 个解决方案

#1


21  

You can use PathGetCharType function, PathCleanupSpec function or the following trick:

您可以使用PathGetCharType函数,PathCleanupSpec函数或以下技巧:

  function IsValidFilePath(const FileName: String): Boolean;
  var
    S: String;
    I: Integer;
  begin
    Result := False;
    S := FileName;
    repeat
      I := LastDelimiter('\/', S);
      MoveFile(nil, PChar(S));
      if (GetLastError = ERROR_ALREADY_EXISTS) or
         (
           (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES)
           and
           (GetLastError=ERROR_INVALID_NAME)
         ) then
        Exit;
      if I>0 then
        S := Copy(S,1,I-1);
    until I = 0;
    Result := True;
  end;

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.

此代码将字符串分成几部分,并使用MoveFile验证每个部分。 MoveFile将因无效字符或保留文件名(如“COM”)而失败,并返回成功或ERROR_ALREADY_EXISTS作为有效文件名。


PathCleanupSpec is in the Jedi Windows API under Win32API/JwaShlObj.pas

PathCleanupSpec位于Win32API / JwaShlObj.pas下的Jedi Windows API中

#2


10  

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:

关于是否有任何API函数来清理文件名称(甚至检查其有效性)的问题 - 似乎没有。引用PathSearchAndQualify()函数的注释:

There does not appear to be any Windows API that will validate a path entered by the user; this is left as an an ad hoc exercise for each application.

似乎没有任何Windows API可以验证用户输入的路径;这是每个应用程序的临时练习。

So you can only consult the rules for file name validity from File Names, Paths, and Namespaces (Windows):

因此,您只能从文件名,路径和命名空间(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:

    使用当前代码页中的几乎任何字符作为名称,包括扩展字符集(128-255)中的Unicode字符和字符,但以下情况除外:

    • The following reserved characters are not allowed:
      < > : " / \ | ? *
    • 不允许使用以下保留字符:<>:“/ \ |?*
    • Characters whose integer representations are in the range from zero through 31 are not allowed.
    • 不允许整数表示在0到31范围内的字符。
    • Any other character that the target file system does not allow.
    • 目标文件系统不允许的任何其他字符。
  • 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.

    不要将以下保留的设备名称用于文件名:CON,PRN,AUX,NUL,COM1..COM9,LPT1..LPT9。同时避免使用这些名称,然后立即进行扩展;例如,不建议使用NUL.txt。

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

如果您知道您的程序只会写入NTFS文件系统,您可能可以确定文件系统不允许其他字符,因此您只需要检查文件名是否太长(使用在删除所有无效字符(或者用下划线替换)之后,MAX_PATH常量)。

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.

程序还应确保文件名清理不会导致文件名冲突,并且它会以静默方式覆盖最终使用相同名称的其他文件。

#3


7  

{
  CleanFileName
  ---------------------------------------------------------------------------

  Given an input string strip any chars that would result
  in an invalid file name.  This should just be passed the
  filename not the entire path because the slashes will be
  stripped.  The function ensures that the resulting string
  does not hae multiple spaces together and does not start
  or end with a space.  If the entire string is removed the
  result would not be a valid file name so an error is raised.

}

function CleanFileName(const InputString: string): string;
var
  i: integer;
  ResultWithSpaces: string;
begin

  ResultWithSpaces := InputString;

  for i := 1 to Length(ResultWithSpaces) do
  begin
    // These chars are invalid in file names.
    case ResultWithSpaces[i] of 
      '/', '\', ':', '*', '?', '"', '<', '>', '|', ' ', #$D, #$A, #9:
        // Use a * to indicate a duplicate space so we can remove
        // them at the end.
        {$WARNINGS OFF} // W1047 Unsafe code 'String index to var param'
        if (i > 1) and
          ((ResultWithSpaces[i - 1] = ' ') or (ResultWithSpaces[i - 1] = '*')) then
          ResultWithSpaces[i] := '*'
        else
          ResultWithSpaces[i] := ' ';

        {$WARNINGS ON}
    end;
  end;

  // A * indicates duplicate spaces.  Remove them.
  result := ReplaceStr(ResultWithSpaces, '*', '');

  // Also trim any leading or trailing spaces
  result := Trim(Result);

  if result = '' then
  begin
    raise(Exception.Create('Resulting FileName was empty Input string was: '
      + InputString));
  end;
end;

#4


5  

Check if string has invalid chars; solution from here:

检查字符串是否包含无效字符;解决方案来自:

//test if a "fileName" is a valid Windows file name
//Delphi >= 2005 version

function IsValidFileName(const fileName : string) : boolean;
const 
  InvalidCharacters : set of char = ['\', '/', ':', '*', '?', '"', '<', '>', '|'];
var
  c : char;
begin
  result := fileName <> '';

  if result then
  begin
    for c in fileName do
    begin
      result := NOT (c in InvalidCharacters) ;
      if NOT result then break;
    end;
  end;
end; (* IsValidFileName *)

And, for strings returning False, you could do something simple like this for each invalid character:

并且,对于返回False的字符串,您可以为每个无效字符执行类似这样的简单操作:

var
  before, after : string;

begin
  before := 'i am a rogue file/name';

  after  := StringReplace(before, '/', '',
                      [rfReplaceAll, rfIgnoreCase]);
  ShowMessage('Before = '+before);
  ShowMessage('After  = '+after);
end;

// Before = i am a rogue file/name
// After  = i am a rogue filename

#5


4  

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

对于读这篇文章并希望使用PathCleanupSpec的其他人,我写了这个测试例程似乎有用......在'网上有一个确定的缺乏例子。你需要包含ShlObj.pas(不确定何时添加了PathCleanupSpec但是我在Delphi 2010中测试了这个)你还需要检查XP sp2或更高版本

procedure TMainForm.btnTestClick(Sender: TObject);
var
  Path: array [0..MAX_PATH - 1] of WideChar;
  Filename: array[0..MAX_PATH - 1] of WideChar;
  ReturnValue: integer;
  DebugString: string;

begin
  StringToWideChar('a*dodgy%\filename.$&^abc',FileName, MAX_PATH);
  StringToWideChar('C:\',Path, MAX_PATH);
  ReturnValue:= PathCleanupSpec(Path,Filename);
  DebugString:= ('Cleaned up filename:'+Filename+#13+#10);
  if (ReturnValue and $80000000)=$80000000 then
    DebugString:= DebugString+'Fatal result. The cleaned path is not a valid file name'+#13+#10;
  if (ReturnValue and $00000001)=$00000001 then
    DebugString:= DebugString+'Replaced one or more invalid characters'+#13+#10;
  if (ReturnValue and $00000002)=$00000002 then
    DebugString:= DebugString+'Removed one or more invalid characters'+#13+#10;
  if (ReturnValue and $00000004)=$00000004 then
    DebugString:= DebugString+'The returned path is truncated'+#13+#10;
  if (ReturnValue and $00000008)=$00000008 then
    DebugString:= DebugString+'The input path specified at pszDir is too long to allow the formation of a valid file name from pszSpec'+#13;
  ShowMessage(DebugString);
end;

#6


3  

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.

好吧,最简单的方法是使用正则表达式和您喜欢的语言版本的gsub来替换任何不是“单词字符”的东西。这个字符类在大多数语言中都是“\ w”,使用类似Perl的正则表达式,或者“[A-Za-z0-9]”作为一个简单的选项。

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.

现在,如果您想使新版本尽可能地与旧版本一样,您可以考虑更换。您可以替换一个或多个您知道的字符,而不是删除。但这样做是一个非常有趣的问题,它可能是另一个问题的好主题。

#7


1  

I did this:

我这样做了:

// Initialized elsewhere...
string folder;
string name;
var prepl = System.IO.Path.GetInvalidPathChars();
var frepl = System.IO.Path.GetInvalidFileNameChars();
foreach (var c in prepl)
{
    folder = folder.Replace(c,'_');
    name = name.Replace(c, '_');
}
foreach (var c in frepl)
{
    folder = folder.Replace(c, '_');
    name = name.Replace(c, '_');
}

#8


0  

Try this on a modern delphi:

在现代的delphi上试试这个:

 use System.IOUtils;
 ...
 result := TPath.HasValidFileNameChars(FileName, False)

I allows also to have german umlauts or other chars like -, _,.. in a filename.

我也允许在文件名中使用德语变音符号或其他字符,如_,_,..

#9


0  

// for all platforms (Windows\Unix), uses IOUtils.
function ReplaceInvalidFileNameChars(const aFileName: string; const aReplaceWith: Char = '_'): string;
var
  i: integer;
begin
  Result := aFileName;
  for i := Low(Result) to High(Result) do
    if not TPath.IsValidFileNameChar(Result[i]) then
      Result[i] := aReplaceWith;
  end;
end.

#1


21  

You can use PathGetCharType function, PathCleanupSpec function or the following trick:

您可以使用PathGetCharType函数,PathCleanupSpec函数或以下技巧:

  function IsValidFilePath(const FileName: String): Boolean;
  var
    S: String;
    I: Integer;
  begin
    Result := False;
    S := FileName;
    repeat
      I := LastDelimiter('\/', S);
      MoveFile(nil, PChar(S));
      if (GetLastError = ERROR_ALREADY_EXISTS) or
         (
           (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES)
           and
           (GetLastError=ERROR_INVALID_NAME)
         ) then
        Exit;
      if I>0 then
        S := Copy(S,1,I-1);
    until I = 0;
    Result := True;
  end;

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.

此代码将字符串分成几部分,并使用MoveFile验证每个部分。 MoveFile将因无效字符或保留文件名(如“COM”)而失败,并返回成功或ERROR_ALREADY_EXISTS作为有效文件名。


PathCleanupSpec is in the Jedi Windows API under Win32API/JwaShlObj.pas

PathCleanupSpec位于Win32API / JwaShlObj.pas下的Jedi Windows API中

#2


10  

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:

关于是否有任何API函数来清理文件名称(甚至检查其有效性)的问题 - 似乎没有。引用PathSearchAndQualify()函数的注释:

There does not appear to be any Windows API that will validate a path entered by the user; this is left as an an ad hoc exercise for each application.

似乎没有任何Windows API可以验证用户输入的路径;这是每个应用程序的临时练习。

So you can only consult the rules for file name validity from File Names, Paths, and Namespaces (Windows):

因此,您只能从文件名,路径和命名空间(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:

    使用当前代码页中的几乎任何字符作为名称,包括扩展字符集(128-255)中的Unicode字符和字符,但以下情况除外:

    • The following reserved characters are not allowed:
      < > : " / \ | ? *
    • 不允许使用以下保留字符:<>:“/ \ |?*
    • Characters whose integer representations are in the range from zero through 31 are not allowed.
    • 不允许整数表示在0到31范围内的字符。
    • Any other character that the target file system does not allow.
    • 目标文件系统不允许的任何其他字符。
  • 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.

    不要将以下保留的设备名称用于文件名:CON,PRN,AUX,NUL,COM1..COM9,LPT1..LPT9。同时避免使用这些名称,然后立即进行扩展;例如,不建议使用NUL.txt。

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

如果您知道您的程序只会写入NTFS文件系统,您可能可以确定文件系统不允许其他字符,因此您只需要检查文件名是否太长(使用在删除所有无效字符(或者用下划线替换)之后,MAX_PATH常量)。

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.

程序还应确保文件名清理不会导致文件名冲突,并且它会以静默方式覆盖最终使用相同名称的其他文件。

#3


7  

{
  CleanFileName
  ---------------------------------------------------------------------------

  Given an input string strip any chars that would result
  in an invalid file name.  This should just be passed the
  filename not the entire path because the slashes will be
  stripped.  The function ensures that the resulting string
  does not hae multiple spaces together and does not start
  or end with a space.  If the entire string is removed the
  result would not be a valid file name so an error is raised.

}

function CleanFileName(const InputString: string): string;
var
  i: integer;
  ResultWithSpaces: string;
begin

  ResultWithSpaces := InputString;

  for i := 1 to Length(ResultWithSpaces) do
  begin
    // These chars are invalid in file names.
    case ResultWithSpaces[i] of 
      '/', '\', ':', '*', '?', '"', '<', '>', '|', ' ', #$D, #$A, #9:
        // Use a * to indicate a duplicate space so we can remove
        // them at the end.
        {$WARNINGS OFF} // W1047 Unsafe code 'String index to var param'
        if (i > 1) and
          ((ResultWithSpaces[i - 1] = ' ') or (ResultWithSpaces[i - 1] = '*')) then
          ResultWithSpaces[i] := '*'
        else
          ResultWithSpaces[i] := ' ';

        {$WARNINGS ON}
    end;
  end;

  // A * indicates duplicate spaces.  Remove them.
  result := ReplaceStr(ResultWithSpaces, '*', '');

  // Also trim any leading or trailing spaces
  result := Trim(Result);

  if result = '' then
  begin
    raise(Exception.Create('Resulting FileName was empty Input string was: '
      + InputString));
  end;
end;

#4


5  

Check if string has invalid chars; solution from here:

检查字符串是否包含无效字符;解决方案来自:

//test if a "fileName" is a valid Windows file name
//Delphi >= 2005 version

function IsValidFileName(const fileName : string) : boolean;
const 
  InvalidCharacters : set of char = ['\', '/', ':', '*', '?', '"', '<', '>', '|'];
var
  c : char;
begin
  result := fileName <> '';

  if result then
  begin
    for c in fileName do
    begin
      result := NOT (c in InvalidCharacters) ;
      if NOT result then break;
    end;
  end;
end; (* IsValidFileName *)

And, for strings returning False, you could do something simple like this for each invalid character:

并且,对于返回False的字符串,您可以为每个无效字符执行类似这样的简单操作:

var
  before, after : string;

begin
  before := 'i am a rogue file/name';

  after  := StringReplace(before, '/', '',
                      [rfReplaceAll, rfIgnoreCase]);
  ShowMessage('Before = '+before);
  ShowMessage('After  = '+after);
end;

// Before = i am a rogue file/name
// After  = i am a rogue filename

#5


4  

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

对于读这篇文章并希望使用PathCleanupSpec的其他人,我写了这个测试例程似乎有用......在'网上有一个确定的缺乏例子。你需要包含ShlObj.pas(不确定何时添加了PathCleanupSpec但是我在Delphi 2010中测试了这个)你还需要检查XP sp2或更高版本

procedure TMainForm.btnTestClick(Sender: TObject);
var
  Path: array [0..MAX_PATH - 1] of WideChar;
  Filename: array[0..MAX_PATH - 1] of WideChar;
  ReturnValue: integer;
  DebugString: string;

begin
  StringToWideChar('a*dodgy%\filename.$&^abc',FileName, MAX_PATH);
  StringToWideChar('C:\',Path, MAX_PATH);
  ReturnValue:= PathCleanupSpec(Path,Filename);
  DebugString:= ('Cleaned up filename:'+Filename+#13+#10);
  if (ReturnValue and $80000000)=$80000000 then
    DebugString:= DebugString+'Fatal result. The cleaned path is not a valid file name'+#13+#10;
  if (ReturnValue and $00000001)=$00000001 then
    DebugString:= DebugString+'Replaced one or more invalid characters'+#13+#10;
  if (ReturnValue and $00000002)=$00000002 then
    DebugString:= DebugString+'Removed one or more invalid characters'+#13+#10;
  if (ReturnValue and $00000004)=$00000004 then
    DebugString:= DebugString+'The returned path is truncated'+#13+#10;
  if (ReturnValue and $00000008)=$00000008 then
    DebugString:= DebugString+'The input path specified at pszDir is too long to allow the formation of a valid file name from pszSpec'+#13;
  ShowMessage(DebugString);
end;

#6


3  

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.

好吧,最简单的方法是使用正则表达式和您喜欢的语言版本的gsub来替换任何不是“单词字符”的东西。这个字符类在大多数语言中都是“\ w”,使用类似Perl的正则表达式,或者“[A-Za-z0-9]”作为一个简单的选项。

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.

现在,如果您想使新版本尽可能地与旧版本一样,您可以考虑更换。您可以替换一个或多个您知道的字符,而不是删除。但这样做是一个非常有趣的问题,它可能是另一个问题的好主题。

#7


1  

I did this:

我这样做了:

// Initialized elsewhere...
string folder;
string name;
var prepl = System.IO.Path.GetInvalidPathChars();
var frepl = System.IO.Path.GetInvalidFileNameChars();
foreach (var c in prepl)
{
    folder = folder.Replace(c,'_');
    name = name.Replace(c, '_');
}
foreach (var c in frepl)
{
    folder = folder.Replace(c, '_');
    name = name.Replace(c, '_');
}

#8


0  

Try this on a modern delphi:

在现代的delphi上试试这个:

 use System.IOUtils;
 ...
 result := TPath.HasValidFileNameChars(FileName, False)

I allows also to have german umlauts or other chars like -, _,.. in a filename.

我也允许在文件名中使用德语变音符号或其他字符,如_,_,..

#9


0  

// for all platforms (Windows\Unix), uses IOUtils.
function ReplaceInvalidFileNameChars(const aFileName: string; const aReplaceWith: Char = '_'): string;
var
  i: integer;
begin
  Result := aFileName;
  for i := Low(Result) to High(Result) do
    if not TPath.IsValidFileNameChar(Result[i]) then
      Result[i] := aReplaceWith;
  end;
end.