Strip Files from Folders (two levels deep) into matching destinations (one level deep)

Please consider the topic discussed here:

Mentioned is a source of multiple file-in-folder groups and the CLI/Script routine tp copy them into a single destination.

I wish to take it one level further... If this statement:
 for /r %a in (*.xls) do xcopy "%a" e:\MYFOLDER\STRUCTURE\*.xls
achieves this:
Source Structure:
copied to Destination (Note grouping of files in single folder):

Which statement will achieve the following:
Source Structure - Files from Folders (two levels deep):
copied to Destination (Note grouping of files in single folder):

To explain the two levels / one level thing... block out the C:\MYFOLDER\STRUCTURE and E:\MYFOLDER\STRUCTURE

As before, any assistance in this matter will be greatly appreciated, however I am also going to keep at it :-)
Who is Participating?
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.

why not leverage two of the points you commented on in your referenced link with a slight modification?

1. xcopy /s
2. cd to target folder

xcopy /s /i *.xls e:\differentfolder\differentstructure
andrejonkerAuthor Commented:
In response to fhmc, I'm not sure what that would achieve...

Here is what I am looking for:

for /r %f do (for /r c:\MYFOLDER\STRUCTURE\%f %a in (*.xls) do xcopy "%a" e:\MYFOLDER\STRUCTURE\*.xls)

Not sure whether this can work, will try to test it later, but would accept the solution if someone can confirm this to be correct or fix it so it would work...
I didn't read your original post correctly... I was thinking you wanted to maintain the subfolder structure from C:\folder\structure in the new location.  

If I read your question correctly, you want to grab all *.xls files recursively in a folder structure to a SINGLE folder...
Ignoring a means to deal with two files that have the same name (e.g.  1\2\3\a.xls and 1\2\a.xls) here's ONE solution.

cd \myfolder\structure
dir /s/a/b *.xls >> xlscopy.txt
for /f %1 in (xlscopy.txt) do copy %1 e:\myfolder\structure
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

argh... nevermind.  I mistyped some of the commands in my last post (should have been dir /as/b) and the suggeted plan doesn't work anyway.

andrejonkerAuthor Commented:
I am thinking perhaps the answer lies here...
using pushd and popd to run through the one layer of folders inside the for structure somehow to then strip the xls files from the second layer...  
andrejonkerAuthor Commented:
Back to the for statement... Here is something interesting:

/D        Indicates that the set contains directories.

An example that would list all the directories (but not sub-directories) on the C: drive is for /d %%X in (C:\*) do echo %%X
in an attempt to redeem myself a bit, let's go for strike three.

cd \myfolder\structure
for /d %a in (*.*) do for /d %b in (%a\*.*) do for %c in (%b\*.xls) do copy %c e:\myfolder\structure

It took a little while to sink into my head exactly what it was you were trying to do... that along w/ the fact I rarely have a need to nest for loops... most of the FOR stuff I do doesn't require nesting.

That said, I DID test my suggestion successfully before posting it here.

ALSO, I made two assumptions... They are as follows:
1.  e:\myfolder\structure is present
2.  no two *.xls files share the same name at any subfolders

If I overlooked something else, please indicate so and I'll try to modify my logic to accommodate it.

side note:

if you plan to employ my suggested approach via a batch type process, you will need to pre-pend a leading % character... e.g. NOT for /d %a, rather, for /d %%a, etc..
err...  rather:
for /d %a in (*.*) do for /d %b in (%a\*.*) do for %c in (%b\*.xls) do copy %c e:\myfolder\structure\%a

You will not achieve your desired result w/ this command if the directory structure is not present.
One way to create the relative directory structure would be as follows:

for /d %a in (*.*) do for /d %b in (%a\*.*) do for %c in (%b\*.xls) do md e:\myfolder\structure\%a |copy %c e:\myfolder\structure\%a
andrejonkerAuthor Commented:
Nearly there!!!

The for .... | copy .... sequence doesn't work, so I ran these two statements to achieve the required result:

cd c:\myfolder\structure
for /d %a in (*.*) do for /d %b in (%a\*.*) do for %c in (%b\*.xls) do md e:\myfolder\structure\%a
for /d %a in ("*.*") do for /d %b in ("%a\*.*") do for /r %c in ("%b\*.xls") do copy "%c" "e:\myfolder\structure\%a\*.*"

Note the changes I made to the second one to support long file names.
Also the folder create is too recursive or repetitive, because you actually see it trying to recreate the same folder multiple times - once for each file instance. I have 100's of files... So I'd like to streamline it a bit, but so far it is running.

Not quite there yet, but well done!!!!
odd, the piped "md e:\..." command didn't work for you.  It worked for me in my XP test environment.

I don't have a good answer for you so far as the excessive iterations of the loop is concerned.  The first two thoughts that come to mind are as follows:

ex 1.
the trouble with this solution is it will recreate the root folder structure from c:\myfolder\structure in e:\myfolder\structure whether an .xls file exist in the c:subfolder environment or not (this may be acceptable for you, but it's worth noting.)

for /d %a in (*.*) do md e:\myfolder\structure\%a

another option could be:
hmm... actually, the following solution might be the answer...
for /d %a in (*.*) do for /d %b in (%a\*.*) do if exist %b\*.xls md e:\myfolder\structure\%a

Let me know how it works.

cd c:\myfolder\structure
for /d %a in (*.*) do for /d %b in (%a\*.*) do if exist %b\*.xls md e:\myfolder\structure\%a
for /d %a in ("*.*") do for /d %b in ("%a\*.*") do for /r %c in ("%b\*.xls") do copy "%c" "e:\myfolder\structure

also, I see you added the /r switch in the %c loop.  What benefit did this offer?  I didn't use the /r switch in any of my tests.

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
andrejonkerAuthor Commented:
Thanks for the help here! I can't remember why I put in the /r in the c% loop. I got an error and it would not work without it. If this is of any help, I used WinXP Pro SP2, maybe it runs different elsewhere. Also, I've been switching between command line (CLI)  and batch files (CMD) and could've gotten myself confused :-)
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
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.