0

I have a series of image files with a set prefix that I would like to remove. Example of the filenames that I will have:

  • Image_2022-06-09-12-29-00_Filename-01
  • Image_2022-07-09-13-29-59_Filename-02
  • Image_2022-02-11-09-26-31_Filename-03

I would like them to turn into:

  • Filename-01
  • Filename-02
  • Filename-03

I currently have a .bat file that manages to remove the first part of all the files which can either be .jpg or .png files:

set current_dir=%cd%
powershell -Command "get-childitem *.png | rename-item -newname { [string]($_.name).substring(26) }"
powershell -Command "get-childitem *.jpg | rename-item -newname { [string]($_.name).substring(26) }"

My problem now however, is that once I run this and the files are renamed, I cannot copy in new files and run the .bat file again because it will rename the files that have already been renamed. Is it possible to identify a sequence of the first part of a filename, and then apply this only to those files? The first 26 characters will always be in that format, but the numbers may change.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
mrwxy
  • 1
  • 1

2 Answers2

1

This simple pure Batch file should do the work (and should run much faster than the PS versions):

@echo off

for /F "delims=" %%a in ('dir /B *.png *.jpg') do for /F "tokens=3 delims=_" %%f in ("%%a") do ren %%a %%f

EDIT 2022/06/22: New method to fulfill the new specifications

@echo off

for /F "delims=" %%a in ('dir /B *.png *.jpg') do for /F "tokens=2* delims=_" %%e in ("%%a") do ren %%a %%f
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks for the suggestion Aacini - I had a go at running this in a .bat file. It seems that what this batch did was only rename one file, and also it so happened that the file that it renamed actually had another underscore. So the filename "Image_2022-06-09-12-29-00_Filename-01_AA" became "Filename-01", and I lost the 'AA' part at the end. Would it be possible using your method to just delete anything before the second underscore? – mrwxy Jun 22 '22 at 08:19
  • Well, such a name will _not_ conform your original specifications... **`:(`** See the edit in my answer... – Aacini Jun 22 '22 at 14:18
0

Seems to me all you have to do is to split the file's BaseName (name without extension) on the underscore and take the last element. Then recombine that with the file's extension and you're good to go.

(Get-ChildItem -Path 'X:\WhereTheFilesAre' -File | Where-Object { $_.Extension -match '\.(jpg|png)' }) | 
Rename-Item -NewName {'{0}{1}' -f ($_.BaseName -split '_')[-1], $_.Extension }

As per your comment, the filenames could have mixed hyphens and underscores, you can change the Rename-Item scriptblock into

Rename-Item -NewName {'{0}{1}' -f ($_.BaseName  -replace '.*[-_](\w+[-_]\d+)$', '$1'), $_.Extension }

Tests:

'Image_2022-06-09-12-29-00_Filename-01' -replace '.*[-_](\w+[-_]\d+)$', '$1' # --> Filename-01
'Image_2022-07-09-13-29-59_Filename_02' -replace '.*[-_](\w+[-_]\d+)$', '$1' # --> Filename_02

As for setting the PowerShell function to use the same current directory as your batch file, please read this answer

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Hi Theo, thanks for your suggestion! I have two questions please. First is, can the path be set to wherever the batch file is by default? That's why I had the 'current_dir' part in my batch. Second one is, as asked in Aacini's response, I have recently discovered that there are some files with additional underscores unfortunately. Is there a way to rather than take the last element, we instead cull the first two elements? So for example, "Image_2022-06-09-12-29-00_Filename-01" & "Image_2022-07-09-13-29-59_Filename_02" becomes "Filename-01", and "Filename_02". Sorry for changing the parameters! – mrwxy Jun 23 '22 at 00:27
  • @mrwxy please see my edit. Also, can you explain why you need a .bat file to run PowerShell code?? Why not just do it all from PowerShell straight away? – Theo Jun 23 '22 at 13:20