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!

Add characters to a filename. 2

Status
Not open for further replies.

pcutler

Technical User
Jan 18, 2002
59
0
0
CA
Hi,

I'm hoping someone can point me in the right direction.

I need to add characters to the file name of all the files in a given directory.

At the start the files are named:
ABC-1105-01.pdf,ABC-1105-02.pdf ...

I need to change them to:
ABC-1105-(01).pdf,ABC-1105-(02).pdf ...

I don't even know where to start with this. Is PERL a good candidate for doing this?

Thanks for your time.

Peter.
 
Hi

Personally I would do it like this :
Code:
ls | perl -pe 's/(.*)(\d\d)(\..*)/mv "$1$2$3" "$1($2)$3"/' | sh
Of course, that works on Linux.

Note, that you did not specified how exceptions should be treated, so I not treated them.

Feherke.
 
The long method (without use of regular expressions) could look something like this so long as the filename format is always x-x-x.ext

Using regular expressions is a much better way as in feherke's example above

Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';
print "Content-type: text/html\n\n";

my $path = '/path/to/directory';

#my @files = ('ABC-1105-01.pdf','ABC-1105-02.pdf');
opendir (LOGDIR, "$path") || die "Cannot Open Dir";
my @files = readdir (LOGDIR);
closedir (LOGDIR);

foreach (@files) {
print "$_\n";
}

my (@newfiles);
foreach (@files) {
#e.g. $filename = "ABC-1105-01" & $extension = "pdf"
my ($filename,$extension) = split(/\./,$_);
#e.g. $part1 = "ABC" & $part2 = "1105" & $part3 = "01"
my ($part1,$part2,$part3) = split(/\-/,$filename);
#e.g. $part3 = "(01)"
$part3 = '('.$part3.')';
#e.g. $newfile = "ABC-1105-(01).pdf"
my $newfile = $part1.'-'.$part2.'-'.$part3.'.'.$extension;
push(@newfiles,$newfile);
}

foreach (@newfiles) {
print "$_\n";
}

Chris
 
Arg, unfortunate you can't edit posts :/

This should be removed unless its necessary:
Code:
use CGI ':standard';
print "Content-type: text/html\n\n";
 
Thanks for your help. You guys rock!

I'm trying to do this on a Windows system (That's where the files are created).

Chris, I've been playing around for a what to split the file name but had no clue how to do it. You've made my life MUCH easier.

When the script runs, it generates a list with the correct names. How do I re-name the actual files?

Thanks again.

Peter.
 
All wrapped up in one:

Code:
use strict;
use warnings;
my $path = '/path/to/directory';
opendir (LOGDIR, $path) or die "Cannot Open Dir: $!";
my @files = grep {/\.pdf$/i} readdir LOGDIR;
closedir (LOGDIR);

foreach (@files) {
   if (/^(.*-)([^.]+)(\.pdf)$/i) {
      rename "$path/$_" , "$path/$1($2)$3"} or print "rename failed :$!\n";
   }
}

You should always test with dummy files in a dummy directory before doing something like rename or unlink on your actual files to make sure its working.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sorry, error in code above, should be:

Code:
use strict;
use warnings;
my $path = '/path/to/directory';
opendir (LOGDIR, $path) or die "Cannot Open Dir: $!";
my @files = grep {/\.pdf$/i} readdir LOGDIR;
closedir (LOGDIR);

foreach (@files) {
   if (/^(.*-)([^.]+)(\.pdf)$/i) {
      rename "$path/$_" , "$path/$1($2)$3" or print "rename failed :$!\n";
   }
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top