如果我有一个目录中有多个文件,想要在它们的文件名后添加一些内容,但不包括扩展名,我该如何做?
我尝试了以下操作,测试文件为file1.txt
和file2.txt
:
ren *.txt *1.1.txt
这会将文件改名为file1.1.txt
和file2.txt1.1.txt
我希望文件名为file1 1.1.txt
和file2 1.1.txt
从命令提示符(cmd)能否实现呢?还是需要批处理(bat)文件来完成?PowerShell呢?
如果我有一个目录中有多个文件,想要在它们的文件名后添加一些内容,但不包括扩展名,我该如何做?
我尝试了以下操作,测试文件为file1.txt
和file2.txt
:
ren *.txt *1.1.txt
这会将文件改名为file1.1.txt
和file2.txt1.1.txt
我希望文件名为file1 1.1.txt
和file2 1.1.txt
从命令提示符(cmd)能否实现呢?还是需要批处理(bat)文件来完成?PowerShell呢?
确保最长名称中的字符数小于 ?
的数量:
ren *.txt "???????????????????????????? 1.1.txt"
请参阅Windows RENAME命令如何解释通配符?以获取更多信息。
新解决方案-2014/12/01
对于那些喜欢正则表达式的人,有一个名为JREN.BAT的混合JScript/batch命令行实用程序,可在XP及以上任何版本的Windows上运行。
jren "^.*(?=\.)" "$& 1.1" /fm "*.txt"
或者
jren "^(.*)(\.txt)$" "$1 1.1$2" /i
步骤 1:
选择所有文件(ctrl + A)
步骤 2:
然后选择重命名选项
步骤 3:
选择您的文件名... 例如:myfile
它会自动重命名为 myfile (01), myfile (02),...
如果您想要替换空格和括号,请继续步骤 4
步骤 4:
从当前文件夹中打开 Windows Powershell
步骤 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(")"),""}
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"
如果您使用简单的for
循环而没有使用/f
参数,则已经重新命名的文件将再次被重命名。
forfiles /M *.txt /C "cmd /c rename @file \"@fname 1.1.txt\""
来源:批量重命名文件扩展名
for
语法简单得多,但缺点是它必须为每个迭代生成一个新的 cmd.exe 进程。这使得它非常低效和慢。 - Amit Naidu@echo off
for %%f in (*.txt) do (
ren "%%~nf%%~xf" "%%~nf 1.1%%~xf"
)
关于Windows RENAME命令如何解释通配符?,请参见此线程中dbenham的答案。@JacksOnF1re - 我的答案中添加了新信息/技巧。你实际上可以使用一个晦涩的正斜杠技巧删除你的"Copy of"前缀:ren "Copy of .txt" "////////"
SKMBT_C36019101512510_001.jpg
SKMBT_C36019101512510_002.jpg
SKMBT_C36019101512510_003.jpg
SKMBT_C36019101512510_004.jpg
:
:
A07_P001.jpg
A07_P002.jpg
A07_P003.jpg
A07_P004.jpg
:
:
ren "SKMBT_C36019101512510_*.*" "/////////////////A06_P*.*"
"
) "<Name Scheme>"
不是一个选项,否则它不起作用,在我们的例子中:"SKMBT_C36019101512510_*.*"
和 "/////////////////A06_P*.*"
被引用。A06_P
代替了 2510_
,而 SKMBT_C3601910151
则通过使用恰好数量的斜杠 /////////////////
(17个字符)被删除。for /f "delims=" %i in ('dir /b /a-d *.*') do ren "%~i" "Service.Enviro.%~ni%~xi"
我也曾为此感到困惑...不喜欢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
这适用于您的特定情况:
ren file?.txt "file? 1.1.txt"