# Solved: Batch to find and copy files



## Johneeee (Apr 4, 2012)

Hi, I need to make up a simple batch file to search through its current directory, and all sub-directories for files with a certain name (Cover.jpg) and make a copy with another name (AlbumArtSmall.jpg) in the same folder. Some of the folders will already have an 'AlbumArtSmall.jpg' file.

In this blog I found a similar question (http://forums.techguy.org/dos-other/996133-batch-find-copy-files.html) with two responses(from Squashman & Ent) so I tried to adjust the supplied code but didn't quite get there.

Would someone be able to show me how to code what I need? Any help would be much appreciated


----------



## Ent (Apr 11, 2009)

This should do what you're after.

```
@echo off

IF EXIST "%CD%\Cover.jpg" (
IF NOT EXIST "%CD%\AlbumArtSmall.jpg" (
Copy "%CD%\Cover.jpg" "%CD%\AlbumArtSmall.jpg"
)
)


FOR /D /R %%Z IN (*) DO (
IF EXIST "%%~dpZ\Cover.jpg" (
IF NOT EXIST "%%Z\AlbumArtSmall.jpg" (
Copy "%%Z\Cover.jpg" "%%Z\AlbumArtSmall.jpg"
)
)
)
ECHO Copy Sequence Complete.
Pause
```
There's probably an easier way to do it too, but I couldn't think of it.


----------



## Johneeee (Apr 4, 2012)

Wow - no wonder I couldn't get the other code to work! Wasn't expecting it to be that complicated. Thanks a million for the help. I guess were almost there but not quite.

Have attached a screenprint of a section of the directory tree that I tested the code on (Screenprint.doc) and the output was as follows when 'AlbumArtSmall.jpg' did not exist in the 'Classical' directory/ folder:

1 file(s) copied.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Copy Sequence Complete.
Press any key to continue . . . 

this was the output when 'AlbumArtSmall.jpg' did exist in the 'Classical' directory/ folder:

The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Copy Sequence Complete.
Press any key to continue . . . 

I Hope that it will not take too much effort to alter the code & thanks again for the help - much appreciated.


----------



## Ent (Apr 11, 2009)

Hmm, very annoying. I should have tested it a bit more thoroughly then.

I thought it should be simpler too. Actually, I still think it should be. Hopefully it will work better then too.


----------



## Ent (Apr 11, 2009)

```
@echo off

FOR /R %%Z IN (Cover.jpg) DO (

IF EXIST %%Z (
IF NOT EXIST "%%~dpZ\AlbumArtSmall.jpg" (
Echo "%%~dpZAlbumArtSmall.jpg"
Copy "%%Z" "%%~dpZAlbumArtSmall.jpg"
)
)
)
ECHO Copy Sequence Complete.
Pause
```


----------



## Johneeee (Apr 4, 2012)

Ent, that worked a treat - & Wow, was it fast! 

It's all done now - really appreciate it

regards, Johneeee


----------



## Ent (Apr 11, 2009)

You're welcome. Since your problem has been resolved, as the original poster you and only you can click the "mark solved" button at the top of the thread. This will alert others with the same issue that a possible fix can be found here. Likewise it will help to ensure that our volunteers focus on issues which still lack a solution instead of possibly wasting time here.


----------

