Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert all directory names to upper case? 1

Status
Not open for further replies.

knsnln

Technical User
Oct 2, 2002
7
0
0
US
Hi all,

I am new to this forum. I am self studying perl and want to write a script to convert all directory names to upper case and all file names to lower case. I will apreciate it if there is any one can help me with this issue.

Thanks in advance.

Ken
 
Hi Ken,

Be careful - on a case sensitive box, some really weird shtuff may start happening.

Are you talking about altering the directory structure to reflect upper case names, as this could have serious ramifications, to users/applications expecting different target files.

At a guess, and without knowing your target OS, you're looking at at least a 3 part process.
Phase 1 - Backup your target environment securely & adequately. (GOOJFC)
Phase 2 - create a set of uppercase directories, and copy files from source directories therein - check they don't exist already - what should be the exception here?
Phase 3 - rename the files to lower case in each of the directories you've created

Unless you're the sole, or understood to be deemed, owner of the files, never delete, as people will wait for you in the parking lot, especially if you said you did it on purpose

I doubt if any of this helps
 
I have a script (bleow) that I use to change the case of directory and file names of directory structures on my NT box. As it stands it changes everything to either upper or lower case, as specified on the commandline, but it would not be difficult to amend it to do one thing with directory names and another with files as it currently treats the 2 separately anyway:

#!/usr/contrib/bin/perl
##########################
# SCRIPT PURPOSE
# changes the case of all files in the selected directory structure to upper or lower case, as selected
##########################
# Define modules
use File::Find;
##########################
# Initialise variables
my @DIRS_LIST;
$dirlength = 0;
$dirname = $ARGV[0]; # read first command line switch - directory
$case = $ARGV[1]; # read second command line switch - case

##########################
# Error trap for incorrect syntax
unless ( defined($case) ) {
die &quot;the syntax should be 'perl changecase.pl <drive:/directory> <u or l>'\n&quot;;
}
########################################
# read the directory structure
$i = 0;
chdir $dirname or die &quot;cannot chdir to $dirname: $!&quot;; # change to directory given by first switch
find sub { $fileinlist [ $i++ ] = $File::Find::name if -d }, '.'; # obtain list of all subdirectories
foreach my $file (@fileinlist) { # for every subdirectory found
$_ = &quot;$file&quot;; # write directoryname to $_
m/([a-zA-Z0-9._\s]+)[.!?]?\s*$/x; # match the name only i.e. remove path
$justname = $1; # write the name only to $justname
chdir $dirname; # change to top level directory given in commandline
if ($case eq &quot;u&quot; or $case eq &quot;U&quot;) { # if u or U chosen
$changed = uc($justname); # change directory name to uppercase
} else {
$changed = lc($justname); # or change to lowercase
}
`rename &quot;$file&quot;,&quot;$changed&quot;`; # do the actual case change
$DIRS_LIST[$dirlength] = $file; # add the subdirectory name to the DIRS_LIST
$dirlength++;
}
print &quot;\nFiles and folders in the following directories have been changed:\n\n&quot;;
foreach $directory (@DIRS_LIST) { # for each subdir in the list
my $actual = substr($directory, 2, 1000); # remove the leading ./ from the name
my $fulldir = join &quot;/&quot;, $dirname, $actual; # produce full pathname for each subdir
print &quot;$fulldir\n&quot;;
chdir $fulldir or die &quot;cannot chdir to $fulldir: $!&quot;; # change directory to next one in $fulldir
if ($case eq &quot;u&quot; or $case eq &quot;U&quot;) { # if u or U chosen
foreach my $file (glob &quot;*&quot;) { # select all files in current dir
if (!-d $file) { # if not a subdirectory
my $newfile = uc($file); # change to uppercase
`move &quot;$file&quot;, &quot;$newfile&quot;`; # rename the file
}
}
} else { # if l (lowercase) selected do same using lc
foreach my $file (glob &quot;*&quot;) {
if (!-d $file) {
my $newfile = lc($file);
`move &quot;$file&quot;, &quot;$newfile&quot;`;
}
}
}
chdir $dirname or die &quot;cannot chdir to $dirname: $!&quot;;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top