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

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

问题描述

我在 Windows 上使用 Perl(Active Perl).我有一个 perl 程序来对当前文件夹中的文件进行 glob,并使用从内部调用的 dos copy 命令将它们全部连接起来使用 system()...

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天全站免登陆