Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File::Find::Rule and file separator

Tags:

perl

file-find

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?

like image 626
smith Avatar asked Jan 03 '14 13:01

smith


People also ask

How to get file separator in Java?

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:

What is the difference between File Separator and path separator?

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

How to use separators correctly to make your program platform independent?

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

How do I separate the file paths?

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


Video Answer


2 Answers

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;
like image 95
ikegami Avatar answered Oct 22 '22 13:10

ikegami


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);
}
like image 35
roland.minner Avatar answered Oct 22 '22 12:10

roland.minner