如何处理带空格的文件名?

发布于 2024-10-04 12:59:55 字数 917 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

萌能量女王 2024-10-11 12:59:56

$文件名=~s/\ /\ /;

无论文件名是什么,只需使用斜杠来引用空格

$filename =~ s/\ /\ /;

what ever the filename is just use slash to refrence spaces

倾城泪 2024-10-11 12:59:56

内置的“重命名”命令还可以移动文件:

    rename $source, $destination;   # ...and move

我一直在 Windows 上使用它。

The built in "rename" command also moves files:

    rename $source, $destination;   # ...and move

I use this on windows all the time.

你的呼吸 2024-10-11 12:59:55

停止使用 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.

杀お生予夺 2024-10-11 12:59:55

您的代码不会在文件名周围添加任何引号。

尝试

"\"$_\""

"\"$outfile\""

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

Try

"\"$_\""

and

"\"$outfile\""
骄傲 2024-10-11 12:59:55

system 很少是正确的答案,使用 File::Copy ;

连接所有文件:

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;

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;
明天过后 2024-10-11 12:59:55

当您尝试访问内部块内的变量 $_ 时,可能会出现问题。最安全的方法是将:更改

foreach (@files)

为:

foreach $file (@files)

然后对@args进行必要的更改,并转义双引号以将它们包含在字符串中。

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

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");
请帮我爱他 2024-10-11 12:59:55

在 Windows 中,您通常可以在文件名(和/或路径)两边加上双引号,以允许特殊字符,即“长文件名”。

C:\"我的长路径\这是一个文件.mp3"

编辑:

这不起作用吗?

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

(注意字符串中的双引号而不是单引号)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文