Avatar of oconnork00
oconnork00Flag for United States of America

asked on 

Check inside folders and copy images to a new folder

Hi all,

I have a lot of folders inside one main folder, and inside these folders is a picture file called image1.jpg

So if I have 100 folders, each folder will contain a different image with that same name.

The problem is though that it take ages to go into each folder and review each image.

So if the folder is structured like this:

c:\mainfolder\otherfolder\otherfolderstill\image1.jpg

I would like the use of a batch that will go into all folders and when it finds a file called image1.jpg to copy it to a new folder into its root, for example D\images\
and auto rename each file by one as it finds it.
I am running XP pro

I look forward to your valued help

Thanks,

Kevin
Windows Batch

Avatar of undefined
Last Comment
oconnork00
Avatar of Shift-3
Shift-3
Flag of United States of America image

How do you want them renamed?
Avatar of oconnork00
oconnork00
Flag of United States of America image

ASKER

They are actually called header_copy.bmp but renaming them incrementally starting at 0001.bmp would be great.
thanks Shift-3
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of oconnork00
oconnork00
Flag of United States of America image

ASKER

Hi Shift-3

This is my customisation attached.

For some reason the batch file just flashes and ends, and nothing appears inside the images folder.

Does this look in all the folders - as there are folders inside of folders of the module1a

Thanks

Kevin


@echo off
setlocal enabledelayedexpansion
 
set source=E:\Module1\Module1a
set target=E:\images
set filename=header_copy.bmp
 
set counter=1
 
for /F "tokens=*" %%G in ('dir "%source%\%filename%" /A:-D /B /S') do (
 set newname=!counter!
 call :_rename
 echo F| xcopy "%%G" "%target%\!newname!%%~xG" /C /H /R /Y /Z > NUL
 set /a counter+=1
)
 
goto :eof
 
:_rename
if "!newname:~3,1!"=="" (
 set newname=0!newname!
 goto :_rename
)
goto :eof

Open in new window

Avatar of Shift-3
Shift-3
Flag of United States of America image

Yes, it should search for the filename in all subfolders.

Remove the > NUL from line 13 and add a pause to line 16 and see if any errors appear.
Avatar of oconnork00
oconnork00
Flag of United States of America image

ASKER

Hi Shift-3,

Ok made the change to the file but again it just flashed up and closed again when I double clicked on it.

I will attach the code below.

Thanks for all the help.

Kevin
@echo off
setlocal enabledelayedexpansion
 
set source=E:\Module1a
set target=E:\images
set filename=header_copy.bmp
 
set counter=1
 
for /F "tokens=*" %%G in ('dir "%source%\%filename%" /A:-D /B /S') do (
 set newname=!counter!
 call :_rename
 echo F| xcopy "%%G" "%target%\!newname!%%~xG" /C /H /R /Y /Z > 
 set /a counter+=1
)
pause 
goto :eof
 
:_rename
if "!newname:~3,1!"=="" (
 set newname=0!newname!
 goto :_rename
)
goto :eof

Open in new window

Avatar of Shift-3
Shift-3
Flag of United States of America image

Well something isn't right.  Are you running it from a location that has a drive letter?  Batch scripts often have problems running from UNC paths.

Go to a command prompt (go to Start|Run, type cmd, and hit Enter) and run the script from there.  That should let you see what's happening.
Avatar of Shift-3
Shift-3
Flag of United States of America image

Also remove the > from the end of line 13.
Avatar of oconnork00
oconnork00
Flag of United States of America image

ASKER

HI,

I removed the > and it ran, but it says "File not found press any key to continue"

The path is 100% correct here, i even changed it directly to where the image_copy.bmp is and the same message came up. I am also running the program from c:

Running it from the command prompt I get the same message.


Cheers,

Kevin
Avatar of Shift-3
Shift-3
Flag of United States of America image

Are the files you're looking for named image_copy.bmp or header_copy.bmp?

Whichever it is, is the filename variable set to the right one?
Avatar of oconnork00
oconnork00
Flag of United States of America image

ASKER

Good call - I thought it was header_copy.bmp but it was infact header copy.bmp

There was a space there.

Sorry for all that confusion - percfect solution thank you Shift-3
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo