Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created October 19, 2011 22:01
Show Gist options
  • Save theburningmonk/1299821 to your computer and use it in GitHub Desktop.
Save theburningmonk/1299821 to your computer and use it in GitHub Desktop.
A F# script to rename image files to be title cased
open System.IO
open System.Threading
let rec filesUnder basePath =
seq {
yield! Directory.GetFiles(basePath)
for subDir in Directory.GetDirectories(basePath) do
yield! filesUnder subDir
}
let toTitleCase (str : string) =
Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase <| str.ToLower()
let (|FileExtension|) filePath = Path.GetExtension(filePath).ToLower()
let (|IsImageFile|_|) filePath =
match filePath with
| FileExtension ".jpg"
| FileExtension ".png"
| FileExtension ".gift"
-> Some()
| _ -> None
let imageFiles =
__SOURCE_DIRECTORY__
|> filesUnder
|> Seq.filter (fun filePath -> match filePath with | IsImageFile -> true | _ -> false)
printfn "Before renaming:"
imageFiles |> Seq.iter (printfn "%s")
printfn "Press any key to rename these files..."
System.Console.Read()
// rename files
imageFiles
|> Seq.iter(fun filePath ->
let fileName = Path.GetFileName(filePath)
File.Move(filePath, sprintf "%s\%s" (Path.GetDirectoryName filePath) (toTitleCase fileName)))
printfn "After renaming:"
imageFiles |> Seq.iter (printfn "%s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment