重命名cmd中的多个文件

问题描述 投票:24回答:9

如果我在目录中有多个文件并想要将某些内容附加到其文件名中,而不是附加到扩展名,我该怎么做?

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

ren *.txt *1.1.txt

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

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

这可能来自cmd还是我需要有一个bat文件来执行此操作? PowerShell怎么样?

windows batch-file cmd wildcard rename
9个回答
17
投票
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"

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


19
投票

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

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

有关更多信息,请参阅How does the Windows RENAME command interpret wildcards?

新解决方案 - 2014/12/01

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

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

要么

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

13
投票

步骤1:

选择所有文件(ctrl + A)

第2步 :

然后选择重命名选项

enter image description here

第3步:

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

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

如果要更换空格和支架,请继续步骤4

第4步:

从当前文件夹中打开Windows Powershell

enter image description here

第5步:

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

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

第6步:

用于更换开口支架

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

用于更换紧密支架

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

10
投票

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

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

来源:Rename file extensions in bulk


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

2
投票

试试这个Bulk Rename Utility效果很好。也许不会重新发明轮子。如果您不需要脚本,这是一个很好的方法。


1
投票

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

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

0
投票

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

这是我写的脚本:

# 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++
}

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

subcan


0
投票

这适用于您的具体情况:

ren file?.txt "file? 1.1.txt" 
© www.soinside.com 2019 - 2024. All rights reserved.