Rename files using .bat

March 8, 2010 at 02:58:44
Specs: Windows XP
Hi, I have multiple files on my PC of images for my website. They are all name by their productid - big - .jpg like this 49652big.jpg

I need to copy rename and paste some (but not all of them) by their barcode.

I have a text file of the images that need renaming and can copy them into a seperate directory using the following code.

@echo off
md c:\CurrentwebImages3\tmp
dir /b "c:\CurrentwebImages3" >>images.txt
for /f %%i in (images.txt) do (
copy "c:\CurrentwebImages3\%%i" "c:\CurrentwebImages3\tmp\"
)

This selects every entry in the images.txt file copies them from the c:\CurrentwebImages3\ directory and pastes them into c:\CurrentwebImages3\tmp\.

I want the file to be renamed by its barcode which is stored in another file barcodes.txt when copied into the new directory.

Any ideas?


See More: Rename files using .bat

Report •

#1
March 8, 2010 at 06:39:18
We need a sample of barcode.txt in order to help you. Does it have more than one column in it? Just paste the top few lines.

Report •

#2
March 8, 2010 at 07:04:52
Hi, its got the image name in the first column and the corresponding barcode in the next column

84809big.jpg 4002888848091
38043big.jpg 4002888380430
42797big.jpg 4002888427975
31900big.jpg 4002888319003
31901big.jpg 4002888319010
31902big.jpg 4002888319027
31903big.jpg 4002888319034

Ive realised now that part of my original file was unecessary. This how it looks now -

@echo off

for /f %%i in (images.txt) do (
copy c:\CurrentwebImages3\%%i c:\CurrentwebImages3\tmp
pause
)

It copies the required images to another folder. I can then use the concatenate function in excel to produce a list of cells like so -

ren 84809big.jpg 4002888848091.jpg
ren 38043big.jpg 4002888380430.jpg
ren 42797big.jpg 4002888427975.jpg

Copy this to notepad and run as a bat file to rename, however it would be more efficient if the original code could rename the images or open a 2nd file to rename them after the copy.

Cheers


Report •

#3
March 8, 2010 at 07:24:32
@echo off

for /f %%i in (images.txt) do (
  copy "c:\CurrentwebImages3\%%i" "c:\CurrentwebImages3\tmp"
)

for /f "tokens=* delims= " %%a in (barcode.txt) do (
  ren "c:\CurrentwebImages3\tmp\"%%a "c:\CurrentwebImages3\tmp\"%%b.jpg
)

This is untested.

P.S. here is some better code...

@echo off

for /f "tokens=* delims= " %%a in (barcode.txt) do (
   copy "c:\CurrentwebImages3\"%%a "c:\CurrentwebImages3\tmp\"%%b.jpg
)

This way you don't have to rename it.


Report •

Related Solutions

#4
March 8, 2010 at 07:59:50
I put a pause in at the end so I can see the results from the cmd window.

The copy still works but the rename says "The system cannot find the file specified"

I can confirm the entries in the barcodes.txt file match the names of the images that have been copied. I thought it might be because the barcodes.txt wasn't in the same directory as the images ie we had just moved them to the new tmp folder, so I added this code before the first for loop -

xcopy c:\CurrentwebImages3\barcodes.txt c:\CurrentwebImages3\tmp\

Still no joy?


Report •

#5
March 8, 2010 at 09:55:12
Hmm. it should work you may need to put some pauses and echos to see what is happening....

@echo off

for /f "tokens=* delims= " %%a in (barcode.txt) do (
  copy "c:\CurrentwebImages3\"%%a "c:\CurrentwebImages3\tmp\"%%b.jpg
  echo "c:\CurrentwebImages3\"%%a
  echo "c:\CurrentwebImages3\tmp\"%%b.jpg
  pause
)

P.S. You may want to post this in the programming forum.


Report •

Ask Question