Home > front end >  Updating Sub folder and File renaming batch script
Updating Sub folder and File renaming batch script

Time:06-17

Need a bit of help with this script

for /d %%D in ("*") do (
  for %%F in ("%%D\*.jpg") do (
    ren "%%~dpF(*).txt" "(*) %%~nF.*"
  )
)

This is the original script and this is what it does

Before
filename.jpg
(1).txt

Result
filename.jpg
(1) filename.txt

it copies the filename from the jpg and adds it to the filename of the txt file

what I have been trying to do is two things
I want to add a controlled Sub folder reader to it, and I would like to the filename to be copied between certain points of the txt files

Before
filename.jpg
(1)(name).txt

Result
filename.jpg
(1) filename (name).txt

I have tried like 10 different ways to make this work and for some reason I can't

tried this

FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof

:Label
set "FILE=%~1"
for /d %%D in ("*") do (
  for %%F in ("%%D\*.jpg") do (
    ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
  )
)

and I removed this as well for /d %%D in ("*") do (
and tried this

FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof

:Label
set "FILE=%~1"
  for %%F in ("*.jpg") do (
    ren "%%~dpF%~1" "(*) %%~nF (*).*"
)

and tried this

for /d %%D in ('dir /b /s /a-d "*"') do (
  for %%F in ("%%D\*.jpg") do (
    ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
  )
)

Any help would be great

Thank you

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

PUSHD "%sourcedir%"

for /d %%B in ("*") do (
 for %%E in ("%%B\*.jpg") do (
  FOR /f "tokens=1,2delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*)(*).txt" 2^>nul') DO (
   rem files matching "(*)(*).txt only
    REN "%%~dpnxB\(%%q)(%%r).txt" "(%%q) %%~nE (%%r).txt"
  )
  FOR /f "tokens=1*delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*).txt" 2^>nul') DO IF /i "%%r" equ ".txt" (
   rem files matching "(*).txt only
    REN "%%~dpnxB\(%%q).txt" "(%%q) %%~nE.txt"
  )
 )
)

popd

GOTO :EOF

Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.

The outer loop on %%B gets the directory names. No surprise there.

The next loop on %%E gets the .jpg names. No surprise there.

The first loop on %%q looks at the .txt files that fit the pattern (*)(*).txt and re-assembles the parts as required for the rename.

The second loop on %%q looks at the .txt files that fit the pattern (*).txt which may include the just-renamed files matching (*)(*).txt now (*) jpgfilename (*).txt, so this time, %%r must be .txt to exclude these newly-renamed files.

I'll repeat
Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.

  • Related