如何使用空格处理文件名? [英] How to handle filenames with spaces?

查看:130
本文介绍了如何使用空格处理文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows(Active Perl)上使用Perl.我有一个perl程序来遍历当前文件夹中的文件,并使用在system()中调用的dos copy命令将它们全部串联起来.

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

执行时,出现dos错误,提示系统找不到指定的文件".这与我拥有的文件名中的空格有关.

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.

这是perl代码:-

@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在我当前的文件夹中返回一个文件名数组,这些文件名之间有空格,因此数组元素之间有空格.如上所述,当我使用system(...)对使用"$ _"的那些数组元素执行复制命令时,会出现上述错误.

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.

我想知道,

1]如何使用上面的代码在文件之间有空格的文件上使用此功能.如何转义"文件名中的空白.

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] Perl中的任何替代解决方案都可以实现相同的目的. (欢迎简单的人..)

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

推荐答案

您的代码未在文件名周围添加引号.

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

尝试

"\"$_\""

"\"$outfile\""

这篇关于如何使用空格处理文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆