How to handle filenames with spaces?

前端 未结 7 767
后悔当初
后悔当初 2020-12-11 03:37

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 syste

相关标签:
7条回答
  • 2020-12-11 04:15

    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.

    0 讨论(0)
  • 2020-12-11 04:21

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

    Try

    "\"$_\""
    

    and

    "\"$outfile\""
    
    0 讨论(0)
  • 2020-12-11 04:26

    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");
    
    0 讨论(0)
  • 2020-12-11 04:29

    The built in "rename" command also moves files:

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

    I use this on windows all the time.

    0 讨论(0)
  • 2020-12-11 04:30

    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;
    
    0 讨论(0)
  • 2020-12-11 04:33

    $filename =~ s/\ /\ /;

    what ever the filename is just use slash to refrence spaces

    0 讨论(0)
提交回复
热议问题