Search code examples
command-promptxcopycopy-item

XCopy specific txt files in multiple subfolders without subfolder in path


I have a batch file to copy over files from Web folder to my local folder.

Web folder destination look like this:

G:\program files\2023\subfolder_1\1.txt
G:\program files\2023\subfolder_1\1_total.txt
G:\program files\2023\subfolder_1\2.txt
G:\program files\2023\subfolder_1\2_total.txt
G:\program files\2023\subfolder_1\3.txt
G:\program files\2023\subfolder_1\3_total.txt
G:\program files\2023\subfolder_1\4.txt
G:\program files\2023\subfolder_1\4_total.txt
G:\program files\2023\subfolder_1\5.txt
G:\program files\2023\subfolder_1\5_total.txt

The script I uesd:

xcopy /s "G:\program files\2023\subfolder_1\*.txt" c:\temp\myfiles\lessons

Then local destination look like this:

c:\myfiles\lessons\subfolder_1_5\1.txt
c:\myfiles\lessons\subfolder_1_5\1_total.txt
c:\myfiles\lessons\subfolder_1_5\2.txt
c:\myfiles\lessons\subfolder_1_5\2_total.txt
c:\myfiles\lessons\subfolder_1_5\3.txt
c:\myfiles\lessons\subfolder_1_5\3_total.txt
c:\myfiles\lessons\subfolder_1_5\4.txt
c:\myfiles\lessons\subfolder_1_5\4_total.txt
c:\myfiles\lessons\subfolder_1_5\5.txt
c:\myfiles\lessons\subfolder_1_5\5_total.txt

I desired:

c:\temp\myfiles\lessons\1.txt
c:\temp\myfiles\lessons\2.txt
c:\temp\myfiles\lessons\3.txt
c:\temp\myfiles\lessons\4.txt
c:\temp\myfiles\lessons\5.txt

I only want to copy .txt but exclude _total.txt ,also don't contain subfolder in my local folder.What am I missing?


Solution

  • The first problem/Question

    You have subfolder 1 now I suppose you have 2,3 and 4 as well?

    xcopy /s "G:\program files\2023*.txt" "c:\temp\myfiles\lessons"

    /s will copy subfolders as well, in this case all txt-files in each subfolder

    For exclude it should be some like /exclude:"*_total.txt"

    EDIT - I was a little to fast in my answer :) Create a txt file, add only _total.txt

    and use this as /exclude Ex. /exclude:excludedfileslist.txt

    Then it will skip copy that file as well

    Good luck