Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Can a batch file be written for this situation? I want to copy all files from dir A to Dir B but exclude any that exist in Dir C. The files are in the format of 120102dailysales.txt,120202dailysales.txt, etc. These prefixes are a date and will change daily. Any ideas will be appreciated Thank You.

If I correctly understand your post, the following script solves your request; it copies each file from the source folder to the destination one if the file does not exist in the "exclude" directory, i.e.
DCopy A B C
Where A is the Source, B the destination, C the "exclude" directory.
Avoid folders' names with spaces, this is the only limit. I tested it under Windows XP and it worked fine.@Echo Off
:: DCopy.bat Sybtax: DCopy Dir_From Dir_To Dir_EX
If %1.==. GoTo ERR01
If %2.==. GoTo ERR01
If %3.==. GoTo ERR01Set DCErr=%1
If not exist %1.\Nul GoTo ERR02
Set DCErr=%2
If not exist %2.\Nul GoTo ERR02
Set DCErr=%3
If not exist %3.\Nul GoTo ERR02
Echo.
Echo Copy in progress...
CD %1
For %%I in (*.*) Do If not exist %3.\%%I Copy "%1.\%%I" %2 > Nul
Echo Processing completed
GoTo EXIT:ERR01
Echo.
Echo Omitted parameter
GoTo EXIT:ERR02
Echo.
Echo Folder %DCErr% does not exist
GoTo EXIT:EXIT
Set DCErr=If anything goes wrong, post again.

I solved the same problem using the EXCLUDE statement in XCOPY by writing an exclude file on the fly then deleting it at the end of the batch file. The exclude file contains a list of directories, extensions, or file names of what NOT to copy. So to exclude dirC from the copying process the exclude file would contain a line \dirC\. I also put in the exclude.txt file name itself so it doesn't get copied accidently. Here is what the code looks like.
...
...
echo \dirC\ > exclude.txt
echo exclude.txt >> exclude.txt
...
...
xcopy /EXCLUDE:exclude.txt DirA\*.* DirB
...
...
del exclude.txtExplanation:
the ">" in the first statement creates the file and outputs whatever is after the echo statement into the file. The ">>" appends to the file. You can add as many things as you want to exclude. The file is created on the fly, used, then deleted at the end of the batch file. The exclude file will be created in whatever directory the batch file is in so make sure it has read/write access. Look at the help on XCOPY to include other switches. I use /C /D /H /I /R /S /E as well so it only copies newer files (making it a faster process), subdirectories, no user intervention, etc. Your XCOPY statement would then be:xcopy /C /D /H /I /R /S /E /EXCLUDE:exclude.txt DirA\*.* DirB

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |