Solved

Using wildcards in perl

Posted on 2006-11-03
19
929 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 90
Edit Odbc.ini using perl module 2 78
Put query string from URL request -  PERL HTTP Daemon 2 73
Perl File::Find alternative 1 52
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now