How can I sanitize a string for use as a filename?

后端 未结 9 810
说谎
说谎 2020-12-23 22:56

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

9条回答
  •  無奈伤痛
    2020-12-23 23:31

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

提交回复
热议问题