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!

Cannot user wildcard in Perl under windows XP

Status
Not open for further replies.

bruceleung2000

IS-IT--Management
Dec 20, 2003
19
0
0
HK
Hello,
I have written a program to try to get wildcard from DOS command (under Windows XP), but it cannot do wildcard to get all files (ok1, ok2, .... ok9) but ok* or ok? - it WORKS under only unix and linux.
Does anybody help to tell me how to solve this problem ???

Thanks in advance !!!
Regards,
Bruce

------------------ command under DOS prompt ----
e.g. perl rm-alike.pl ok*
-------------- end ----------

------------ extract from rm-alike.pl ------------
while ($fname = $ARGV[$ctr]) {
print "ctr=[" ;
print $ctr ;
print "]\n" ;
print "fname=[" ;
print $fname ;
print "]\n" ;
$fname_arr[$ctr] = $fname;
print "fname_arr=[" ;
print $fname_arr[$ctr];
print "]\n" ;
$ctr = $ctr +1;
} # end while
---------------- end --------------------------------

----------------- output ---------------------
G:\Schedule_Jobs\perl>perl rm-alike.pl 'ok*'
ctr=[0]
fname=['ok*']
---------------- end -------------------


----------------- Version of my Perl -----
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

G:\Schedule_Jobs\perl>perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Binary build 811 provided by ActiveState Corp. ActiveState is a division of Sophos.
Built Dec 13 2004 09:52:01
------------- end -------------------------
 
The easy way is to just add another loop in there and do the globbing yourself.
Code:
foreach my $arg (@ARGV) {
    foreach my $fname (glob $arg) {
        # do stuff
    }
}
There was some other way you could alter your Perl environment in Windows, but I don't remember it off the top of my head. More importantly, if you're concerned about multi-platform compatibility for distribution, other Windows users won't have their environment altered to glob everything.

________________________________________
Andrew

I work for a gift card company!
 
On *nix the shell does the wildcard expansion, before Perl even sees the arguments. The shell on Windows does not do wildcard expansion.

icrf has got the way to deal with this.



 
Might be easier and more reliable to use perl to
read in a directory listing. That you know exactly
what you will be getting.
Cheers.

Code:
# This can be any valid regex
# ".*" Matches anything.  
# Use "ok" to return any filename that contains that word.
 my($pattern) = ".*";  
 
# Set to whatever directory you want. 
# Hint "." equals current directory.
 my($mydir) = "."; 
 
 my(@fnames); 
 opendir(DIR,$mydir);
 while ($f=readdir(DIR))
  {
   next if ($f eq "." || $f eq "..");
   if( $f =~ /$pattern/){ push(@fnames,$f); }   
  }
 closedir(DIR);  
 $ctr = @fnames;
 print "\nctr=[$ctr]\n";
 for (@fnames){  print "fname_arr=['$_']\n";   }
 
You can also
Code:
my @dirlist = qx[dir "mydir\\*.xml" /B]
chomp @dirlist;
and let the Windows DIR command do the globbing for you.
 
I wouldn't be too keen on using 'dir' when there are easily-implemented cross-platform ways of achieving the same thing.
 
glob() is Perl. When using shell-like wildcards, I'm a huge fan of it. It's short, quick, and portable. Sure, opendir et al is faster, but often takes a good bit more code to make it work. You've got to seriously be splitting hairs to tell the difference.

________________________________________
Andrew

I work for a gift card company!
 
icrf said:
You've got to seriously be splitting hairs
as opposed to having a bit of fun while splitting hairs, no doubt

--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top