Search within window folders for certain folder names and copy structure to new location including files with in each folder

Hello,

I'm looking to create a batch script to search through folders to see if it contains certainly named subfolders and if it does find them to copy the folder ( and structure and files in them ) to a new destination.

so it would be as follows

Folder 1
 -> Folder Files
            -> in here would be files
 -> Folder Docs
            -> in here would be files
Folder 2
 -> Folder Files
            -> in here would be files
 -> Folder Docs
            -> in here would be files

so all files within each of the Folder Files and Folder Docs to be copied aswell.

Thanks in advance.
batch-file.PNG
deanmachine333Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
Create a batch containing
for /D %%A in (C:\AAA\*) do If Exist %%A\Files xcopy %%A\Files\*.* Destination for files&If Exist %%A\Docs xcopy %%A\Docs\*.* Destination for docs

Open in new window


You'll have to add double-quotes in case your path contains blanks
0
deanmachine333Author Commented:
Hello Michael, thanks for the reply , i take it this will copy say the top folder Folder 1 and within there if exists the 2 other folders and files with in them and copy to new destination in same folder structure?

Also as it seems it doesn't keep the struct and just copies files and overwrites them.

thanks
0
Michael PfisterCommented:
pushd C:\AAA
for /D %%A in (*) do If Exist "%%A\Files" xcopy "%%A\Files\" "Destination\%%A\Files\"&If Exist "%%A\Docs" xcopy "%%A\Docs\" "Destination\%%A\Docs\"
popd

Open in new window

In case Files / Docs folders have subfolders you wish to copy as well, add /S to the two xcopy commands
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

deanmachine333Author Commented:
i have tried it but its saying invalid path ?
error-bat.PNG
0
Michael PfisterCommented:
Sorry

pushd C:\AAA
for /D %%A in (*) do If Exist "%%A\Files" xcopy "%%A\Files" "Destination\%%A\Files\"&If Exist "%%A\Docs" xcopy "%%A\Docs" "Destination\%%A\Docs\"
popd

Open in new window

0
deanmachine333Author Commented:
also sorry , i see its got the & in command is there anything like a OR command to use so if either Docs or Files or Both of these exist to copy to new location?

thanks again
0
Bill PrewIT / Software Engineering ConsultantCommented:
A few clarifying question before I provide a potential approach.

  1. Where do you want the folders copied to?
  2. In the destination folder, do you also want the "Folder1" and "Folder2" parent folder, or are you trying to merge all files from all "Docs" folders into a single destination "Docs" folder?


»bp
0
Michael PfisterCommented:
The "&" just seperates the 2 "If Exist" ...
So this should work if Docs or Files or both folder exist
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, I'll make some assumptions, and here is how I would approach it.  Adjust the two folder paths near the top and give it a test there.  Results of my test here are shown below showing my test folder structure, and the resulting XCOPY commands that were executed.

***** BAT Script *****
@echo off
setlocal

rem Define from and to folder locations
set BaseDir=B:\EE\EE29095370\Base
set DestDir=B:\EE\EE29095370\Dest

rem Enumerate all first level folders on from folder
for /d %%A in ("%BaseDir%\*") do (

    rem Enumerate any "docs" and "files" folders in this subfolder
    for /d %%B in ("%%~A\Docs.*" "%%~A\Files.*") do (

        rem Copy found folders to new location
        xcopy /i /s "%%~B" "%DestDir%\%%~nA\%%~nB"

    )

)

Open in new window

***** Test folder structure *****
B:\EE\EE29095370
+---Dest
\---Base
    +---Folder1
    |   +---Other
    |   +---Files
    |   \---Docs
    +---Folder2
    |   +---Other
    |   +---Files
    |   \---Docs
    +---Folder3
    |   \---Other
    \---Folder4

Open in new window

***** XCOPY commands executed *****
xcopy /i /s "B:\EE\EE29095370\Base\Folder2\Docs" "B:\EE\EE29095370\Dest\Folder2\Docs"
xcopy /i /s "B:\EE\EE29095370\Base\Folder2\Files" "B:\EE\EE29095370\Dest\Folder2\Files"
xcopy /i /s "B:\EE\EE29095370\Base\Folder1\Docs" "B:\EE\EE29095370\Dest\Folder1\Docs"
xcopy /i /s "B:\EE\EE29095370\Base\Folder1\Files" "B:\EE\EE29095370\Dest\Folder1\Files"

Open in new window



»bp
0
deanmachine333Author Commented:
Hello Michael,

I have copied new script , and it appears to work by coping the folder structure and folders and files to new destination , but for the one one folder where i took out the "Files Folder" it didt copy it over , so looks like it only copying if it has both? it very close though :-)
0
Michael PfisterCommented:
Of course it wouldn't execute the 2nd if in case the first one is false .. I've forgotten the brackets
pushd C:\AAA
for /D %%A in (*) do (If Exist "%%A\Files" xcopy "%%A\Files" "Destination\%%A\Files\")&(If Exist "%%A\Docs" xcopy "%%A\Docs" "Destination\%%A\Docs\")
popd

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deanmachine333Author Commented:
Perfect Michael works like a dream :-)

cheers
0
deanmachine333Author Commented:
Works brilliantly, as expected
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
batfile

From novice to tech pro — start learning today.