批量重命名和复制 Windows 中的多个文件
Batch rename and copy multiple files in Windows
我在同一文件夹中有大量名称如下的文件:
- myPic_fr.png
- myPic_it.png
- myPic_gr.png
我想将它们重命名为:
- myPic_fr_1080.png
- myPic_it_1080.png
- myPic_gr_1080.png
然后像这样将它们复制到一个新文件夹中:
- ../fr/myPic_fr_1080.png
- ../it/myPic_it_1080.png
- ../gr/myPic_gr_1080.png
如何创建批处理脚本或 powershell 脚本来完成这项工作?
编辑:
我尝试了这个批处理脚本代码来完成重命名工作 (Thanks @RoXX):
@echo off
setlocal EnableDelayedExpansion
SET oldPart=.png
SET newPart=_1080.png
for /f "tokens=*" %%f in ('dir /b *.png') do (
SET newname=%%f
SET newname=!newname:%oldPart%=%newPart%!
move "%%f" "!newname!"
)
但是对于 "copy" 部分我不知道该怎么做!也许需要正则表达式?
谢谢
这远非最佳,但您可以尝试这样的事情:
foreach ($pic in (Get-ChildItem *.png -Name)) {
# delete "myPic_" and ".png" to get the destination-folder from the filename
$destFolder = ($pic -replace "myPic_", "") -replace "`.png", ""
# replace ".png" with "_1080.png" to create the new filename
$destName = $pic -replace ".png", "_1080.png"
Copy-Item "$pic" "$destFolder$destName"
}
之前的示例树
> tree /f
myPic_fr.png
myPic_gr.png
myPic_it.png
运行 这个脚本:
## Q:\Test18\SO_52760856.ps1
Get-ChildItem *_*.png | Where-Object BaseName -match '^.*_([^_]{2})$' |
ForEach-Object {
$Country=$Matches[1]
$NewName=$_.BaseName+"_1080"+$_.Extension
$_ | Rename-Item -NewName $NewName
if (!(Test-Path $Country)){MD $Country|Out-Null}
Copy-Item $NewName (Join-Path $Country $newName)
}
和
之后的树
> tree /f
│ myPic_fr_1080.png
│ myPic_gr_1080.png
│ myPic_it_1080.png
│
├───fr
│ myPic_fr_1080.png
│
├───gr
│ myPic_gr_1080.png
│
└───it
myPic_it_1080.png
试试这个
Get-ChildItem "C:\temp\test" -file -Filter "?*_?*.png" | %{
$NewName="{0}_1080{1}" -f $_.BaseName, $_.Extension
$NewDir="{0}\{1}" -f $_.DirectoryName, ($_.BaseName -split "_")[-1]
$NewPath="{0}\{1}" -f $NewDir, $NewName
New-Item $NewDir -ItemType Directory -Force
Copy-Item $_.FullName -Destination $NewPath -Force -Recurse
Rename-Item $_.FullName -NewName $NewName
}
我在同一文件夹中有大量名称如下的文件:
- myPic_fr.png
- myPic_it.png
- myPic_gr.png
我想将它们重命名为:
- myPic_fr_1080.png
- myPic_it_1080.png
- myPic_gr_1080.png
然后像这样将它们复制到一个新文件夹中:
- ../fr/myPic_fr_1080.png
- ../it/myPic_it_1080.png
- ../gr/myPic_gr_1080.png
如何创建批处理脚本或 powershell 脚本来完成这项工作?
编辑: 我尝试了这个批处理脚本代码来完成重命名工作 (Thanks @RoXX):
@echo off
setlocal EnableDelayedExpansion
SET oldPart=.png
SET newPart=_1080.png
for /f "tokens=*" %%f in ('dir /b *.png') do (
SET newname=%%f
SET newname=!newname:%oldPart%=%newPart%!
move "%%f" "!newname!"
)
但是对于 "copy" 部分我不知道该怎么做!也许需要正则表达式?
谢谢
这远非最佳,但您可以尝试这样的事情:
foreach ($pic in (Get-ChildItem *.png -Name)) {
# delete "myPic_" and ".png" to get the destination-folder from the filename
$destFolder = ($pic -replace "myPic_", "") -replace "`.png", ""
# replace ".png" with "_1080.png" to create the new filename
$destName = $pic -replace ".png", "_1080.png"
Copy-Item "$pic" "$destFolder$destName"
}
之前的示例树
> tree /f
myPic_fr.png
myPic_gr.png
myPic_it.png
运行 这个脚本:
## Q:\Test18\SO_52760856.ps1
Get-ChildItem *_*.png | Where-Object BaseName -match '^.*_([^_]{2})$' |
ForEach-Object {
$Country=$Matches[1]
$NewName=$_.BaseName+"_1080"+$_.Extension
$_ | Rename-Item -NewName $NewName
if (!(Test-Path $Country)){MD $Country|Out-Null}
Copy-Item $NewName (Join-Path $Country $newName)
}
和
之后的树> tree /f
│ myPic_fr_1080.png
│ myPic_gr_1080.png
│ myPic_it_1080.png
│
├───fr
│ myPic_fr_1080.png
│
├───gr
│ myPic_gr_1080.png
│
└───it
myPic_it_1080.png
试试这个
Get-ChildItem "C:\temp\test" -file -Filter "?*_?*.png" | %{
$NewName="{0}_1080{1}" -f $_.BaseName, $_.Extension
$NewDir="{0}\{1}" -f $_.DirectoryName, ($_.BaseName -split "_")[-1]
$NewPath="{0}\{1}" -f $NewDir, $NewName
New-Item $NewDir -ItemType Directory -Force
Copy-Item $_.FullName -Destination $NewPath -Force -Recurse
Rename-Item $_.FullName -NewName $NewName
}