Solved

Using wildcards in perl

Posted on 2006-11-03
19
933 Views
Last Modified: 2008-02-01
How do I use a wild card in this case?

The file always equals /home/inside/DVD*/bla_bla.txt. I'm looking for it to look at any DVD but it's not. bla_bla.txt is really in /home/inside/DVD3/bla_bla.txt

my $dir = "/home/inside/DVD*";
my $file = "bla_bla.txt";

  if (-f $dir/$file) {  
     move ("$dir/$file","${other_dvd}/.") || die "Can't copy $dir/$file $!\n";
  }

0
Comment
Question by:ckaspar
  • 6
  • 6
  • 2
  • +4
19 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17871313
for( <$dir/$file> ){
    move ($_,"${other_dvd}/.") || die "Can't copy $_ $!\n";  
}
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17872044
Is there more than one bla_bla.txt, and you're trying to move them all?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17872058
If you're not trying to move them all, which do you want to move?
is your move sub from File::Copy?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 4

Expert Comment

by:NorCal2612
ID: 17872378
Ozo, you're a maniac. You never sleep! :D
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17872534
Do You want use glob?

my $dir = "/home/inside/DVD*";
my $file = "bla_bla.txt";
my @MATCH = glob("$dir/$file");
foreach $m (@MATCH) {
  if (-f $m) {  
     move ("$m","${other_dvd}/.") || die "Can't copy $m $!\n";
  }
}
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17872537
OT: Ozo! You really maniac - what is YoursTZ?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17874426
Assuming you want to check for a single file, then simply do

my $dir = "/home/inside/DVD*";
my $file = "bla_bla.txt";

if (-f <$dir/$file>) {  
     move ("$dir/$file",$other_dvd) or die "Can't copy $dir/$file to $other_dvd $!\n";
}
0
 

Author Comment

by:ckaspar
ID: 17875094
OK,
     I'm looping the content of a file.....

my $dir = "/home/inside/DVD*";

  open (FH, "$list") || die "can't open $list $! \n";
  while (<FH>) {
    chomp;
    @list = split / /;
    $files = $list[0];
   
  if (-f $dir/$file) {  
     move ("$dir/$file","${other_dvd}/.") || die "Can't copy $dir/$file $!\n";
  }


0
 
LVL 84

Expert Comment

by:ozo
ID: 17875126
 while (<FH>) {
    $file = (split)[0];
   
    for( <$dir/$file> ){
       move ($_,"${other_dvd}/.") || die "Can't copy $_ $!\n" if -f;
    }
  }
0
 

Author Comment

by:ckaspar
ID: 17877987
ozo,

Your last post is giving

Modification of a read-only value attempted at test.pl line 216, <FH> line 1.

move ($_,"${other_dvd}/.") || die "Can't copy $_ $!\n" if -f; is line 216

Any ideas?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17878462
how do you define move?
Is there anything else near line 216?
0
 

Author Comment

by:ckaspar
ID: 17890851
if ( <$dir/$file> ){
0
 

Author Comment

by:ckaspar
ID: 17891661
how do you define move? using File::Copy
0
 
LVL 84

Expert Comment

by:ozo
ID: 17892635
I said for( <$dir/$file> ){  not if( <$dir/$file> ){
0
 

Author Comment

by:ckaspar
ID: 17895200
Right, I tried both and none of them worked

for ( <$dir/$file> ){

if( <$dir/$file> ){

and still getting

Modification of a read-only value attempted at test.pl line
0
 
LVL 84

Expert Comment

by:ozo
ID: 17895228
That's very strange, because
move ($_,"${other_dvd}/.") || die "Can't copy $_ $!\n" if -f;
does not attempt to modify anything
What else is in the program?
0
 

Author Comment

by:ckaspar
ID: 17895317
Ok I figured out one way to do this.

I'm not getting Modification of a read-only value attempted at test.pl line

my $dir = "/home/inside/DVD*";

  open (FH, "$list") || die "can't open $list $! \n";
  while (<FH>) {
  $file = (split)[0];

  $new_dir = qx(ls $dir/$file);
   chomp ($new_dir);
   
  if (-f $new_dir ) {  
     move ("$new_dir","${other_dvd}/.") || die "Can't copy $new_dir $!\n";
  }
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18136331
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compress files in RAR using perl 13 81
remove duplicates from the csv file 13 109
Replace  text in a file 2 110
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 140
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question