BAT file - XCOPY, way to exclude a folder(s)?

Discussion in 'Computer Support' started by fitwell, Oct 21, 2003.

  1. fitwell

    fitwell Guest

    I've got 3 great working bat files using XCOPY. It would be nice if
    one of them could exclude a folder but I can't find anything on the
    net re this. The /EXCLUDE command didn't work and I didn't realize it
    was for just files until I didn't get results (I'm quite a newbie re
    bat file creation. I'm an expert at using, but not at creating <g>!).

    The bat file is below, and as an fyi, there are 2 MD commands in it
    because it's one of 3 XCOPY ones. All 3 of them create a folder
    within the main "FldrStruct2Bckup" one referenced. Just wanted to
    explain why the two MD steps are necessary. Here is the bat as it
    stands now:

    MD C:\WINDOWS\DESKTOP\FldrStruct2Bckup
    MD "C:\WINDOWS\DESKTOP\FldrStruct2Bckup\BACKUP of upper part of
    ''START MENU''"
    XCOPY /T /E "C:\WINDOWS\Start Menu"
    "C:\WINDOWS\DESKTOP\FldrStruct2Bckup\BACKUP of upper part of ''START
    MENU''" /-Y

    Since I have a separate bat like this just for the PROGRAMS folder in
    the start menu, want this bat above to exclude it that folder from the
    XCOPYing process. Any way to do this?

    fitwell, Oct 21, 2003
    1. Advertisements

  2. The XCOPY's /EXCLUDE:file parm should work. Include something like this
    in your batch file:

    echo \directoryname\ > excludefile.txt
    echo excludefile.txt >> excludefile.txt
    del excludefile.txt

    The first echo command creates the .txt file with the directory name to
    be excluded by XCOPY. The second echo appends the name of the file
    itself to the .txt file, so that xcopy will exclude it. And, finally,
    the exclude file is deleted when the XCOPY has completed.
    SirOsisOfThuliver, Oct 21, 2003
    1. Advertisements

  3. fitwell

    fitwell Guest

    Argggh, my brain just isn't managing this today <g>. I've made so
    many other strides in other areas over last couple of days, I guess
    this is just one thing too many to try to wrap my brain around. I'm
    understanding the concept somewhat, but obviously not totally because
    I can't yet see how a text file is involved in terms of coding it into
    the bat file. <?>

    One file yesterday did reference a text file, but I didn't understand
    that that was part of the syntax and not a completely separate
    function being coded into the bat.

    The folder to exlude in the above bat is this one:

    C:\WINDOWS\Start Menu\Programs

    How would I fit that into the bat file below, all the text file
    Is a text file actually created, btw? And, if so, is it automatically
    deleted after the process is completed?

    fitwell, Oct 22, 2003

  4. I added the statements to create and delete an "exclude file" to your
    batch file, below, as well as added the /EXCLUDE parameter to the XCOPY
    command. I left, as is, a couple of things that didn't look right to me.
    For example,

    1. The "/T" parameter on the XCOPY command will cause it to "create a
    directory structure, but not copy files."

    I haven't tested this, but my understanding is: It will copy NO files.
    It creates directories, only. Was that your intention? If not, you'll
    need to remove the /T. Maybe it works differently in your version of
    Windows. I'll leave that to you to verify.

    2. In multiple places, you have embedded quotation marks. That is, you
    have a quoted string within a larger quoted string. I've never tried
    that in a batch file. Does it cause an error?

    Below, line 6 and 7 create a file named excludefile.txt. The directory
    that you want to exclude, "C:\WINDOWS\Start Menu\Programs", is copied to
    that file. The name of the file itself, excludefile.txt, is then copied
    (appended, actually) to the file. Line 11 deletes the file after the
    XCOPY has completed.

    MD C:\WINDOWS\DESKTOP\FldrStruct2Bckup
    MD "C:\WINDOWS\DESKTOP\FldrStruct2Bckup\BACKUP of upper part of
    echo "C:\WINDOWS\Start Menu\Programs" > excludefile.txt
    echo excludefile.txt >> excludefile.txt
    XCOPY /T /E /EXCLUDE:excludefile.txt "C:\WINDOWS\Start Menu"
    "C:\WINDOWS\DESKTOP\FldrStruct2Bckup\BACKUP of upper part of "START
    MENU"" /-Y
    del excludefile.txt
    SirOsisOfThuliver, Oct 22, 2003
  5. fitwell

    fitwell Guest

    Thank you!
    Yes. Most definitely. The 3 bats are just to back up folder
    structures and content is not desirable in this case. I don't like
    imaging backups; my installation is just too dynamic for this type of
    backup procedure to work. I much prefer to do a wipe and then
    reinstall as I go. I may eventually get to the point where I feel I
    can image that first basic installation but even if that's the case,
    will need to keep a current backup of the folder structures of 3
    folders at present: the start menu both the programs and the portion
    above that and my c:\program files. I have an efficient way of
    installing everything by category but it'll be nice when I can just
    dump the structure into the 3 main roots files after a clean
    No. I put them in because the bat didn't work otherwise in this
    sense: I try to avoid using DOS 8.3 file format where a bat like this
    can act as a template that I can modify for other uses - much easier
    to make changes. It's tough to see the coding otherwise sometimes.
    But if you use the LFN, the bat never works without encapsulating the
    pathname in quotation marks. I learned that trick along the way
    somewhere and I use it whenever I need to reference LFN vs 8.3. (When
    there is no need for long pathname, I just use the 8.3 one which I
    easily get through using one of the apps I can't live without - a PC
    Mag shell extension called Path Copy.)
    Ah, that's what I thought. One would have to write in a cleaup line.
    Super, wow! Looks good. I'm going to try it out and report back.

    Trouble with being a power user newbie is not only do I have enough
    knowledge to get myself into but not out of trouble, sometimes, but
    things can still can get so overwhelming. This was one of those
    times. Thanks for helping me out!

    Cheers! :eek:D
    fitwell, Oct 22, 2003
  6. fitwell


    Sep 9, 2009
    Likes Received:
    ANOTHER question

    i just want to copy only the folders in a particular directory say for example

    i have directory:

    under that directory there is 1 directory
    along with 10 files.

    i just want to copy the \20090909.16.14.26 folder and anything under it
    to C:\japan\WF_SCRIPTS_FILES\Backup\JSTR

    thanks a lot!
    and onte thing, how to start a thread?
    vincecup17, Sep 9, 2009
  7. fitwell


    Feb 13, 2010
    Likes Received:
    xcopy - include / exclude certain folders

    This script prompts for a destination drive and destination folder and backups up my (user = Mark) desktop to that location. (Vista or Windows7)

    It also excludes any files and folders listed in the excludes.txt file.
    This excludes file resides in the same location as the batch file (since no path is given in /EXCLUDE:

    Typical entries in the exclude file would be


    and to exclude a folder


    note - folder names should have a back slash in front and after. This makes sure this only applies to folders and not to files as well.

    If you just had...


    this would exclude all files and all folders with a name CONTAINING the string "somename" (wtb). So, aFileSomeNameOrOther.txt would be excluded, as would a folder called someTextSomeNameMoreText !!

    Here's the script for the batch file... Hope this helps...

    @Echo off

    rem XCOPY switches are...
    rem /r overwrite read only files
    rem /k copy attributes
    rem /y overwrites without prompting
    rem /e copies any subfolder, even if it is empty

    Echo Backs up Mark's Desktop
    Echo Press any key to continue...

    pause > NUL

    Echo Please enter the destination drive letter:
    set INPUT=
    set /P INPUT=Type input: %=%
    if "%INPUT%"=="" goto inputDrive

    set var destinationDrive = %INPUT%

    Echo Please enter the destination folder name (e.g. backups1):
    set INPUT=
    set /P INPUT=Type input: %=%
    if "%INPUT%"=="" goto inputFolder

    set var destinationFolder = %INPUT%

    set var user = Mark

    rem backup desktop
    Echo Copy
    XCOPY C:\Users\%user%\Desktop.* %destinationDrive%:\destinationFolder\%user%\Desktop\*.* /R /K /Y /E /EXCLUDE:excludes.txt

    Echo Done - press any key to finish...

    pause > NUL
    scratch, Feb 13, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.