Advertisement
Guest User

Renaming Files with TCL

a guest
Jan 19th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.13 KB | None | 0 0
  1. #!/usr/bin/tclsh
  2.  
  3. # Convert to lower and dash for spaces.
  4.  
  5. if { $argc != 1 } {
  6.     puts "Requires a file path."
  7.     exit
  8. }
  9.  
  10. set filepath [lindex $argv 0]
  11. puts "File path: $filepath"
  12.  
  13. if { ![file isdirectory $filepath] } {
  14.     puts "File path is not a directory."
  15.     exit
  16. }
  17.  
  18. proc generatefilename { filename } {
  19.     set temp [string trim [string tolower $filename]]
  20.     regsub -all { } $temp {-} filename
  21.  
  22.     return $filename
  23. }
  24.  
  25. proc getfilename { file } {
  26.     if { [regexp {.+/(.+)} $file match filename] } {
  27.         return $filename
  28.     }
  29.  
  30.     puts "Regexp failed for $file"
  31.     return $file
  32. }
  33.  
  34. # check leading slash for filepath
  35. if { ![string eq [string index $filepath end] {/}] } {
  36.     append filepath {/}
  37. }
  38.  
  39. foreach file [lsort [glob -nocomplain -directory $filepath "*\.*"]] {
  40.     if { ![file isfile $file] } {
  41.         puts "Skipping $file"
  42.         continue
  43.     }
  44.  
  45.     set filename [getfilename $file]
  46.  
  47.     set newfilename [generatefilename $filename]
  48.  
  49.     puts "Renaming $filename to $newfilename"
  50.  
  51.     set newfilepath "${filepath}${newfilename}"
  52.     file rename $file $newfilepath
  53. }
  54.  
  55. puts "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement