• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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

Please consider the topic discussed here: http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_23486000.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 :-)
0
andrejonker
Asked:
andrejonker
  • 7
  • 5
1 Solution
 
fhmcCommented:
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
0
 
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...
0
 
fhmcCommented:
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
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

0
 
andrejonkerAuthor Commented:
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...  
0
 
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
0
 
fhmcCommented:
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
0
 
fhmcCommented:
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..
0
 
fhmcCommented:
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
0
 
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!!!!
0
 
fhmcCommented:
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.


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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now