Since both the simple and C # versions of Ruby are written, it's good to write an F # version to compare.
The following program logic is basically similar to the previous C # version, but in how to extract the various parts of the file name in a different way: C # version of the Trygetrenamename () method is more inclined to directly support different patterns and format;f# The version is hard-coded directly into the Parsefilename and Getreformattedfilename functions with the pattern and format. In the end how to divide the good I am not quite sure, but in such a small program to divide the way is not reflected in the good, perhaps not worth more?
Reformatcomiketfilenames.fsx
F # code
#light Open System Open System.IO Open System.Text.RegularExpressions let parsefilename name = let result = Regex.mat CH (name, @ "^\ ([^)]+) \) \s*\ ([^)]+) \) \s*\[([^\]]+) \]\s* (. +) $") Let success = result. Success Let Comiketnum = result. Groups.item (1). Value Let ContentType = result. Groups.item (2). Value Let Circlename = result. Groups.item (3). Value Let ItemName = result. Groups.item (4). Value success, Comiketnum, ContentType, Circlename, itemname let getreformattedfilename name = let success, COMIKETNU M, ContentType, circlename, itemname = parsefilename name if success then sprintf "(%s) (%s) [%s]%s" Comiketnum Conte Nttype circlename itemname Else name let Reformatandrenamedir (dirinfo:directoryinfo) = let name = Dirinfo.name L ET destname = getreformattedfilename name If name <> destname then printfn '%s ' Destname Dirinfo.moveto (Pa Th. Combine (DirInfo.Parent.FullName, destname)) True Else false let Reformatandrenamefile (Fileinfo:fileinfo) = let name = fileinfo.name let Destname = getreformattedfilename name If name <> Destname then PRINTFN '%s ' Destname Fileinfo.moveto (Path.Combine (Fileinfo.directoryname, destname)) True Else false let Getrootdir = Fun ()-> #if COMPILED Let args = Environment.getcommandlineargs () #else let args = FSI. CommandLineArgs #endif Let RootPath = Match args with | [| _ |]
-> new DirectoryInfo (environment.currentdirectory); | _-> New DirectoryInfo (args.[ 1]) RootPath Let main = Fun ()-> Let root = Getrootdir () array.iter (Fun dir-> (Reformatandrenamedir dir; ())) (Root. GetDirectories ()) Array.iter (fun file-> (reformatandrenamefile file; ())) (Root. GetFiles ()) main ()