T
theo22
Newbie question:
Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread
All I want to do is pass an argument to my script on the command line.
The argument is the directory that gets passed to the opendir()
function. When I run the script , I always get an error:
Uncaught exception from user code:
Directory D:\My Documents\ScriptTesting does not exist: No
directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.
How can it say the directory does not exist if the file that it is
referring to with the error, is in that directory? If I hard code the
directory in the opendir() function, it works fine.
Here is my code. You will notice it takes a directory and then spits
it out to a .txt file. It works great except I can't pass the
directory I want it to list through the command line:
******************************************
#!/usr/bin/perl -w
use strict;
use diagnostics;
#Tried a variable name for ARGV[0] and chomp as alternative, but it
made no difference so I commented it all out.
#my $DirPath=$ARGV[0];
#chomp($DirPath);
#Create directory handle so we can get a list of all the files in the
directory.
#opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
$!";
opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";
#Create filehandle to dump list of directory files into a .txt file
for reading.
open(MyFile, ">My_Files.txt") || die "Cannot open My_Files.txt: $!";
#Gets the list of only .txt files
my @Files = grep (/\.txt$/, readdir MyDir);
#Print each file in the directory to a .txt file. Prepend filenames
with thier full path.
foreach my $Files (@Files){
$Files = "$ARGV[0]\\$Files";
print MyFile "$Files\n";
}
print "My_Files.txt file created with list of all .txt files!\n";
#Close directory and file handles.
closedir(MyDir);
close(MyFile);
****************************************
Please Help. Here is the command that I used:
C:>GetDirList3.pl "D:\My Documents\ScriptTesting"
What am I doing wrong here?
Thx!!
Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread
All I want to do is pass an argument to my script on the command line.
The argument is the directory that gets passed to the opendir()
function. When I run the script , I always get an error:
Uncaught exception from user code:
Directory D:\My Documents\ScriptTesting does not exist: No
directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.
How can it say the directory does not exist if the file that it is
referring to with the error, is in that directory? If I hard code the
directory in the opendir() function, it works fine.
Here is my code. You will notice it takes a directory and then spits
it out to a .txt file. It works great except I can't pass the
directory I want it to list through the command line:
******************************************
#!/usr/bin/perl -w
use strict;
use diagnostics;
#Tried a variable name for ARGV[0] and chomp as alternative, but it
made no difference so I commented it all out.
#my $DirPath=$ARGV[0];
#chomp($DirPath);
#Create directory handle so we can get a list of all the files in the
directory.
#opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
$!";
opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";
#Create filehandle to dump list of directory files into a .txt file
for reading.
open(MyFile, ">My_Files.txt") || die "Cannot open My_Files.txt: $!";
#Gets the list of only .txt files
my @Files = grep (/\.txt$/, readdir MyDir);
#Print each file in the directory to a .txt file. Prepend filenames
with thier full path.
foreach my $Files (@Files){
$Files = "$ARGV[0]\\$Files";
print MyFile "$Files\n";
}
print "My_Files.txt file created with list of all .txt files!\n";
#Close directory and file handles.
closedir(MyDir);
close(MyFile);
****************************************
Please Help. Here is the command that I used:
C:>GetDirList3.pl "D:\My Documents\ScriptTesting"
What am I doing wrong here?
Thx!!