# Solved: command script to flatten a tree structure



## ttx336 (Nov 7, 2012)

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:


```
@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...


```
@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


----------



## foxidrive (Oct 20, 2012)

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.


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


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


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


Will this create the top-level (Artist Name) folder in the Z:\ drive and copy the corresponding .mp3 and .m4a files into it?


----------



## Squashman (Apr 4, 2003)

The 3rd party utility XXcopy is really good at flattening directories.


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> Will this create the top-level (Artist Name) folder in the Z:\ drive and copy the corresponding .mp3 and .m4a files into it?


That's not flattening the directory structure.


----------



## ttx336 (Nov 7, 2012)

Squashman said:


> The 3rd party utility XXcopy is really good at flattening directories.


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


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> That's not flattening the directory structure.


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


----------



## Squashman (Apr 4, 2003)

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.


----------



## foxidrive (Oct 20, 2012)

Untested: It should retain the top level structure.


```
@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
```


----------



## ttx336 (Nov 7, 2012)

Squashman said:


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


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?


----------



## Squashman (Apr 4, 2003)

So you think 3rd party utilities cannot run from the command line?


----------



## foxidrive (Oct 20, 2012)

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.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


> Untested: It should retain the top level structure.
> 
> 
> ```
> ...


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


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> 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...
> 
> -Gary


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.


----------



## Squashman (Apr 4, 2003)

foxidrive said:


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


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.


----------



## ttx336 (Nov 7, 2012)

foxidrive said:


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


very similar for me, although, and I've told you this before, I am quite amazed by you... I wrote code for a living, albeit a very specialized type of code, and I still am stumped quite often by this command scripting issue. More often than not, I must defer to you to get my projects done. Thankfully, they're also almost always for the mental exercise, it keeps me thinking in terms of logical, conditional, reasoning.

The code that I write is for CNC machines, do you know that those are? I help others on machining forums, I really, really enjoy being able to share what I know, however much or little that may be...

I also contribute on automotive forums as well... another hobby of mine.

-Gary


----------



## ttx336 (Nov 7, 2012)

Squashman said:


> So you think 3rd party utilities cannot run from the command line?


no, of course not, each of these scripts that we write are essentially 3rd party...

my question was whether XXcopy would need to be searched for and downloaded from a vendor or if it was part of a resource kit from Microsoft; I seem to remember that in one of your or foxidrive's replies to me or possibly to someone else, in the past, about the resource kit...


----------



## foxidrive (Oct 20, 2012)

ttx336 said:


> I wrote code for a living, albeit a very specialized type of code, and I still am stumped quite often by this command scripting issue. More often than not, I must defer to you to get my projects done.


I know very well that batch script is full of inconsistent syntax and has lots of 'gotcha' moments with poison characters and special conditions and undocumented techniques. I only know it as I do from long experience (msdos 3.1 and later) and lots of personal projects. It's a bit of fun though. It would be difficult to come to grips with starting off with NT syntax and not having the grounding from earlier and simpler (but more convoluted) days.



> The code that I write is for CNC machines, do you know that those are? I help others on machining forums, I really, really enjoy being able to share what I know, however much or little that may be...
> 
> I also contribute on automotive forums as well... another hobby of mine.


Then you already share lots, Gary. I know CNC is for computer aided tools but I know nothing of the language or syntax. My brother was interested in CNC with routers in very early days but it was an armchair interest (when everything cost 3 or 4 Mint's worth).

Cheers, I hope the task worked for you.


----------



## ttx336 (Nov 7, 2012)

yes, it did, it worked a treat! a side benefit of doing it this way is that it necessarily eliminated duplicates.

I also started out in the DOS world, and I believe 3~ish is when I came on the scene, it was around 1986, my first PC was an IBM PS2, it was a form factor not unlike the original iMac, an all-in-one machine. It came with two 3-1/2 floppies, I was SO excited when I finally was able to put a 20MB HDD in it! Man, I thought I was walking in tall cotton then!

I wrote many, many utility programs in Pascal, still my fav programming language, then C and Assembler... I, unfortunately, did not keep up and lost much ground when DOS went by the wayside, now I am playing catch-up.


----------

