07 May, 2008

Parsing File Lists with Tcl

Hey all,
Been a bit of time since my last post. I've been struggling attaining some new information to post, but haven't found anything 'post worthy'. Instead, I wandered back in the ol' memory to find a useful nugget of info that you may find useful.

I love Tcl. From the bottom of my heart, this is the scripting language of choice for this cubicle dweller. A routine use for scripting languages is to batch process vast lists of files. Any well behaved *nix user probably uses camel back file naming notations (e.g. SomeFileForYou.txt), while others may use a underscore delimited notation (e.g. some_file_for_you.txt), but Windows enthusiasts (many of my teammates included) insist on using spaces in their file names (e.g. Some File For You.txt).

Batch processing filenames with spaces can present a problem if you don't parse the list properly. Since Tcl lists are generally seperated by spaces, file names with spaces are parsed as individual files for each word.

Observe:

$ ls /var/tmp/someDir
total 8
-rw-r--r-- 1 fatslowkid fatslowkid 6 2008-05-07 22:38 Some Longer File Name.txt
-rw-r--r-- 1 fatslowkid fatslowkid 7 2008-05-07 22:38 Some Long File Name.txt

$ cat listFiles
1 #!/usr/bin/tclsh
2
3 set fileList [exec find /var/tmp/someDir/]
4 #set fileList [split $fileList "\n"]
5
6 foreach file $fileList {
7 puts '$file'
8 }


Running will result in:
$ ./listFiles
'/var/tmp/someDir/'
'/var/tmp/someDir/Some'
'Long'
'File'
'Name.txt'
'/var/tmp/someDir/Some'
'Longer'
'File'
'Name.txt'


Uncommenting line 4 which splits the list using the newline character as the delimiter solves the problem. Observe:

$ ./listFiles
'/var/tmp/someDir/'
'/var/tmp/someDir/Some Long File Name.txt'
'/var/tmp/someDir/Some Longer File Name.txt'


TaDa. Hope you find this useful, I certainly have over the years.

No comments: