如何处理带空格的文件名?
我在 Windows 上使用 Perl(Active Perl)。我有一个 perl 程序来遍历当前文件夹中的文件,并使用从 system() 内部调用的 dos copy 命令将它们全部连接起来...
当我执行时,这会给出一个 dos 错误,提示“系统找不到指定的文件。 ”这与我的文件名中的空格有关。
这是 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(...) 使用“$_”对这些数组元素执行复制命令(如上所示)时,它会给出如上所述的错误。
我尝试了几种可以调用系统(...)的方法,但没有成功。
我想知道,
1] 如何使用上面的代码在文件之间有空格的文件上进行此操作。如何“转义”文件名中的空格。
2] Perl 中实现相同目标的任何替代解决方案。 (简单的欢迎..)
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..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
$文件名=~s/\ /\ /;
无论文件名是什么,只需使用斜杠来引用空格
$filename =~ s/\ /\ /;
what ever the filename is just use slash to refrence spaces
内置的“重命名”命令还可以移动文件:
我一直在 Windows 上使用它。
The built in "rename" command also moves files:
I use this on windows all the time.
停止使用
system()
进行可以通过可移植库完成的调用。 Perl 有一个 File::Copy 模块,使用它来代替,你不必担心类似的事情再加上您可以获得更好的操作系统可移植性。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.您的代码不会在文件名周围添加任何引号。
尝试
并
Your code doesn't add any quotes around the filenames.
Try
and
system
很少是正确的答案,使用 File::Copy ;
连接所有文件:
system
is rarely the right answer,use File::Copy;
To concatenate all files:
当您尝试访问内部块内的变量
$_
时,可能会出现问题。最安全的方法是将:更改为:
然后对@args进行必要的更改,并转义双引号以将它们包含在字符串中。
Issues may arise when you're trying to access the variable
$_
inside an inner block. The safest way, change:to:
Then do the necessary changes on
@args
, and escape doublequotes to include them in the string..在 Windows 中,您通常可以在文件名(和/或路径)两边加上双引号,以允许特殊字符,即“长文件名”。
C:\"我的长路径\这是一个文件.mp3"
编辑:
这不起作用吗?
(注意字符串中的双引号而不是单引号)
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?
(NOTE THE DOUBLE quotes within the string not single quotes)