Welcome, guest | Sign In | My Account | Store | Cart

This utility changes the filenames that are in upper-case to lower-case.

Tcl, 28 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
#\
    exec tclsh "$0" "$@"


#changes filenames in caps to lowercase

#show usage info if no option is given
if {[llength $argv] == 0} {
    puts "Usage: hi2lo file1 file2..."
}

foreach file $argv {  
    #check if the string contains any UPPERCASE letter
    if {[string is lower $file]} {
	puts "File '$file' is already in lowercase."
    } else {
		set filelow [string tolower $file]   	
		#check if the file is present on the disk
		if {[file exists $file]} { 
		    #check if the two names are different
	    	if {[string compare $file $filelow]} {		
				puts "From: $file To: $filelow"
				exec cp $file $filelow
		    }
		}
    }
}

While moving files from Windows to Unix, a common problem is that the filenames under Windows are in caps; whereas in Unix it is preferable/addiction to keep the filenames in lower-case. The basic minimum is to use 'mv' command. However, dealing with lots of files is a trouble. This script is to be put in your bin directory and then used as $ hi2lo FILE1 FILE2 ... It will create file1, file2 ... keeping the original FILE1, FILE2,... intact.

1 comment

Gerald Lester 20 years, 10 months ago  # | flag

Use built in Tcl commands where available -- not exec. Instead try:

foreach originalName $argv {
    set newName [string tolower $orignalName]
    if {![string equal $orignalName $newName]} then {
        file rename $orignalName $newName
        ##
        ## Or closer to the original post:
        ##    file copy $orignalName $newName
    }
}
Created by saif.abrar on Wed, 14 May 2003 (MIT)
Tcl recipes (162)
saif.abrar's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks