F#'文件名无效!'在尝试File.Move时

时间:2018-03-10 22:52:24

标签: f#

我决定学习F#。我编写的用于学习新语言的程序之一是“jpg-renamer”,一个命令行应用程序,它将文件路径作为args,读取NSURLVolumeAvailableCapacityForImportantUsageKey EXIF标记并使用DateTimeOriginal将文件重命名为{{ 1}}这是一个有效的文件名。

执行实际重命名的功能是:

DateTimeOriginal

当我运行程序时,会发生这种情况:

YYYY-MM-dd_hh.mm.ss.jpg

注意let renameFile (path:string) = let newName = (getOriginalDateTime path |> changeTimeStampString) + Path.GetExtension(path) printfn "%s -> %s" <| path <| newName File.Move(path, newName) 如何打印正确有效的新文件名。

作为调查的一部分,我试图将新文件名作为字符串文字放入代码中,如下所示:

mono srenamer.exe IMG_20180303_153239.jpg
IMG_20180303_153239.jpg -> 2018-03-03_15.32.40.jpg

Unhandled Exception:
System.ArgumentException: The file name is not valid.
  at System.IO.File.Move (System.String sourceFileName, System.String 
destFileName) [0x0008d] in <b64e2aa77b4f4d60b739d6ceaf49caa4>:0
  at Srenamer.renameFile (System.String path) [0x0005f] in <5aa45864019ff926a74503836458a45a>:0
  at Srenamer.renameFiles[a](Microsoft.FSharp.Collections.FSharpList`1[T] paths) [0x00030] in <5aa45864019ff926a74503836458a45a>:0
  at Srenamer.main (System.String[] args) [0x00006] in <5aa45864019ff926a74503836458a45a>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: The file name is not valid.
  at System.IO.File.Move (System.String sourceFileName, System.String destFileName) [0x0008d] in <b64e2aa77b4f4d60b739d6ceaf49caa4>:0
  at Srenamer.renameFile (System.String path) [0x0005f] in <5aa45864019ff926a74503836458a45a>:0
  at Srenamer.renameFiles[a] (Microsoft.FSharp.Collections.FSharpList`1[T] paths) [0x00030] in <5aa45864019ff926a74503836458a45a>:0
  at Srenamer.main (System.String[] args) [0x00006] in <5aa45864019ff926a74503836458a45a>:0

哪个按预期工作。我还检查了printfn,它只返回let newName = "2018-03-03_15.32.40.jpg" File.Move(path, newName) (Linux)。

那么为什么当完全相同的硬编码字符串工作正常时代码生成无效文件名的字符串呢?

供参考,以下是完整的源代码:

Path.GetInvalidFileNameChars

1 个答案:

答案 0 :(得分:2)

查看EXIF标记值的实际字节

printfn "%A" <| x.Value

显示了这一点:

[|50uy; 48uy; 49uy; 56uy; 58uy; 48uy; 51uy; 58uy; 48uy; 51uy; 32uy; 49uy; 53uy; 58uy; 51uy; 50uy; 58uy; 52uy; 48uy; 0uy|]

最后一个字符为空。删除它会使文件名有效。感谢@FyodorSoikin指出我正确的方向。