CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
filename matching



Jun 22, 2006, 6:46 AM

Post #1 of 2 (632 views)
filename matching Can't Post


I was trying to scan one directory for one file and if it finds then move it to another directory. To achieve this task first I was trying to find file I wrote the code but the code prints all files in the directory...

use POSIX qw(ceil floor); 
use File::Copy;
use File::Find;

$| = 1; #flush output right away

#input directory for time being local directory used
my $base_dir = "E://Scripts////Perl";

print "\nStarting checkFile script\n";

my $filename = "FCC.pos";


# Check for file in directory
sub scanInputDir()
print "\n IN scanDir \n";
#scan directory for filename
my @current_dir;
opendir(DIR, $base_dir) || die "Error: could not open input directory $input_dir";
@current_dir = readdir(DIR);
foreach (@current_dir)
my $flname = $_;
my $name = $flname;
if ( $flname eq "." || $flname eq "..")
# empty no file or directory
if( $flname = ~ /^$filename/ )
# found the file
#print "Found File \n";
$found = 1;
print "\n $name \n";
# not found the file
#print "Not Found the File\n";


(This post was edited by davorg on Jun 23, 2006, 2:40 AM)

Thaumaturge / Moderator

Jun 23, 2006, 2:57 AM

Post #2 of 2 (628 views)
Re: [abhisri] filename matching [In reply to] Can't Post

I can see two problems here. One specific and one more general.

The specific problem is with your syntax. Your filename check looks like this:

if( $flname = ~ /^$filename/ )

You've taken the =~ operator and split it into two. Perl interprets that as the = operator followed by the ~ operator. That's not what you want at all.

Incidently, if your match operator was working as you expected, it still wouldn't be going the right thing as your regex is checking for strings that start with $filename not strings that equal $filename. It's a subtle difference, but it might be important. Also for checking string equality, the equality operator (eq) is better than the match operator.

The more general problem is that you're doing far too much work for yourself. You've basically reinvented the glob. Why not use that instead.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives