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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Function

Status
Not open for further replies.

Nova980

Technical User
May 20, 2009
40
US
Hello,

I have a file that is As2'd to me named BC090602.txt. Every week the numbers in the file name change. So next week the file will be named BC090603.txt. How can I change my script to look for the BC characters then change orginal filename to just BC.txt no matter what the numbers are? Can I use wild characters?

Code:
#!/usr/bin/perl

print "Content-type:text/html\n\n";

rename("BC090602.txt", "BC.txt") || print "Unable to change name.<br><br>";

print "Done";

exit;
 
You'll probably need to scan the directory and find files that begin with BC.

Code:
foreach my $file (<BC*.txt>) {
   rename ($file, "BC.txt");
   last; # so we only rename 1 file
}

Or to shorten it:

Code:
rename ( (<BC*.txt>)[0], "BC.txt");

Example:

Code:
[kirsle@firefly test]$ touch BC010203.txt
[kirsle@firefly test]$ touch BC010204.txt
[kirsle@firefly test]$ ll
total 0
-rw-rw-r-- 1 kirsle kirsle 0 2009-07-14 13:05 BC010203.txt
-rw-rw-r-- 1 kirsle kirsle 0 2009-07-14 13:05 BC010204.txt
[kirsle@firefly test]$ perl
rename ( (<BC*.txt>)[0], "BC.txt");
__END__
[kirsle@firefly test]$ ll
total 0
-rw-rw-r-- 1 kirsle kirsle 0 2009-07-14 13:05 BC010204.txt
-rw-rw-r-- 1 kirsle kirsle 0 2009-07-14 13:05 BC.txt

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks a million, works perfectly. How can I include the current date after the BC in BC.txt? Ex:
Code:
foreach my $file (<BC*.txt>) {
   rename ($file, "BCyymmdd.txt");
   last; # so we only rename 1 file
}

 
Nevermind I just used POSIX qw(strftime).

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top