用文件夹名称重命名文件的脚本?

发布于 2024-12-18 21:17:35 字数 105 浏览 0 评论 0原文

我想通过在文件前面添加文件夹名称来递归重命名文件。例如:c:\test\foo\a.txt 将变为 c:\test\foo\foo-a.txt

I want to recursively rename files by prepending the folder name in front of it. Ex: c:\test\foo\a.txt would become c:\test\foo\foo-a.txt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

最冷一天 2024-12-25 21:17:35

以下 Perl 脚本可能适合您:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Basename;
use File::Find;
use File::Spec;

sub rename {
    my ($dir_name) = ( File::Spec->splitdir($File::Find::dir) )[-1];
    my $file_name = basename $_;
    if ( -f $_ ) {
        $file_name = "$dir_name-$file_name";
        rename $_, File::Spec->catdir( $File::Find::dir, $file_name );
    }
}

find { 'wanted' => \&rename, 'no_chdir' => 1 }, 'C:/test/foo';

参考:

The following Perl script may work for you:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Basename;
use File::Find;
use File::Spec;

sub rename {
    my ($dir_name) = ( File::Spec->splitdir($File::Find::dir) )[-1];
    my $file_name = basename $_;
    if ( -f $_ ) {
        $file_name = "$dir_name-$file_name";
        rename $_, File::Spec->catdir( $File::Find::dir, $file_name );
    }
}

find { 'wanted' => \&rename, 'no_chdir' => 1 }, 'C:/test/foo';

References:

反话 2024-12-25 21:17:35
use File::Find::Rule qw( );
use Path::Class      qw( dir file );

my $base = dir('.')->absolute;
for my $qfn (File::Find::Rule->file->in($base)) {
   my $file = file($qfn);
   my $dir  = $file->dir;

   my $src = $file;
   my $dst = $dir->file($dir->basename . '-' . $file->basename);

   if (-e $dst) {
      warn("Can't rename $src to $dst: Already exists\n");
   } 
   elsif (!rename($src, $dst)) {
      warn("Can't rename $src to $dst: $!\n");
   } 
}
use File::Find::Rule qw( );
use Path::Class      qw( dir file );

my $base = dir('.')->absolute;
for my $qfn (File::Find::Rule->file->in($base)) {
   my $file = file($qfn);
   my $dir  = $file->dir;

   my $src = $file;
   my $dst = $dir->file($dir->basename . '-' . $file->basename);

   if (-e $dst) {
      warn("Can't rename $src to $dst: Already exists\n");
   } 
   elsif (!rename($src, $dst)) {
      warn("Can't rename $src to $dst: $!\n");
   } 
}
浮光之海 2024-12-25 21:17:35
use strict;
use warnings;

use File::Spec;
use File::Copy qw(move);
use File::Glob qw(:glob);

my $folder_path = qw( c:\test\foo\ );
my @file_paths  = bsd_glob( $folder_path . '*' );

foreach my $old_path (@file_paths) {
    if ( -f $old_path ) {
        my ( $volume, $directories, $file ) = File::Spec->splitpath($old_path);
        my @dirs = File::Spec->splitdir($directories);
        my $prepend;
        while ( !( $prepend = pop @dirs ) ) { } # see notes below
        my $new_fname = $prepend . '-' . $file;
        my $new_path = File::Spec->catpath( $volume, $directories, $new_fname );
        move( $old_path, $new_path );
    }
}

我使用 while 弹出文件名之前的最后一个目录名称,因为 splitdir 在 Windows 上有一个警告。看起来您正在处理 Windows 文件。

use strict;
use warnings;

use File::Spec;
use File::Copy qw(move);
use File::Glob qw(:glob);

my $folder_path = qw( c:\test\foo\ );
my @file_paths  = bsd_glob( $folder_path . '*' );

foreach my $old_path (@file_paths) {
    if ( -f $old_path ) {
        my ( $volume, $directories, $file ) = File::Spec->splitpath($old_path);
        my @dirs = File::Spec->splitdir($directories);
        my $prepend;
        while ( !( $prepend = pop @dirs ) ) { } # see notes below
        my $new_fname = $prepend . '-' . $file;
        my $new_path = File::Spec->catpath( $volume, $directories, $new_fname );
        move( $old_path, $new_path );
    }
}

I use while to pop the last directory name before the filename because splitdir has a caveat on Windows. It looks like you are dealing with a Windows file.

还如梦归 2024-12-25 21:17:35
    #!/usr/bin/perl

    use strict;
    use warnings;

    use File::Basename;
    use File::Find;
    use File::Spec;

    ($#ARGV == 0) or die "Usage: $0 [directory]\n"; 

    my $input_file_dir = $ARGV[0];

    sub process_file 
    {
      my $dir_name = (File::Spec -> splitdir ($File::Find::dir)) [-1];
      my $file_name = basename $_;
      my $extension = ($file_name =~ m/([^.]+)$/)[0];        
      if ( -f $_ ) 
      {
        print "$dir_name.$extension\n";
        rename $_, "$dir_name.$extension";
      }
    }

    finddepth { 'wanted' => \&process_file, 'no_chdir' => 0 }, $input_file_dir;
    #!/usr/bin/perl

    use strict;
    use warnings;

    use File::Basename;
    use File::Find;
    use File::Spec;

    ($#ARGV == 0) or die "Usage: $0 [directory]\n"; 

    my $input_file_dir = $ARGV[0];

    sub process_file 
    {
      my $dir_name = (File::Spec -> splitdir ($File::Find::dir)) [-1];
      my $file_name = basename $_;
      my $extension = ($file_name =~ m/([^.]+)$/)[0];        
      if ( -f $_ ) 
      {
        print "$dir_name.$extension\n";
        rename $_, "$dir_name.$extension";
      }
    }

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