I am using File::Find::Rule
on Strawberry Perl Windows.
when I run the following code:
@files = File::Find::Rule->file()
->in( $dir );
foreach my $file (@files){
say $file;
}
I get the list of files in this format:
C:\data\mydata\file/1.xls
and not this format:
C:\data\mydata\file\1.xls
What could be the problem?
The file separator is the character used to separate the directory names that make up the path to a specific location. 2.1. Get the File Separator There are several ways to get the file separator in Java. We can get the separator as a String using File.separator: We can also get this separator as a char with File.separatorChar:
The file separator is the character used to separate the directory names that make up the path to a specific location. The output will depend on the host operating system. The path separator is a character commonly used by the operating system to separate individual paths in a list of paths
To make our program platform independent, we should always use these separators to create file path or read any system variables like PATH, CLASSPATH. Here is the code snippet showing how to use separators correctly. Show activity on this post. You use separator when you are building a file path. So in unix the separator is /.
You use a ; to separate the file paths so on Windows File.pathSeparator would be ;. File.separator is either / or \ that is used to split up the path to a specific file. For example on Windows it is \ or C:\Documents\Test
The only problem is your expectations. C:\data\mydata\file/1.xls
is a perfectly valid Windows path.
File::Spec can normalize the path for you.
use File::Spec::Functions qw( canonpath );
$path = canonpath($path);
or
use File::Spec::Functions qw( canonpath );
@files = map { canonpath($_) } @files;
The cause is probably manual concatenation of the dir and the filename. You can fix it using File::Spec:
use File::Spec;
my @files = File::Find::Rule->file()->in( $dir );
foreach my $file (@files){
say File::Spec->catfile($file);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With