Link to home
Start Free TrialLog in
Avatar of andrejonker
andrejonkerFlag for South Africa

asked on

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

Please consider the topic discussed here: https://www.experts-exchange.com/questions/23486000/Strip-Files-from-Folders-into-Single-Folder.html

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:
C:\MYFOLDER\STRUCTURE\AFOLDER123\AFILE.XLS
C:\MYFOLDER\STRUCTURE\BFOLDER456\BFILE.XLS
C:\MYFOLDER\STRUCTURE\CFOLDER7\CFILE.XLS
C:\MYFOLDER\STRUCTURE\DFOLDER8\DFILE.XLS
C:\MYFOLDER\STRUCTURE\EFOLDER99999999\EFILE.XLS
copied to Destination (Note grouping of files in single folder):
E:\MYFOLDER\STRUCTURE\AFILE.XLS
E:\MYFOLDER\STRUCTURE\BFILE.XLS
E:\MYFOLDER\STRUCTURE\CFILE.XLS
E:\MYFOLDER\STRUCTURE\DFILE.XLS
E:\MYFOLDER\STRUCTURE\EFILE.XLS

Which statement will achieve the following:
Source Structure - Files from Folders (two levels deep):
C:\MYFOLDER\STRUCTURE\LEVELZ\AFOLDER123\AFILE.XLS
C:\MYFOLDER\STRUCTURE\LEVELZ\BFOLDER456\BFILE.XLS
C:\MYFOLDER\STRUCTURE\LEVELY\CFOLDER7\CFILE.XLS
C:\MYFOLDER\STRUCTURE\LEVELX\DFOLDER8\DFILE.XLS
C:\MYFOLDER\STRUCTURE\LEVELX\EFOLDER99999999\EFILE.XLS
copied to Destination (Note grouping of files in single folder):
E:\MYFOLDER\STRUCTURE\LEVELZ\AFILE.XLS
E:\MYFOLDER\STRUCTURE\LEVELZ\BFILE.XLS
E:\MYFOLDER\STRUCTURE\LEVELY\CFILE.XLS
E:\MYFOLDER\STRUCTURE\LEVELX\DFILE.XLS
E:\MYFOLDER\STRUCTURE\LEVELX\EFILE.XLS

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 :-)
Avatar of fhmc
fhmc

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

c:
cd c:\MYFOLDER\STRUCTURE
xcopy /s /i *.xls e:\differentfolder\differentstructure
Avatar of andrejonker

ASKER

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.

C:
cd \myfolder\structure
dir /s/a/b *.xls >> xlscopy.txt
for /f %1 in (xlscopy.txt) do copy %1 e:\myfolder\structure
 
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.

I am thinking perhaps the answer lies here...
http://technet.microsoft.com/en-us/library/bb490969(TechNet.10).aspx
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...  
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.

c:
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.

-fhmc
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
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!!!!
ASKER CERTIFIED SOLUTION
Avatar of fhmc
fhmc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :-)