1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: command script to flatten a tree structure

Discussion in 'DOS/Other' started by ttx336, Feb 22, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    My Kenwood deck in my truck does not seem to work with a tree structure on a 32GB micro SD that I just bought but it worked fine with an 8GB micro SD in which I had flattened the tree structure. Not wanting to wade through all 5,000+ songs I attempted to write a script. I have had mixed success, here are two that I wrote:

    Code:
    @echo off 
    cls
    M:
    cd \iTunes\Music
    for /d %%X in (*) do (
    	if not exist "c:\users\gary\test\%%X" 	(
    		echo Directory: "c:\users\gary\test\%%X" is being created
    		md "c:\users\gary\test\%%X"
    		)		
    		cd \iTunes\Music\%%X
    
    	forfiles /S /M *.mp3 /C "cmd /c copy @fname.mp3 c:\users\gary\test\%%X\"
    )
    C:
    cd \Users\gary
    Above is my second attempt, it runs only sub-folders that have no spaces, try as I might, I could not come up with the proper syntax to correct that...

    Next up, my first attempt...

    Code:
    @echo off 
    cls
    M:
    cd \iTunes\Music
    for /d %%X in (*) do (
    	if not exist "D:\%%X" 	(
    		echo Directory: "D:\%%X" is being created
    		md "D:\%%X"
    		)		
    		cd \iTunes\Music\%%X
    
    	for /d %%Z in (*) do (
    		cd \iTunes\Music\%%X\%%Z
    		if exist *.mp3 (
    			if not exist "D:\%%X\*.mp3" copy *.mp3 "D:\%%X\"
    			)
    
    		if exist *.m4a (
    			if not exist "D:\%%X\*.m4a" copy *.m4a "D:\%%X\"
    			)
    		cd \iTunes\Music\%%X
    		)
    )
    
    C:
    cd \Users\gary\Dropbox\Computers\Batch		
    
    After several hours I thought I had this working but later learned that it only copies the FIRST sub-folder that it encounters, then it returns back to the initial "for" loop and gets the next top-level folder. I cannot understand why that is; the syntax of the second for loop is the same as the first for loop but it just won't iterate through the sub-folder structure.

    The M:\iTunes\Music\ is structured thusly: The top level is the artist name, under that is/are a sub-folder(s) that is labeled as the album name, in that/those sub-folder(s) are the mp3 or m4a files.

    Thanks in advance,
    -Gary
     
  2. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    This should copy all the MP3 and M4a files to Z:\ assuming the M4a files have no drm.

    If the sd card is formatted in fat32 then there is a root directory limit for the number of files.



    Code:
    @echo off
    pushd "M:\iTunes\Music"
    for /f "delims=" %%a in ('dir *.mp3 *.m4a /b /s /a-d') do copy /b "%%a" Z:\
    popd
     
  3. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    Will this create the top-level (Artist Name) folder in the Z:\ drive and copy the corresponding .mp3 and .m4a files into it?
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    The 3rd party utility XXcopy is really good at flattening directories.
     
  5. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    That's not flattening the directory structure.
     
  6. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    I'm afraid that my terminology was misleading, I am seeking to flatten the structure insofar as limiting it to the top-level folder that holds the Artist's Name... hopefully this is possible...

    Thanks, Squashman, I will check out XXcopy, however I am limited to what software I can use at work; the things that I do at home, such as this music/mp3/m4a project are practice and practical application for things that I do here at work, so I need to be able to do this with command script.

    I tried foxidrive's code and it doesn't create the top-level directory, I realized that I was misleading in stating that I wanted to flatten the structure, I am very sorry...
     
  7. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    I regret using that term so loosely, is it possible to "flatten" each of the "Artist's Name" folders? Is that a better way to phrase the question?

    I need to retain the top-level or I will not be able to search by artist in my stereo...

    -Gary
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    XXcopy is cmd line utility and now you are changing the parameters of this problem. You went from personal use to work use. I really like people to be honest with me upfront.
     
  9. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Untested: It should retain the top level structure.

    Code:
    @echo off
    pushd "M:\iTunes\Music"
    for /f "delims=" %%z in ('dir /b /ad') do (
    pushd "%%z"
    for /f "delims=" %%a in ('dir *.mp3 *.m4a /b /s /a-d') do xcopy "%%a" "Z:\%%z\"
    popd
    )
    popd
    
     
  10. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    this project is for home, I do at times, use command scripts at work and the practice that I get at home comes in handy at work.
    the mainstay of my batch/script work is done at home, though.

    You initially said XXcopy was a third party utility, but now you have said that it is a command line utility, I am not sure I understand... is it part of a resource kit?
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    So you think 3rd party utilities cannot run from the command line?
     
  12. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    XXCopy is a third party command line tool that is free for personal use.

    For flattening folder structure and lots of other uses such as manipulating date ranges it is very handy and worth knowing about.
     
  13. ttx336

    ttx336 Thread Starter

    Joined:
    Nov 7, 2012
    Messages:
    49
    I'm watching it run right now, seems to be doing just fine, do you accept contributions/donations? You have been such a big help to me, this is like the third time you've solved a mystery for me...

    I can PayPal it if you pm an email to me; or however you wish to do it is fine with me!

    -Gary
     
  14. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Thanks for the offer - this is my hobby and I do it for the mental exercise and to help people.

    If you help someone in turn, maybe something totally unrelated to batch files, then it's all good. :)
     
  15. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Ditto. This is free help forum. Everyone should give back by helping someone else. We have lots of forum categories here. Browse around and see if you can help.
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1090580

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice