4

如何使用 Perl 将具有 .wma 和 .wmv 扩展名的驱动器上的所有文件重命名为 .txt 扩展名,无论它们在目录结构中的深度如何?

4

7 回答 7

11

请参阅perldoc 文件::查找。文档中的示例非常不言自明,可以帮助您了解大部分情况。当您尝试时,请使用更多信息更新问题。

如果这是一个学习练习,那么首先尝试自己做会学得更好。

更新:

假设您有机会自己研究如何做到这一点,并考虑到已经发布了各种解决方案的事实,我将发布我将如何做到这一点。请注意,我会选择忽略诸如“.wmv”之类的文件:我的正则表达式需要在点之前出现一些内容。

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

my ($dir) = @ARGV;

find( \&wanted, $dir );

sub wanted {
    return unless -f;
    return unless /^(.+)\.wm[av]$/i;
    my $new = "$1.txt";
    rename $_ => $new
        or warn "'$_' => '$new' failed: $!\n";
    return;
}

__END__
于 2009-06-26T12:02:56.337 回答
3
#!/usr/bin/perl

使用严格;
使用警告;
使用文件::查找;

我的 $dir = '/path/to/dir';

文件::查找::查找(
    子{
        我的 $file = $_;
        如果 -d $file; 返回
        返回 if $file !~ /(.*)\.wm[av]$/;
        重命名 $file, "$1.txt" 或死 $!;
    }, $目录
);
于 2009-06-26T20:02:28.977 回答
2

如果您是新手,还有一条更有用的建议:要重命名文件,请使用“File::Copy”模块中的“move()”方法(并始终检查 move() 是否失败)

此外,避免意外重命名以 .wma/.wmv 结尾的目录的不明显错误(因为在文件和目录上都调用了“wanted”回调)

PS 我绝对同意上面的 File::Find 建议(另外,请考虑查看 File::Find::Rule,如此链接中所述)。然而,作为学习 Perl 的练习,如果你的目标是学习而不是仅仅写一个快速的文件,那么你可能会考虑编写自己的递归文件查找器(或者更好的是,将其从递归转换为广度优先搜索循环)一次性。

于 2009-06-26T12:23:54.407 回答
1
find . -name '*.wm[va]' -a -type f -exec mv '{}' '{}.txt' \;

好的,上面有两个基本问题。首先,它是 find,而不是 perl。其次,它实际上只是将 .txt 放在最后,而不是您想要的。

如果您真的必须在 perl 中执行此操作,那么第一个问题只是一个问题。这可能意味着您只是在学习 perl,但这没关系,因为这只是第一步。如果您只想完成工作而不关心语言,则第二个问题只是一个问题。我先解决第二个问题:

find . -name '*.wm[va]' -a -type f | while read f; do mv $f ${f%.*}; done

这只是完成了工作,但实际上使我们远离了 perl 解决方案。那是因为,如果您在 find 中完成所有操作,您可以使用 find2perl 转换为 perl:

find . -name '*.wm[va]' -a -type f -exec mv '{}' '{}.txt' \;

这将打印出一个 perl 脚本,您可以保存它:

find2perl . -name '*.wm[va]' -a -type f -exec mv '{}' '{}.txt' \; > my.pl

它包括一个 doexec() 函数,可以对其进行修改以执行您想要的操作。首先是将第二个参数更改为正确的名称(使用File::Basename的 basename 函数: basename($command[2], qw/.wmv .wma/) ),第二个只是消除调用到系统,STDOUT munging 等,只需调用重命名。但这至少给了你一个开始。

于 2009-06-26T16:17:44.377 回答
0
# include the File::Find module, that can be used to traverse directories 
use File::Find;

# starting in the current directory, tranverse the directory, calling
# the subroutine "wanted" on each entry (see man File::Find)
find(\&wanted, ".");

sub wanted
{
    if (-f and
        /.wm[av]$/)
    {
        # when this subroutine is called, $_ will contain the name of
        # the directory entry, and the script will have chdir()ed to
        # the containing directory. If we are looking at a file with
        # the wanted extension - then rename it (warning if it fails).
        my $new_name = $_;
        $new_name =~ s/\.wm[av]$/.txt/;
        rename($_, $new_name) or
            warn("rename($_, $new_name) failed - $!");
    }
}
于 2009-06-26T16:23:20.477 回答
0

I had to do something similar recently. This script would require modification, but has all the essentials:

  1. It recurses through files and directories (sub recurse).
  2. It has a function to act on directories (processDir) and a separate one to act on files (processFile).
  3. It handles spaces in file names using an alternate version of the glob function from File::Glob.
  4. It performs no actions, but instead writes an output file (CSV, TAB or perl script) so that the user can review proposed changes before making a big mistake.
  5. It outputs partial results periodically, which is useful if your system goes down part way.
  6. It proceeds in depth first order. This is important, because if you have a script that modifies (renames or moves) a parent directory before processing the subdirectories and files, bad things can happen.
  7. It reads from a skip list file, which allows you to avoid huge directories and mounted volumes that you do not want to visit.
  8. It does not follow symbolic links, which often cause circularities.

A small modification to processFile is most of what you would need to do, plus gutting the features you don't need. (This script was designed to look for files with characters in their names not supported on Windows.)

NOTE: At the end it calls "open", which on the MAC will open the resulting file in its default application. On Windows, use "start". On other Unix systems, there are similar commands.

#!/usr/bin/perl -w

# 06/04/2009. PAC. Fixed bug in processDir. Was using $path instead of $dir when forming newpath.

use strict;
use File::Glob ':glob'; # This glob allows spaces in filenames. The default one does not.

sub recurse(&$);
sub processFile($);
sub stem($);
sub processXMLFile($);
sub readFile($);
sub writeFile($$);
sub writeResults($);
sub openFileInApplication($);

if (scalar @ARGV < 4) {
    print <<HELP_TEXT;

    Purpose: Report on files and directories whose names violate policy by:
                   o containing illegal characters
                   o being too long
                   o beginning or ending with certain characters

    Usage:   perl EnforceFileNamePolicy.pl root-path skip-list format output-file 

        root-path .... Recursively process all files and subdirectories starting with this directory.
        skip-list .... Name of file with directories to skip, one to a line.
        format ....... Output format:
                            tab = tab delimited list of current and proposed file names
                            csv = comma separated list of current and proposed file names
                            perl = perl script to do the renaming
        output-file .. Name of file to hold results.

    Output:  A script or delimited file that will rename the offending files and directories is printed to output-file.
             As directories are processed or problems found, diagnostic messages will be printed to STDOUT.

    Note: Symbolic links are not followed, otherwise infinite recursion would result.
    Note: Directories are processed in depth-first, case-insensitive alphabetical order. 
    Note: If \$CHECKPOINT_FREQUENCY > 0, partial results will be written to intermediate files periodically.
          This is useful if you need to kill the process before it completes and do not want to lose all your work.

HELP_TEXT
  exit;
}


########################################################
#                                                      #
#                 CONFIGURABLE OPTIONS                 #
#                                                      #
########################################################

my $BAD_CHARACTERS_CLASS = "[/\\?<>:*|\"]";
my $BAD_SUFFIX_CLASS = "[. ]\$";
my $BAD_PREFIX_CLASS = "^[ ]";
my $REPLACEMENT_CHAR = "_";
my $MAX_PATH_LENGTH = 256;
my $WARN_PATH_LENGTH = 250;
my $LOG_PATH_DEPTH = 4; # How many directories down we go when logging the current directory being processed.
my $CHECKPOINT_FREQUENCY = 20000; # After an integral multiple of this number of directories are processed, write a partial results file in case we later kill the process.

########################################################
#                                                      #
#                COMMAND LINE ARGUMENTS                #
#                                                      #
########################################################

my $rootDir = $ARGV[0];
my $skiplistFile = $ARGV[1];
my $outputFormat = $ARGV[2];
my $outputFile = $ARGV[3];


########################################################
#                                                      #
#                BEGIN PROCESSING                      #
#                                                      #
########################################################

my %pathChanges = (); # Old name to new name, woth path attached.
my %reasons = ();
my %skip = (); # Directories to skip, as read from the skip file.
my $dirsProcessed = 0;

# Load the skiplist
my $skiplist = readFile($skiplistFile);
foreach my $skipentry (split(/\n/, $skiplist)) {
    $skip{$skipentry} = 1;  
}

# Find all improper path names under directory and store in %pathChanges.
recurse(\&processFile, $rootDir);

# Write the output file.
writeResults(0);
print "DONE!\n";

# Open results in an editor for review.
#WARNING: If your default application for opening perl files is the perl exe itself, this will run the otput perl script!
#         Thus, you may want to comment this out.
#         Better yet: associate a text editor with the perl script.
openFileInApplication($outputFile);

exit;


sub recurse(&$) {
    my($func, $path) = @_;
    if ($path eq '') {
        $path = ".";
    }

    ## append a trailing / if it's not there
    $path .= '/' if($path !~ /\/$/);

    ## loop through the files contained in the directory
    for my $eachFile (sort { lc($a) cmp lc($b)  } glob($path.'*')) {
        # If eachFile has a shorter name and is a prefix of $path, then stop recursing. We must have traversed "..".
        if (length($eachFile) > length($path) || substr($path, 0, length($eachFile)) ne $eachFile) {
            ## if the file is a directory
            my $skipFile = defined $skip{$eachFile};
            if( -d $eachFile && ! -l $eachFile && ! $skipFile) { # Do not process symbolic links like directories! Otherwise, this will never complete - many circularities.
                my $depth = depthFromRoot($eachFile);
                if ($depth <= $LOG_PATH_DEPTH) {
                    # Printing every directory as we process it slows the program and does not give the user an intelligible measure of progress.
                    # So we only go so deep in printing directory names.
                    print "Processing: $eachFile\n";
                }

                ## pass the directory to the routine ( recursion )
                recurse(\&$func, $eachFile);

                # Process the directory AFTER its children to force strict depth-first order.
                processDir($eachFile);
            } else {
                if ($skipFile) {
                    print "Skipping: $eachFile\n";
                }

                # Process file.
                &$func($eachFile);
            }           
        }

    }
}


sub processDir($) {
    my ($path) = @_;
    my $newpath = $path;    
    my $dir;
    my $file;
    if ($path eq "/") {
        return; 
    }
    elsif ($path =~ m|^(.*/)([^/]+)$|) {
        ($dir, $file) = ($1, $2);
    }
    else {
        # This path has no slashes, hence must be the root directory.
        $file = $path;
        $dir = '';
    }
    if ($file =~ /$BAD_CHARACTERS_CLASS/) {
        $file =~ s/($BAD_CHARACTERS_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectDir($path, $newpath, "Illegal character in directory.");
    }
    elsif ($file =~ /$BAD_SUFFIX_CLASS/) {
        $file =~ s/($BAD_SUFFIX_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectDir($path, $newpath, "Illegal character at end of directory.");
    }
    elsif ($file =~ /$BAD_PREFIX_CLASS/) {
        $file =~ s/($BAD_PREFIX_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectDir($path, $newpath, "Illegal character at start of directory.");
    }
    elsif (length($path) >= $MAX_PATH_LENGTH) {
        rejectDir($path, $newpath, "Directory name length > $MAX_PATH_LENGTH.");
    }
    elsif (length($path) >= $WARN_PATH_LENGTH) {
        rejectDir($path, $newpath, "Warning: Directory name length > $WARN_PATH_LENGTH.");
    }
    $dirsProcessed++;
    if ($CHECKPOINT_FREQUENCY > 0 && $dirsProcessed % $CHECKPOINT_FREQUENCY == 0) {
        writeResults(1);
    }
}

sub processFile($) {
    my ($path) = @_;
    my $newpath = $path;
    $path =~ m|^(.*/)([^/]+)$|;
    my ($dir, $file) = ($1, $2);
    if (! defined ($file) || $file eq '') {
        $file = $path;
    }
    if ($file =~ /$BAD_CHARACTERS_CLASS/) {
        $file =~ s/($BAD_CHARACTERS_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectFile($path, $newpath, "Illegal character in filename.");
    }
    elsif ($file =~ /$BAD_SUFFIX_CLASS/) {
        $file =~ s/($BAD_SUFFIX_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectFile($path, $newpath, "Illegal character at end of filename.");
    }
    elsif ($file =~ /$BAD_PREFIX_CLASS/) {
        $file =~ s/($BAD_PREFIX_CLASS)/$REPLACEMENT_CHAR/g;
        $newpath = $dir . $file;
        rejectFile($path, $newpath, "Illegal character at start of filename.");
    }
    elsif (length($path) >= $MAX_PATH_LENGTH) {
        rejectFile($path, $newpath, "File name length > $MAX_PATH_LENGTH.");
    }
    elsif (length($path) >= $WARN_PATH_LENGTH) {
        rejectFile($path, $newpath, "Warning: File name length > $WARN_PATH_LENGTH.");
    }

}

sub rejectDir($$$) {
    my ($oldName, $newName, $reason) = @_;
    $pathChanges{$oldName} = $newName;
    $reasons{$oldName} = $reason;
    print "Reason: $reason  Dir: $oldName\n";
}

sub rejectFile($$$) {
    my ($oldName, $newName, $reason) = @_;
    $pathChanges{$oldName} = $newName;
    $reasons{$oldName} = $reason;
    print "Reason: $reason  File: $oldName\n";
}


sub readFile($) {
    my ($filename) = @_;
    my $contents;
    if (-e $filename) {
        # This is magic: it opens and reads a file into a scalar in one line of code. 
        # See http://www.perl.com/pub/a/2003/11/21/slurp.html
        $contents = do { local( @ARGV, $/ ) = $filename ; <> } ; 
    }
    else {
        $contents = '';
    }
    return $contents;
}

sub writeFile($$) {
    my( $file_name, $text ) = @_;
    open( my $fh, ">$file_name" ) || die "Can't create $file_name $!" ;
    print $fh $text ;
}   

# writeResults() - Compose results in the appropriate format: perl script, tab delimited, or comma delimited, then write to output file.
sub writeResults($) {
    my ($checkpoint) = @_;
    my $outputText = ''; 
    my $outputFileToUse;
    my $checkpointMessage;
    if ($checkpoint) {
        $checkpointMessage = "$dirsProcessed directories processed so far.";
    }
    else {
        $checkpointMessage = "$dirsProcessed TOTAL directories processed.";
    }
    if ($outputFormat eq 'tab') {
            $outputText .= "Reason\tOld name\tNew name\n";
            $outputText .= "$checkpointMessage\t\t\n";
    }
    elsif ($outputFormat eq 'csv') {
            $outputText .= "Reason,Old name,New name\n";
            $outputText .= "$checkpointMessage,,\n";
    }
    elsif ($outputFormat eq 'perl') {
        $outputText = <<END_PERL;
#/usr/bin/perl

# $checkpointMessage
#
# Rename files and directories with bad names.
# If the reason is that the filename is too long, you must hand edit this script and choose a suitable, shorter new name.

END_PERL
    }

    foreach my $file (sort  { 
        my $shortLength = length($a) > length($b) ? length($b) : length($a); 
        my $prefixA = substr($a, 0, $shortLength);
        my $prefixB = substr($b, 0, $shortLength); 
        if ($prefixA eq $prefixB) {
            return $prefixA eq $a ? 1 : -1; # If one path is a prefix of the other, the longer path must sort first. We must process subdirectories before their parent directories.
        }
        else {
            return $a cmp $b;
        }
    } keys %pathChanges) {
        my $changedName = $pathChanges{$file};
        my $reason = $reasons{$file};
        if ($outputFormat eq 'tab') {
            $outputText .= "$reason\t$file\t$changedName\n";
        }
        elsif ($outputFormat eq 'csv') {
            $outputText .= "$reason,$file,$changedName\n";
        }
        else {
            # Escape the spaces so the mv command works.
            $file =~ s/ /\\ /g;
            $changedName =~ s/ /\\ /g;
            $outputText .= "#$reason\nrename \"$file\", \"$changedName\"\n";        
        }
    }
    $outputFileToUse = $outputFile;
    if ($checkpoint) {
        $outputFileToUse =~ s/(^.*)([.][^.]+$)/$1-$dirsProcessed$2/;
    }

    writeFile($outputFileToUse, $outputText);
}

# Compute how many directories deep the given path is below the root for this script.
sub depthFromRoot($) {
    my ($dir) = @_;
    $dir =~ s/\Q$rootDir\E//;
    my $count = 1;
    for (my $i = 0; $i < length($dir); $i++) {
        if (substr($dir, $i, 1) eq "/") { $count ++; }
    }
    return $count;
}

#openFileInApplication($filename) - Open the file in its default application.
#
# TODO: Must be changed for WINDOWS. Use 'start' instead of 'open'??? 
sub openFileInApplication($) {
    my ($filename) = @_;
    `open $filename`;
}
于 2009-06-26T13:46:20.200 回答
0

看重命名

find -type f -name '*.wm?' -print0 | xargs -0 rename 's/\.wm[av]$/.txt/'

或者

find -type f -name '*.wm?' -exec rename 's/\.wm[av]$/.txt/' {} +

或者制作自己的脚本

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

find( sub {
    return unless -f;
    my $new = $_;
    return unless $new =~ s/\.wm[av]$/.txt/;
    rename $_ => $new
        or warn "rename '$_' => '$new' failed: $!\n";
  }, @ARGV );
于 2009-06-26T14:15:32.933 回答