Solved Can anyone help me with this batch script to rename files?

November 28, 2019 at 12:22:42
Specs: Windows 10
I have a directory which contains folders with names like Lot 1, Lot 2, Lot 5, Lot A, Block A, Block C

Each of these folders contains pdf survey drawings. The names of the files are inconsequential. My goal is to prefix each pdf in ONLY LOT FOLDERS with an edited version of the name of its parent folder. Instead of adding the prefix 'Lot 1_' to the filenames of files in folder 'Lot 1' I would like to add 'L1_' to the filename.

Currently, I have a batch file which will add any prefix entered into a prompt to all pdfs in current folder. This means I have to go into each folder & run the batch script. This is tedious as I have thousands to deal with.

This is what the code looks like:

Set /p prefix="Enter Prefix to add to file names: "
FOR /f "delims=" %%F IN ('DIR /a-d /b *.PDF') DO (RENAME "%%F" %prefix%_"%%F")

I have been messing around trying to create a batch file to iterate through all folders with Lot and a space in their names and so far all I can do is use a for loop to get all the 'Lot 'whatevernumber names and output them to a text file.

This is what that code looks like:

for /f "delims=" %%F in ('dir /A:D /b Lot*') do echo %%F >> folders.txt

and then I get text file with:
Lot 1
Lot 2
Lot 5
Lot A

I can also output just the number portion of the Folder name. That code:

for /f "tokens=2" %%F in ('dir /A:D /b Lot*') do echo %%F >> folders2.txt

and then I get text file with:
1
2
5
A

I felt like I was real close with that last one, but what I tried to do was instead of echoing to folders2.txt I wanted to enter another for loop where i would rename each file in 'Lot '%%F/ and add a prefix of 'L'%%F and the rest of the filename.

Anyone have any ideas???!!!


See More: Can anyone help me with this batch script to rename files?

Reply ↓  Report •

#1
November 28, 2019 at 20:17:41
@echo off & setlocal
for /f "tokens=*" %%a in ('dir /b /ad lot*') do (
for %%b in ("%%a"\*.pdf) do call :xx "%%~dpb" "%%~nxb"
)
goto :eof

:xx
set z=%~1
set z=%z:~0,-1%
for %%c in ("%z%") do set k=%%~nc
set k=%k:lot =L%
echo ren %1%2 %k%_%2
ren %1%2 %k%_%2


Reply ↓  Report •

#2
November 29, 2019 at 06:11:21
That works great! Thank you so much.

One thing I can't figure out though is sometimes files get the prefix twice. I'm not sure why that happens but it seems to be the first file renamed in each lot folder.
Anyway you could break down the script? Particularly after the call :xx. Thanks again, I really appreciate the help.

message edited by Tesolai


Reply ↓  Report •

#3
November 29, 2019 at 19:23:34
✔ Best Answer
I think this fixes the "cyclic redundancy" flaw in post 1 that caused the double-renamed files:
@echo off & setlocal
for /f "tokens=*" %%a in ('dir /b /ad lot*') do (
:: this method dodges the C.R by using a delayed exposure of the subdir
for /f "tokens=*" %%b in ('dir /b /s "%%a"\*.pdf') do call :xx "%%~dpb" "%%~nxb"
:: foll .line didn't work due to C.R
rem for %%b in ("%%a"\*.pdf) do call :xx "%%~dpb" "%%~nxb"
)
goto :eof

:xx
set z=%~1
rem next 2 lines just gets the last level of the path-string. the "\" needs to be removed,
rem then the "fake" for %%c is just to allow extraction the "name" from the path into 'k'
set z=%z:~0,-1%
for %%c in ("%z%") do set k=%%~nc
rem now 'k' subdir-name is "lot" compressed to first letter and whatever follows that
set k=%k:lot =L%
echo ren %1%~2 %k%_%~2
ren %1%~2 %k%_%~2
::-----------
Use command: FOR /?
to get more info on the syntax of some of the above. Hope this helps.


Reply ↓  Report •

Related Solutions

#4
December 2, 2019 at 08:32:08
Sure is wonderful nbrane....Thank you. This will save me a tonne (ton) of time.

Reply ↓  Report •

Ask Question