Discussion Locked

Ranaming of files

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi Ppl

I like all my filenames to be in samll case.
However it becomes quite a pain to sit and rename all the new files I get from outside.
Can i write a script/bat etc to automate the process.

TIA

Mayuresh

10 replies

by thechas

This won't take care of everything.
Depending on your OS and IE version, there is a checkbox in the file view settings that can be unchecked to prevent all uppercase file names.

I believe that it will still allow the first letter to be uppercase.

by timthetoolman

...but because I could I went one further...

Once you've installed perl, cut and paste this code into a txt document using notepad, rename it to have a .bat extension rather than .txt and stick it on your desktop.

Now you'll be able to simply drag and drop folders from the windows explorer onto the icon and it will automatically rename the folder's contents recursively - ie. all its subfolders too...

@rem = '--*-Perl-*--
@echo off

if not exist %0 set batchf=%0.bat
if exist %0 set batchf=%0

if "%OS%" == "Windows_NT" goto WinNT
perl -x %batchf% %1 %2 %3 %4 %5 %6 %7 %8 %9
if not errorlevel 0 goto fail1
goto endofperl

:WinNT
perl -x %batchf% %*
if not errorlevel 0 goto fail1
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul

:endofperl
echo.
pause
exit

:fail1
echo.
echo Problem with the rename script.
echo.
goto endofperl

#!perl

#use File::Recurse;
use File::Find;

# read in directory
@ARGV = qw(.) unless @ARGV;

# verify directory
if (-d @ARGV[0])
{
print "Working Directory: @ARGV[0]\n";
}
else
{
print "\nError: Directory Not Found @ARGV[0]\n"; exit(0);
}

# call recurse function from module
# this applies the sub-routine lcName to all files
# in the specified directory
find(\&lcName, @ARGV);

# define the lcName sub-routine which lowercases all files

sub lcName {
# receive passed filename
$filename = $File::Find::name;

$newfilename = $filename;

# regexp that converts to lowercase
$newfilename =~ tr/A-Z/a-z/;

# if I can read and write to it
# rename the filename tothe lowercase one
if ((-r $filename) && (-w $filename)) {
rename($filename, $newfilename);
print " Renamed: $filename to $newfilename\n";
}
}

Cheers,
Tim.

by timthetoolman

...and one last thing.

Windows isn't generally case sensitive, but Java is. Dont use this to rename .java filenames or you'll find they may not work anymore.

Also...all the formatting has been lost with the post, but it shouldn't matter... youcan re-indent it if you like.

Cheers,
Tim.

by mayuresh

Thanks guys. I will be trying it at home. Will post on how it works out.

Thanks again..

Mayuresh

by mayuresh

Tim...the perl solution was amazing.
It works like a charm.
I went one step futher though. Instead of keeping it on my desktop, which was kinda difficult to drop any folder on it. So i just added my bat to the "send to" option in my right click.
Thanks Again.

Mayuresh

ps: if you'll just reply, i can give you all the points u so deserve. I still can't get the hang of how to properly asign points..comments etc...oh well..maybe some day :-)

by timthetoolman

Hi Mayuresh,

I'm glad the script worked so well for you. Adding it to the send-to menu was a great idea...nice touch!

Cheers,
Tim.

This discussion is closed to new replies.