Solved

Using wildcards in perl

Posted on 2006-11-03
19
947 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do I grep for a specfic DNS record in a directory 3 49
Perl 101 11 85
Problem with one substitution modifying another 3 77
perl getopt long help 34 89
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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

733 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