0

我在同一个文件夹中有大量名称如下的文件:

  • 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 脚本来完成这项工作?

编辑:我试过这个批处理脚本代码来做重命名工作(谢谢@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!"
)

但是对于“复制”部分,我不知道该怎么做!也许需要正则表达式?

谢谢

4

3 回答 3

0

这远非最佳,但你可以尝试这样的事情:

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"
}
于 2018-10-11T13:25:05.677 回答
0

之前的样本树

> tree /f
    myPic_fr.png
    myPic_gr.png
    myPic_it.png

运行此脚本:

## Q:\Test\2018\10\11\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
于 2018-10-11T13:50:33.317 回答
0

试试这个

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
}
于 2018-10-11T14:16:42.943 回答