Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle filenames with spaces?

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

like image 716
goldenmean Avatar asked Nov 26 '10 14:11

goldenmean


People also ask

Are spaces allowed in filenames?

Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

How do you cat a file with a space in the name?

Another way to deal with spaces and special characters in a file name is to escape the characters. You put a backslash ( \ ) in front of the special character or space.

How do you handle a space in a batch file?

When you send arguments, those with poison or space characters need to be doublequoted. Inside your batch file, if you no longer need the surrounding doublequotes, you'd remove them by using %~5 instead of %5 . Additionally the recommended syntax for the set command is Set "VariableName=VariableValue" .

How do you use file names with spaces in Linux?

Basically, you put a \ before every space in the filename. You could also use single quotes instead of double quotes. Single quotes ignore any special characters. Double quotes ignores all except $, back quotes and baclslashes.


2 Answers

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.

like image 54
Ian C. Avatar answered Sep 19 '22 08:09

Ian C.


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

Try

"\"$_\""

and

"\"$outfile\""
like image 34
Ed Guiness Avatar answered Sep 22 '22 08:09

Ed Guiness