0

I have a large number of files with names like this in a same folder:

  • myPic_fr.png
  • myPic_it.png
  • myPic_gr.png

I want to rename them to:

  • myPic_fr_1080.png
  • myPic_it_1080.png
  • myPic_gr_1080.png

Then copy them to a new folder like this:

  • ../fr/myPic_fr_1080.png
  • ../it/myPic_it_1080.png
  • ../gr/myPic_gr_1080.png

How to create a batch script or powershell script to do that job?

Edit: I tried this batch script code to do the rename job (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!"
)

But for the "copy" part I don't know how to do it! Maybe need Regex?

Thanks

Squashman
  • 13,649
  • 5
  • 27
  • 36
Wadjey
  • 145
  • 13
  • Do you need to rename them just in the destination folder, or in both places? – Mathias R. Jessen Oct 11 '18 at 13:11
  • 3
    I'd try reading the documentation first: [Get-ChildItem](https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Management/Get-ChildItem), [Copy-Item](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item6) & [Rename-Item](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item) and then make an attempt at writing some code yourself. If/when you come across any issues with your code, explain what you have tried, and why it did not work for you. – henrycarteruk Oct 11 '18 at 13:12
  • in both places. – Wadjey Oct 11 '18 at 13:12
  • You way over coded the batch file. Read the last section of the help file for the `FOR` command. It is super easy to strip the file extension from the file name without having to do string replacement. – Squashman Oct 11 '18 at 13:37

3 Answers3

0

This is far from optimal, but you could try something like this:

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"
}
Razorfen
  • 423
  • 4
  • 12
0

Sample tree before

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

running this script:

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

and the tree after

> 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
  • Thanks very much @lotpings Just one more thing, how to match a file name in this format myPic_02_fr.png ? – Wadjey Oct 11 '18 at 15:40
  • To avoid problems when running the script mutiple times I chose a RegEx to select the country which only expects **one** underscore. The way @Esperento57 uses avoids this by selecting the last underscore delimited token. I'll modifiy my RegEx to be u - nspecific on the numer of underscores in a .minute - **DONE** –  Oct 11 '18 at 15:45
  • Perfect! Thanks again – Wadjey Oct 11 '18 at 15:50
0

try this

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
}
Esperento57
  • 16,521
  • 3
  • 39
  • 45