Script to unify the filename format of the Comiket (F # Edition)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.