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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
filename matching

 



abhisri
Novice

Jun 22, 2006, 6:46 AM

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

Hello

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...

Code
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";

scanInputDir();

# 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
}
else
{
if( $flname = ~ /^$filename/ )
{
# found the file
#print "Found File \n";
$found = 1;
print "\n $name \n";
}
else
{
# not found the file
#print "Not Found the File\n";
}
}
}

closedir(DIR)
}



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


davorg
Thaumaturge / Moderator

Jun 23, 2006, 2:57 AM

Post #2 of 2 (559 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:


Code
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
http://www.dave.org.uk/
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