Solved

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

Posted on 2008-06-17
12
371 Views
Last Modified: 2010-04-16
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
Comment
Question by:andrejonker
  • 7
  • 5
12 Comments
 
LVL 7

Expert Comment

by:fhmc
ID: 21808723
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
 

Author Comment

by:andrejonker
ID: 21810646
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
 
LVL 7

Expert Comment

by:fhmc
ID: 21813497
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
 
LVL 7

Expert Comment

by:fhmc
ID: 21814985
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
 

Author Comment

by:andrejonker
ID: 21815579
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
 

Author Comment

by:andrejonker
ID: 21815641
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:fhmc
ID: 21818368
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
 
LVL 7

Expert Comment

by:fhmc
ID: 21819598
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
 
LVL 7

Expert Comment

by:fhmc
ID: 21823069
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
 

Author Comment

by:andrejonker
ID: 21824348
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
 
LVL 7

Accepted Solution

by:
fhmc earned 500 total points
ID: 21827199
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
 

Author Comment

by:andrejonker
ID: 21911554
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now