Even on Windows the forward slash in filepaths works. That saves you a lot of escaping. use Modern::Perl;
my $dirname = 'c:/data/eps';
opendir( DIR, $dirname );
my @files = readdir(DIR);
foreach my $file (@files) {
my $file_path = "$dirname/$file";
say "File path: $file_path";
}
closedir DIR;
OutputFile path: c:/data/eps/.
File path: c:/data/eps/..
File path: c:/data/eps/agrafort.jpg
File path: c:/data/eps/alazif
File path: c:/data/eps/ARF.jpg
File path: c:/data/eps/ARF.png
...
For anything more complicated than a simple list of all files in a directory, File::Find::Rule is something to think about (and it gives you the full file path).
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [Watch: Dir/Any] [d/l] [select] |
#!/usr/bin/perl
use 5.014; # use 5.014 is effectivly the same as use
+strict. Do it.
use File::Spec; # you can't use File::Spec and components
+until you've
# imported the module and its components
my ($FILE, @FILES); # strict requires my; more important, it w
+ill tell you about many goofs
my $dirname="...
Update: fixed missing words in comment. | [reply] [Watch: Dir/Any] [d/l] |
If you're just interested in "*.txt" files in one directory, I recommend to forego using readdir and friends, and go straight to glob.
@txt_files = glob qq("C:\\Users\\Me\\Desktop\\Cluster1\\*.txt");
Unlike readdir, glob returns the full path in the same format as you passed as an argument: absolute path in yields absolute path out; relative path in (relative to the current working directory of perl which you can change with chdir) yields relative path out.
Note that if (and only if) your $dirname contains spaces, then the double quotes are required. You don't have to put them around the whole path-with-wildcards, replacing every single space with qq(" ") will work, too.
| [reply] [Watch: Dir/Any] [d/l] [select] |
use File::Util;
my $f = File::Util->new();
my @files = $f->list_dir( "C:\\book", qw( --no-fsdots --with-paths ) )
+;
From the command line: (demonstrating that "/" works fine on windows)
$ perl -MFile::Util -e 'print join "\n", File::Util->new->list_dir("C:
+/book", qw( --no-fsdots --with-paths ))'
C:/book/Generic_User_Guide.pdf
C:/book/Quick_Guide.pdf
--
Tommy
$ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'
| [reply] [Watch: Dir/Any] [d/l] [select] |
#!/usr/bin/perl -l
use strict;
use warnings;
my $dirname = shift @ARGV;
opendir( DIR, $dirname );
my(@files) = readdir(DIR);
print "File path: ";
foreach my $file (@files) {
my $file_path = "$dirname/$file";
$file_path =~ s/\\|\//\\\\/g;
print $file_path;
}
closedir DIR;
| [reply] [Watch: Dir/Any] [d/l] |
Am adding just a "kobo" suggestion, using your OP and the modification provided by ww to your script.
Anonymous Monk says:
"..D:\Prog\Perls is the location where I execute my code.."
In that case, you can get your desired result using chdir. {Updated} Like so:
#!/usr/bin/perl
use warnings;
use strict;
use File::Spec;
my $dirname = "C:\\Users\\Me\\Desktop\\Cluster1";
my @file_to_use_later; ## for use later
chdir $dirname; ## add this to OP script
opendir DIR, $dirname or die "can't open directory: $!";
my @FILES = readdir(DIR);
foreach my $FILE (@FILES) {
my $fil_path = File::Spec->rel2abs($FILE);
print "File path:", $fil_path, $/;
push @file_to_use_later, $fil_path if -f $fil_path; ## check fi
+les contents later
}
closedir DIR or die "can't close directory: $!";
## test the files stored in the array variable later
for my $filename (@file_to_use_later) {
print $filename, $/;
open my $fh, '<', $filename or die "can't open: $!";
while (<$fh>) {
chomp;
print $_, $/;
}
close $fh or die "can't close file:$!";
}
Also, take note of this important info from File::Spec documentation about the "rel2abs".
rel2abs()
Converts a relative path to an absolute path.
$abs_path = File::Spec->rel2abs( $path ) ;
$abs_path = File::Spec->rel2abs( $path, $base ) ;
If $base is not present or '', then Cwd is used. If $base is relative, then it is converted to absolute form using rel2abs(). This means that it is taken to be relative to Cwd.
On systems with the concept of volume, if $path and $base appear to be on two different volumes, we will not attempt to resolve the two paths, and we will instead simply return $path .
The above explain, why you are getting this:
D:\Prog\Perls\doc.txt
D:\Prog\Perls\volt.txt
D:\Prog\Perls\holiday.txt
instead of this
C:\Users\Me\Desktop\Cluster1\doc.txt
C:\Users\Me\Desktop\Cluster1\volt.txt
C:\Users\Me\Desktop\Cluster1\holiday.txt
Update:
You may also want to look into the module Path::Class and it's "cousins". Path::Class 'just' is a "wrapper" for File::Spec.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [Watch: Dir/Any] [d/l] [select] |