# Solved: Help with creating a batch file for copying files



## Duggzo (Jun 21, 2013)

Right I'm still new to coding and this is going to get a little bit complicated so any help will be appreciated.

I need to copy all .jpg files for "dir a" into "drive b"

If there is no .jpg in "dir a" I need it to throw up an error possibly a .vbs script which I have already made.

Any help will be a big help.

Thanks.


----------



## Squashman (Apr 4, 2003)

```
@echo off
pushd C:\a
for %%G in (*.jpg) do copy "%%G" B:\
IF %errorlevel% equ 1 echo NO FILES TO COPY
```


----------



## Duggzo (Jun 21, 2013)

Thats brilliant.
Thanks for the help.

How would you put a 7 minute time limit on this?


----------



## Squashman (Apr 4, 2003)

http://technet.microsoft.com/en-us/library/cc754891(v=ws.10).aspx


----------



## Duggzo (Jun 21, 2013)

I'm not getting the error message when there is no jpeg?


----------



## Duggzo (Jun 21, 2013)

@echo
:loop
pushd U:\USERS\PROD\Dugg
for %%G in (*13*.jpg) do copy "%%G" M:\
IF %ERRORLEVEL% EQU 0 GOTO :COMPLETE
echo NO JPEG FILE! 
pause
:COMPLETE
timeout /t 300 /nobreak
GOTO LOOP


Here is what I have so far but it never skips to :complete whatever the situation is?


----------



## Squashman (Apr 4, 2003)

Working on another solution. I can get it work using the DIR command inside the FOR loop but I am trying to bulletproof the code and can't get it too work the way I want.


----------



## Duggzo (Jun 21, 2013)

Its almost like its not recieving an errorlevel of 0 when there are no files?


----------



## Duggzo (Jun 21, 2013)

The destinations are on a server if this helps


----------



## Squashman (Apr 4, 2003)

Duggzo said:


> The destinations are on a server if this helps


Has nothing to do with the issue.


----------



## Duggzo (Jun 21, 2013)

I'm fresh our of ideas how to fix this now :/.
So any help will be very much appreciated.

Cheers


----------



## Squashman (Apr 4, 2003)

Duggzo said:


> I'm fresh our of ideas how to fix this now :/.
> So any help will be very much appreciated.
> 
> Cheers


I said I am working on it.


----------



## Duggzo (Jun 21, 2013)

Thank you Squashman


----------



## foxidrive (Oct 20, 2012)

Squashman was busy and asked me to post this for you:


```
@echo off
pushd "c:\a"
for /f "delims=" %%G in ('dir /b /a-d *.jpg 2^>nul ') do copy "%%G" "B:\"
if not exist *.jpg echo no jpgs found & pause
popd
```


----------



## Squashman (Apr 4, 2003)

Not sure why I didn't think of this earlier. 

```
@echo off
:LOOP
pushd "U:\USERS\PROD\Dugg"
for /f "delims=" %%G in ('dir /b /a-d *.jpg 2^>nul ^|^| echo NO JPGS found^>CON ') do copy "%%G" "M:\"
POPD
pause
timeout /t 300 /nobreak
GOTO LOOP
```


----------



## Squashman (Apr 4, 2003)

Of course now you have to deal with overwrite issues. Not sure why we are checking for JPG's and copying them. If we copying them, they are still going to be there 300 seconds later to copy again. You either need to use the MOVE command or delete them after you copy otherwise you need to add the /Y switch to your copy command.


----------



## Duggzo (Jun 21, 2013)

Squashman said:


> Of course now you have to deal with overwrite issues. Not sure why we are checking for JPG's and copying them. If we copying them, they are still going to be there 300 seconds later to copy again. You either need to use the MOVE command or delete them after you copy otherwise you need to add the /Y switch to your copy command.


Yeah, that worked a treat.
thanks.

I defiently need them to copy so I have added in the /Y after copy so it doesnt ask.

Thanks for all your help.


----------

