2
votes

I'm trying to develop a batch script that will allow me to rename, delete and move files.

Step 1:

For each folder: delete "00.jpg" and "Credits.jpg" file.

FOR /D %%d IN (*) DO del %%d\00.jpg;del %%d\Credits.jpg

Step 2 (Where I'm stuck):

For each folder: retrieve the last 4 characters (XXXX).
Every file in each folder must be renamed to : XXXX + " (" + filename+")".jpg

For example: a_v18_c160\03.jpg -> a_v18_c160\c160 (03).jpg

Folder names can contain special characters: "["."]"."_","-" but always end with "c" plus three numbers.

Step 2.1(Optional):

Remove 0 if it's not necessary. For example: c160 (03).jpg -> c160 (3).jpg

Step 3:

Move files from every subfolder to root folder:
Example: Root Folder\a\c160 (02).jpg -> Root Folder\c160 (02).jpg

FOR /D %%d IN (*) DO move %%d\* "F:\Root Folder\"

Starting from something like this:

F:\Root Folder\a_v18_c160\00.jpg
F:\Root Folder\a_v18_c160\01.jpg
F:\Root Folder\a_v18_c160\02.jpg
F:\Root Folder\a_v18_c160\03.jpg
F:\Root Folder\a_v18_c160\Credits.jpg
F:\Root Folder\a_v18_c161\00.jpg
F:\Root Folder\a_v18_c161\01.jpg
F:\Root Folder\a_v18_c161\Credits.jpg

The result would be:

F:\Root Folder\c160 (01).jpg
F:\Root Folder\c160 (02).jpg
F:\Root Folder\c160 (03).jpg
F:\Root Folder\c161 (01).jpg
F:\Root Folder\a_v18_c160\
F:\Root Folder\a_v18_c161\

How can I perform step 2?

2

2 Answers

1
votes

Test this on some sample files and folders.

It is meant to be run from the root_folder where the files are to be moved to.

@echo off
for /r %%a in (00.jpg Credits.jpg) do del "%%a"
  setlocal enabledelayedexpansion
   for /d /r %%a in (*) do (
      set "num=%%~nxa"
      set "num=!num:~-4!"
      for %%b in ("%%a\*.jpg") do move "%%b" ".\!num! (%%~nb)%%~xb"
   )
for /f "delims=" %%a in ('dir /b /a-d *.jpg') do (
   set "name=%%a"
   ren "%%a" "!name:(0=(!"
)
0
votes

instead of a batch script you could always write a command line program in Java, Perl, Ruby, PHP. etc your favorite language. I find writing complicated scripts like this easier using a high level programming language.