0
votes

I need to copy an unknown number of directories (including the files inside them) from one location to another with a batch file. My only problem is that I must not copy the files that are located in the same location as the directories.

For example:

Let say c:\Folder\ contains the directories: Dir1 and Dir2 and the file: f1.

I want to copy c:\Folder\Dir1 and c:\Folder\Dir2 (and the files inside them) to c:\Location directory but not file: f1.

Help please!

2
check xcopy and robocopy commands . - npocmaka
Yeah... But they either copy the directories empty, or copy them properly but with file: f1 and as I sad I must void from coping it. - David Lesner
Do these files, which you won't copy like (f1), all on same direction and do they have the same file Extension? - Bk_
They are all .xml files, f1 and the files inside Dir1 and Dir2, so I can't use the exclude option. - David Lesner

2 Answers

0
votes

Try this:

@echo off
setlocal enabledelayedexpansion

REM Set variable
set _SOURCE="C:\Temp\Test\"
set _DESTINATION="C:\Temp\New\"

REM Change Direction
pushd %_SOURCE%

FOR /D %%a in (*) DO xcopy /S /I %%a %_DESTINATION%%%a
  • /D : Just directories
  • /S : Copy subdirectories
  • /I : Targets are directory (not file)
0
votes

How about something like...

FOR /F "usebackq tokens=*" %%d IN (`DIR /AD /B C:\FOLDER`) DO (
    IF NOT EXIST "C:\LOCATION\%%d" (MKDIR "C:\LOCATION\%%d")
    XCOPY /E "%%d" "C:\LOCATION\%%d"
)