Thursday, February 19, 2015

windows - bulk renaming sets of image files


We use a 6-camera insect imaging setup described here: https://osf.io/s2p73/


Part of our workflow involves renaming the image files so they can be imported into our database. We currently do this through multiple "rename pair" steps in Bulk Rename Utility and are looking for a single DOS batch file that would simplify the process. The images start and stay in two folders with initial --> final names as below. Each day there are up to 1000 images in folder 1 and the corresponding 5000 images in folder 2 to be processed.


The first number indicates the specimen (0001/2/3/4/5 etc). The bold number below is the unique catalog number which then needs to be transferred to the other 5 images during the rename and the last two numbers after the catalogue number can be different between image sets.


Folder 1: "Renamed"


0001_ALICE1-012345678_887856_162658.jpg --> 012345678_887856_162658.jpg


Folder 2: "Additional"


0001_ALICE2.jpg --> 012345678_lateral.jpg


0001_ALICE3.jpg --> 012345678_additional_1.jpg


0001_ALICE4.jpg --> 012345678_additional_2.jpg


0001_ALICE5.jpg --> 012345678_additional_3.jpg


0001_ALICE6.jpg --> 012345678_additional_4.jpg


the next set of images would be 0002_etc and have a different catalog number to be transferred.
Any help would be appreciated!


Answer




  • Iterate files in Folder1 with a matching pattern

  • split the base file names (%%F) at - and _ into parts (%%A..%%E)

  • use %%C..%%E to rename Alice1 file

  • use %%A to rename the other according to your scheme


With an initial tree of test folders/files on my Ramdisk A:


> tree a:\ /f
A:\
├───Additional
│ 0001_ALICE2.jpg
│ 0001_ALICE3.jpg
│ 0001_ALICE4.jpg
│ 0001_ALICE5.jpg
│ 0001_ALICE6.jpg

└───Renamed
0001_ALICE1-012345678_887856_162658.jpg

This batch file:


:: Q:\Test\2019\03\30\SU_1419303.cmd
@Echo off
set "Folder1=A:\Renamed"
set "Folder2=A:\Additional"
For /f "delims=" %%F in ('
Dir /B/S/A-D "%Folder1%\????_Alice?-*_*_*.jpg
') Do For /F "tokens=1-5 delims=-_" %%A in ("%%~nF") DO (
Echo %%~nxF
Echo %%A %%B %%C %%D %%E
Ren "%%~fF" "%%C_%%D_%%E%%~xF"
Ren "%Folder2%\%%A_ALICE2.jpg" "%%C_lateral.jpg"
Ren "%Folder2%\%%A_ALICE3.jpg" "%%C_additional_1.jpg"
Ren "%Folder2%\%%A_ALICE4.jpg" "%%C_additional_2.jpg"
Ren "%Folder2%\%%A_ALICE5.jpg" "%%C_additional_3.jpg"
Ren "%Folder2%\%%A_ALICE6.jpg" "%%C_additional_4.jpg"
)

yields this result:


> Q:\Test\2019\03\30\SU_1419303.cmd
0001_ALICE1-012345678_887856_162658.jpg
0001 ALICE1 012345678 887856 162658
> tree a:\ /f
A:\
├───Additional
│ 012345678_additional_1.jpg
│ 012345678_additional_2.jpg
│ 012345678_additional_3.jpg
│ 012345678_additional_4.jpg
│ 012345678_lateral.jpg

└───Renamed
012345678_887856_162658.jpg

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...