windows 批量重命名文件扩展名,包括子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1025470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:31:53  来源:igfitidea点击:

Batch rename file extensions, including subdirectories

windowscommand-line

提问by Glenn

I'm renaming empty file extensions with this command:

我正在使用以下命令重命名空文件扩展名:

rename *. *.bla

However, I have a folder with hundreds of such subfolders, and this command requires me to manually navigate to each subfolder and run it.

但是,我有一个包含数百个此类子文件夹的文件夹,此命令要求我手动导航到每个子文件夹并运行它。

Is there a command that I can run from just one upper level folder that will include all the files in the subfolders?

是否有一个命令可以只从一个上层文件夹运行,该文件夹将包含子文件夹中的所有文件?

采纳答案by Glenn

@echo off
for /f "tokens=* delims= " %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)

Thanks @Wadih M. Find and rename files with no extension?

谢谢@Wadih M。查找和重命名没有扩展名的文件?

回答by k3beast

for /r c:\path\ %G in (*.) do ren "%G" *.bla

回答by Alan Haggai Alavi

You can easily do this and many more things with the Perl module File::Find.

您可以使用 Perl 模块File::Find轻松完成此操作以及更多操作。

#!perl

use strict;
use warnings;
use File::Find;

my $extension = 'bla';
my $directory = '/tmp/test';

print "Files renamed:\n";
find( \&wanted, $directory );

sub wanted {
    return if /\./;
    return unless -f $File::Find::name;

    if ( rename( $File::Find::name, "$File::Find::name.$extension" ) ) {
        print "    $File::Find::name -> $File::Find::name.$extension\n";
    }
    else {
        print "    Error: $! - $File::Find::name\n";
    }
}

回答by akf

this will allow you to enter dirs with spaces in the names. (note the double % is for batch files, use a single % for command lines.)

这将允许您输入名称中带有空格的目录。(请注意,双 % 用于批处理文件,命令行使用单个 %。)

 for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*." "*.bla"

回答by Rihan Meij

Try this

尝试这个

for /R c:\temp %I in (*. ) DO Echo rename %I "%~nI.bla"

回答by Joey

You can use forto iterate over subdirectories:

您可以使用for迭代子目录:

for /d %x in (*) do pushd %x & ren *. *.bla & popd

When using it from a batch file you would need to double the %signs:

从批处理文件中使用它时,您需要将%符号加倍:

for /d %%x in (*) do pushd %%x & ren *. *.bla & popd

回答by tomyjwu

Using find and xargs and basename would make the expression easier to read

使用 find 和 xargs 和 basename 会使表达式更易于阅读

Prepare a rename shell script like this (corrected to handle multiple files)

准备一个像这样的重命名 shell 脚本(已更正以处理多个文件)

#!/bin/sh
if [ $# -lt 3 ] 
then
    echo "usage: rename.sh sufold sufnew file1 (file2 file3 ...) "
    exit
fi

sufold=
sufnew=
shift
shift

for f in $*
do
  echo "to mv " $f `dirname $f`"/"`basename $f $sufold`$sufnew
  mv $f `dirname $f`"/"`basename $f $sufold`$sufnew
done

Then you could invoke that to each file matched your criteria, including recursive folder search by find command

然后你可以调用它来匹配你的条件的每个文件,包括通过 find 命令进行递归文件夹搜索

find . -name "*." | xargs rename.sh "." ".bla"

A more common sample

更常见的样本

find . -name "*.htm" | xargs rename.sh ".htm" ".html"