Page 1 of 1

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

Posted: 14 Jun 2021 15:06
by JeffP
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!

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

Posted: 15 Jun 2021 02:05
by penpen
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.

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

Posted: 15 Jun 2021 09:07
by JeffP
Thanks, penpen.

But couldn't get this to work.

Any ideas?

Thanks!

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

Posted: 15 Jun 2021 20:05
by AR Coding
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

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

Posted: 15 Jun 2021 22:36
by Squashman
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"

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

Posted: 17 Jun 2021 09:28
by penpen
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.