Computing.Net > Forums > Disk Operating System > Batch file question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file question

Reply to Message Icon

Name: Eric Holmes
Date: May 20, 2003 at 20:40:28 Pacific
OS: Windows XP
CPU/Ram: P4\512
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: May 21, 2003 at 06:18:49 Pacific
Reply:

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 ERR01

Set 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.


0

Response Number 2
Name: josh
Date: May 23, 2003 at 09:12:03 Pacific
Reply:

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.txt

Explanation:
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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch file question

batch file question www.computing.net/answers/dos/batch-file-question/3271.html

Batch File question? www.computing.net/answers/dos/batch-file-question/5299.html

Another batch file question www.computing.net/answers/dos/another-batch-file-question/9863.html