Batch script to rename text files to match image files names.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JeffP
Posts: 3
Joined: 14 Jun 2021 14:54

Batch script to rename text files to match image files names.

#1 Post by JeffP » 14 Jun 2021 15:06

I have a folder that contains countless image files. ( jpg, and psd), and one text file "Placehold.txt" ( this text file contains specific instructions inside)

I'm in need of a batch script that will duplicate the Placehold.txt file and rename it to match all image files within this folder. Z:\Images\Converted\
An example of a before and after would look like this:

Cat_001.00_R R.jpg
Dog 3_04  25.00.A.jpg
House_23.00_A DD.psd

Cat_001.00_R R.txt
Dog 3_04  25.00.A.txt
House_23.00_A DD.txt

** each new text file are a copy of the original "Placehold.txt" file

Thanks for any help!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to rename text files to match image files names.

#2 Post by penpen » 15 Jun 2021 02:05

This might help you (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
pushd "Z:\Images\Converted\"
for %%a in (*.jpg *.psd) do copy "Placehold.txt" "%%~na.txt"
popd
goto :eof
penpen

Edit: Corrected critical error, thanks to Squashman for seeing that.

JeffP
Posts: 3
Joined: 14 Jun 2021 14:54

Re: Batch script to rename text files to match image files names.

#3 Post by JeffP » 15 Jun 2021 09:07

Thanks, penpen.

But couldn't get this to work.

Any ideas?

Thanks!

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Batch script to rename text files to match image files names.

#4 Post by AR Coding » 15 Jun 2021 20:05

You can try switching

Code: Select all

for %%a in (*.jpg *.psd) do copy "Placehold.txt" "%%~a"
to

Code: Select all

for %%a in (*.jpg *.psd) do type Placehold.txt > "%%~a"
Or try adding .txt After %%~a

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to rename text files to match image files names.

#5 Post by Squashman » 15 Jun 2021 22:36

Type would be slower. You just need to use the command modifiers to get the base file name and then add the txt extension.

copy "Placehold.txt" "%%~na.txt"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch script to rename text files to match image files names.

#6 Post by penpen » 17 Jun 2021 09:28

I am so sorry for making the above error and want to apologize.
I hope the /Y command wasn't preset in the COPYCMD variable,
so your test environment wasn't messed up.
Thanks to Squashman, for finding that error, i literally didn't see that.
I fixed the above code.

Post Reply