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

Yet another regular expression, getting file name and number 1

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hey all, lets say I have a directory that contains the following files
Code:
test
test_1
test_22
test_200
test_301
test_555
testing
wasatest
sysconfig
test.cfg
htp.conf
I want to get a list of all the files that begin with test and have a _number and rename them. I want to rename them to test_number + 1. I have created a script that is not working, it is a regexp.
Code:
#!/usr/bin/perl
use Cwd;
use File::Copy;
# We have to get the argumet
my $fileToName = $ARGV[0];
chomp($fileToName);
my $dir = getcwd;
my $dirToOpen = $dir;
opendir(DIR,$dir) || die("Cannot open directory !\n");
@dirContents = readdir(DIR);
closedir(DIR);
foreach $file (@dirContents)
{
   if(!(($file eq ".") || ($file eq "..")))
   {
      if($fileToName =~ /$file\d/){
         print $file . "\n";
      }
   }
}

any help with the regexp would be grateful.
Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec::Functions[/green] [red]qw([/red][purple]catfile[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [red]'[/red][purple]foobar[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$dh[/blue], [blue]$dir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$dir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]@files[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple]^test_[purple][b]\d[/b][/purple]+$[/purple][red]/[/red][red]}[/red] [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red][blue]$dh[/blue][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red][blue]$dh[/blue][red])[/red][red];[/red]

[gray][i]# Reserve Numerical Sort (to avoid renaming conflicts)[/i][/gray]
[blue]@files[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red] [blue]$_[/blue]->[red][[/red][fuchsia]0[/fuchsia][red]][/red] [red]}[/red]
	[url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red] [blue]$b[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] <=> [blue]$a[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] [red]}[/red]
	[black][b]map[/b][/black] [red]{[/red] [red][[/red][blue]$_[/blue], [red]/[/red][purple]^test_([purple][b]\d[/b][/purple]+)$[/purple][red]/[/red][red]][/red] [red]}[/red]
	[blue]@files[/blue][red];[/red]

[gray][i]# Rename Files[/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][blue]@files[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$num[/blue] = [blue]$file[/blue] =~ [red]/[/red][purple]^test_([purple][b]\d[/b][/purple]+)$[/purple][red]/[/red][red];[/red]
	[blue]$num[/blue]++[red];[/red]
	
	[black][b]my[/b][/black] [blue]$from[/blue] = [maroon]catfile[/maroon][red]([/red][blue]$dir[/blue], [blue]$file[/blue][red])[/red][red];[/red]
	[black][b]my[/b][/black] [blue]$to[/blue]   = [maroon]catfile[/maroon][red]([/red][blue]$dir[/blue], [red]"[/red][purple]test_[blue]$num[/blue][/purple][red]"[/red][red])[/red][red];[/red]
	
	[url=http://perldoc.perl.org/functions/rename.html][black][b]rename[/b][/black][/url][red]([/red][blue]$from[/blue], [blue]$to[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't rename [blue]$from[/blue] -> [blue]$to[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top