Batch file copy folders

March 21, 2009 at 06:56:07
Specs: Windows XP

I need to write a batch file with three parameters: source directory, file name, destination directory.
Batch file has to check if the subdirectories of the given directory contain a certain file. If a directory contains a file with that name then that subdirectory and its files should be copied to a new directory.
I'm new at this and so far my file copies all subdirectories to the new location even if they do not contain specific file.

See More: Batch file copy folders

Report •

March 21, 2009 at 09:57:55
What have you got so far?

If at first you don't succeed, you're about average.


Report •

March 22, 2009 at 04:56:46
so far i have:

@echo off

if "%1" == "" goto help
if not exist %1 goto NoDirectory
if "%2" == "" goto help
if "%3" == "" goto help

for /r %1 %%i in (.) do if exist %2 xcopy %%i %3

goto end

echo Directory does not exist.
goto help


Report •

March 22, 2009 at 05:59:36
I'm not sure I'm with you but the bat below may help.


You want to be careful putting spaces in equality test [==]; things can get very confused.

You don't need to declare END; you can use the built-in :EOF, but for reasons unknown it needs the colon.

No need to test for each param individually. If 1 is missing then so are 2 and 3.

:: 3 param: src filename dest

@echo off & setLocal EnableDelayedExpansion

if %3'==' echo you need src filename dest && goto :eof
if not exist %1\%2 echo %2 not found && goto :eof
if not exist %3 echo %3 not found && goto :eof

pushd %1

copy . %3

If at first you don't succeed, you're about average.


Report •

Related Solutions

March 23, 2009 at 14:19:09
Thanks for the help, but your way didn't work. It did however give me some other ideas to try and i've managed to do what i wanted. So no more help needed.

Thanks again.

Report •

Ask Question