windows 在cmd中重命名多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17271586/
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 10:37:43  来源:igfitidea点击:

Rename multiple files in cmd

windowsbatch-filecmdwildcardrename

提问by Morne

If I have multiple files in a directory and want to append something to their filenames, but not to the extension, how would I do this?

如果我在一个目录中有多个文件并想在它们的文件名中附加一些内容,而不是扩展名,我该怎么做?

I have tried the following, with test files file1.txtand file2.txt:

我已经尝试了以下测试文件file1.txtfile2.txt

ren *.txt *1.1.txt

This renames the files to file1.1.txtand file2.txt1.1.txt

这会将文件重命名为file1.1.txtfile2.txt1.1.txt

I want the files to be file1 1.1.txtand file2 1.1.txt

我希望文件是file1 1.1.txtfile2 1.1.txt

Will this be possible from cmd or do I need to have a bat file to do this? What about PowerShell?

这可以从 cmd 实现还是我需要一个 bat 文件才能做到这一点?PowerShell 呢?

采纳答案by Endoro

for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"

If you use the simple forloop without the /fparameter, already renamed files will be again renamed.

如果使用for不带/f参数的简单循环,已重命名的文件将再次重命名。

回答by dbenham

Make sure that there are more ?than there are characters in the longest name:

确保?最长名称中的字符多于字符数:

ren *.txt "???????????????????????????? 1.1.txt"

See How does the Windows RENAME command interpret wildcards?for more info.

请参阅Windows RENAME 命令如何解释通配符?了解更多信息。

New Solution - 2014/12/01

新解决方案 - 2014/12/01

For those who like regular expressions, there is JREN.BAT- a hybrid JScript/batch command line utility that will run on any version of Windows from XP forward.

对于那些喜欢正则表达式的人,有JREN.BAT- 一种混合 JScript/批处理命令行实用程序,可以在 XP 以后的任何版本的 Windows 上运行。

jren "^.*(?=\.)" "$& 1.1" /fm "*.txt"

or

或者

jren "^(.*)(\.txt)$" " 1.1" /i

回答by Ranjith Kumar

Step 1:

第1步:

Select all files (ctrl + A)

选择所有文件 (ctrl + A)

Step 2 :

第2步 :

Then choose rename option

然后选择重命名选项

enter image description here

在此处输入图片说明

Step 3:

第 3 步:

Choose your filename... for ex: myfile

选择您的文件名...例如:myfile

it automatically rename to myfile (01),myfile (02),,.....

它会自动重命名为myfile (01),myfile (02),,.....

If you want to replace spaces & bracket.. continue step 4

如果要替换空格和括号.. 继续第 4 步

Step 4:

第四步:

Open Windows Powershell from your current folder

从当前文件夹打开 Windows Powershell

enter image description here

在此处输入图片说明

Step 5:

第 5 步:

For replace empty space to underscore (_)

用于将空格替换为下划线 (_)

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape(" "),"_"}

Step 6:

第 6 步:

For replace open bracket

用于更换开式支架

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape("("),""}

For replace close bracket

用于更换右括号

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape(")"),""}

回答by Giri

The below command would do the job.

下面的命令可以完成这项工作。

forfiles /M *.txt /C "cmd /c rename @file \"@fname 1.1.txt\""

source: Rename file extensions in bulk

来源:批量重命名文件扩展名

回答by Eugene

@echo off
for %%f in (*.txt) do (
    ren "%%~nf%%~xf" "%%~nf 1.1%%~xf"
)

回答by Panama Hyman

Try this Bulk Rename UtilityIt works well. Maybe not reinvent the wheel. If you don't necessary need a script this is a good way to go.

试试这个批量重命名实用程序它运行良好。也许不会重新发明轮子。如果您不需要脚本,这是一个很好的方法。

回答by MJA

I tried pasting Endoro's command (Thanks Endoro) directly into the command prompt to add a prefix to files but encountered an error. Solution was to reduce %% to %, so:

我尝试将 Endoro 的命令(感谢 Endoro)直接粘贴到命令提示符中以向文件添加前缀,但遇到错误。解决方案是将 %% 减少到 %,因此:

for /f "delims=" %i in ('dir /b /a-d *.*') do ren "%~i" "Service.Enviro.%~ni%~xi"

回答by Juv

I found the following in a small comment in Supperuser.com:

我在 Supperuser.com 的一条小评论中发现了以下内容:

@HymansOnF1re - New information/technique added to my answer. You can actually delete your Copy of prefix using an obscure forward slash technique: ren "Copy of .txt" "////////"

@HymansOnF1re - 在我的回答中添加了新信息/技术。您实际上可以使用晦涩的正斜杠技术删除前缀副本: ren "Copy of .txt" "////////"

Of How does the Windows RENAME command interpret wildcards?See in this thread, the answerof dbenham.

如何在Windows RENAME命令通配符解释?请参阅本主题中,答案dbenham

My problem was slightly different, I wanted to add a Prefix to the file and remove from the beginning what I don't need. In my case I had several hundred of enumerated files such as:

我的问题略有不同,我想在文件中添加一个前缀并从一开始就删除我不需要的内容。就我而言,我有数百个枚举文件,例如:

SKMBT_C36019101512510_001.jpg
SKMBT_C36019101512510_002.jpg
SKMBT_C36019101512510_003.jpg
SKMBT_C36019101512510_004.jpg
:
:

Now I wanted to respectively rename them all to (Album 07 picture #):

现在我想分别将它们全部重命名为(专辑07图片#):

A07_P001.jpg
A07_P002.jpg
A07_P003.jpg
A07_P004.jpg
:
:

I did it with a single command line and it worked like charm:

我用一个命令行完成了它,它的作用就像魅力一样:

ren "SKMBT_C36019101512510_*.*" "/////////////////A06_P*.*"

Note:

笔记:

  1. Quoting (") the "<Name Scheme>"is not an option, it does not work otherwise, in our example: "SKMBT_C36019101512510_*.*"and "/////////////////A06_P*.*"were quoted.
  2. I had to exactly count the number of characters I want to remove and leave space for my new characters: The A06_Pactually replaced 2510_and the SKMBT_C3601910151was removed, by using exactly the number of slashes /////////////////(17 characters).
  3. I recommend copying your files (making a backup), before applying the above.
  1. 引用 ( ") the"<Name Scheme>"不是一个选项,否则它不起作用,在我们的示例中:"SKMBT_C36019101512510_*.*"并且"/////////////////A06_P*.*"被引用。
  2. 我必须精确计算我要删除的字符数,并为我的新字符留出空间:A06_P实际替换2510_SKMBT_C3601910151删除的,通过使用斜杠的数量/////////////////(17 个字符)。
  3. 我建议在应用上述内容之前复制您的文件(进行备份)。

回答by jfajunior

This works for your specific case:

这适用于您的特定情况:

ren file?.txt "file? 1.1.txt" 

回答by subcan

I was puzzled by this also... didn't like the parentheses that windows puts in when you rename in bulk. In my research I decided to write a script with PowerShell instead. Super easy and worked like a charm. Now I can use it whenever I need to batch process file renaming... which is frequent. I take hundreds of photos and the camera names them IMG1234.JPG etc...

我对此也感到困惑......不喜欢 Windows 在批量重命名时放入的括号。在我的研究中,我决定用 PowerShell 编写一个脚本。超级简单,工作起来很有魅力。现在我可以在需要批量处理文件重命名时使用它......这很常见。我拍了数百张照片,相机将它们命名为 IMG1234.JPG 等...

Here is the script I wrote:

这是我写的脚本:

# filename: bulk_file_rename.ps1
# by: subcan
# PowerShell script to rename multiple files within a folder to a 
# name that increments without (#)
# create counter
$int = 1
# ask user for what they want
$regex = Read-Host "Regex for files you are looking for? ex. IMG*.JPG "
$file_name = Read-Host "What is new file name, without extension? ex. New Image "
$extension = Read-Host "What extension do you want? ex. .JPG "
# get a total count of the files that meet regex
$total = Get-ChildItem -Filter $regex | measure
# while loop to rename all files with new name
while ($int -le $total.Count)
{
    # diplay where in loop you are
    Write-Host "within while loop" $int  
    # create variable for concatinated new name - 
    # $int.ToString(000) ensures 3 digit number 001, 010, etc
    $new_name = $file_name + $int.ToString(000)+$extension
    # get the first occurance and rename
    Get-ChildItem -Filter $regex | select -First 1 | Rename-Item -NewName $new_name
    # display renamed file name
    Write-Host "Renamed to" $new_name
    # increment counter
    $int++
}

I hope that this is helpful to someone out there.

我希望这对那里的人有所帮助。

subcan

子罐