How to handle filenames with spaces?

梦想的初衷 提交于 2019-11-27 06:49:27

问题


I use Perl on windows(Active Perl). I have a perl program to glob the files in current folder, and concatenate them all using dos copy command called from within using system()...

When i execute, this gives a dos error saying "The system cannot find the file specified." It's related to the spaces in the filenames I have.

This is the perl code :-

@files = glob "*.mp3";
$outfile = 'final.mp3';
$firsttime = 1;
foreach (@files)
{

    if($firsttime == 1)
    {
       @args = ('copy' ,"/b ","$_","+","$outfile", "$outfile");
       system (@args);
       #system("copy /b '$_'+$outfile $outfile"); 
       $firsttime = 0;  
    }
    else
    {
       @args = ('copy' ,"/b ","$outfile","+","$_", "$outfile");
       system (@args);
       #system("copy /b $outfile+'$_' $outfile"); 
    }

}

glob returns a array of filenames in my current folder, Those file names have spaces in between them, so the array elements have spaces in between. When i use the system(...) to execute my copy command on those array elements using "$_" as shown above, it gives error as above.

I tried couple of ways in which I could call the system(...) but without any success.

I would like to know,

1] How can i get this working on files which have spaces in between them using the code above. How to 'escape' the white space in file names.

2] Any alternative solution in Perl to achieve the same thing. (Simple ones welcome..)


回答1:


Your code doesn't add any quotes around the filenames.

Try

"\"$_\""

and

"\"$outfile\""



回答2:


Stop using system() to make a call that can be done with a portable library. Perl has a the File::Copy module, use that instead and you don't have to worry about things like this plus you get much better OS portability.




回答3:


system is rarely the right answer, use File::Copy;

To concatenate all files:

use File::Copy;
my @in = glob "*.mp3";
my $out = "final.mp3";

open my $outh, ">", $out;
for my $file (@in) {
    next if $file eq $out;
    copy($file, $outh);
}
close $outh;



回答4:


Issues may arise when you're trying to access the variable $_ inside an inner block. The safest way, change:

foreach (@files)

to:

foreach $file (@files)

Then do the necessary changes on @args, and escape doublequotes to include them in the string..

@args = ('copy' ,"/b ","\"$file\"","+","$outfile", "$outfile");
...
@args = ('copy' ,"/b ","$outfile","+","\"$file\"", "$outfile");



回答5:


In windows you can normally put double quotes around the filenames (and/or paths) allowing special chars i.e "long file names".

C:\"my long path\this is a file.mp3"

Edit:

Does this not work?

system("copy /b \"$_\"+$outfile $outfile");

(NOTE THE DOUBLE quotes within the string not single quotes)




回答6:


$filename =~ s/\ /\ /;

what ever the filename is just use slash to refrence spaces



来源:https://stackoverflow.com/questions/4285989/how-to-handle-filenames-with-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!