Notifications
Clear all

[Closed] parsing tabs as folder depth

We have a subscription to CGTextures.com and we try to keep our image library organized in a similar folder structure.

I would like to parse just the folder names of the directory tree on that site (hope that’s ok) to generate a folder structure on our asset drive.

I expanded all of the tree branches and copied the text of that frame into a file. It created a sort of tab delimited file with each new branch one tab indented.

I figured out a couple different ways of approaching this both using

((filterString thisLine "	" SplitEmptyTokens:true).count)

to count the tabs in each line to make some sort of comparison of the depth at each line.

	Textures
  		Animals
  			Birds
  				Feathers
  				Ostrich
  				Peacock
  				Pelican
  				Various Birds
  			Bison
  			Domestic
  			Elephant
  				Closeups
  				Full Body
  				Head
  			Fish
 etc....
  

I realize I have to somehow track where in the depth of the tree each line is and keep note of the parent folder… my head hurts…

clearListener()
 
 catFile = openFile @"E:\Materials\CGTexturesCategoryList.txt"
 seek catFile 0
 theRoot = @"E:\Test"
 newRoot = theRoot
 thisDir = newRoot
 prevDepth = 0
 prevDir = newRoot as string 
 while not eof catFile do 
 (
 	thisLine = readLine catFile
 	
 	--compare this line depth to the previous depth. if it is one greater then the previous line is the newRoot
 	thisDepth = ((filterString thisLine "	" SplitEmptyTokens:true).count)-1
 	if (thisDepth == prevDepth + 1)
 		then
 			(
 				newRoot = prevDir
 				--format "new Root: %
" newRoot
 				thisDir = (FilterString thisLine "	")[1]
 				--format "this Dir: %
" thisDir
 				newDir = theRoot + "\\" + newRoot + "\\" + thisDir
 				format "new Dir: %
" newDir
 				makeDir newDir
 			)
 		else
 			(
 				thisDir = (FilterString thisLine "	")[1]
 				newDir = theRoot + "\\"+ newRoot + "\\" + thisDir
 				makeDir newDir
 			)
 
 	prevDir = thisDir
 	prevDepth = thisDepth
 	)
 	
 close catFile
2 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

use trimleft str ” “. the deference of old str count and new one gives you the depth.

I still counted the tabs/depth my old way… but finally got this to work…

(
 	clearListener()
 	catFile = openFile @"E:\Materials\CGTexturesCategoryList.txt"
 	seek catFile 0
 	thePathArray = filterstring @"E:\Test\" "\\"
 	theFolderList = #()
 	prevDepth = 1
 	folderCount = 0
 	startTime = timestamp() as float
 	while not eof catFile do 
 	(
 		folderItem = #()
 		thisLine = readLine catFile
 		thisDepth = ((filterString thisLine "	" SplitEmptyTokens:true).count)
 		depthDiff = (prevDepth - thisDepth)
 		(
 			append folderItem (FilterString thisLine "	")[1]
 			append folderItem depthDiff
 			if folderItem[1] != undefined do append theFolderList folderItem
 			prevDepth = thisDepth
 		)
 		
 	)
 	format "Folder Count: %
" theFolderList.count
 	
 	for f = 1 to theFolderList.count do 
 	(
 		thisDir = theFolderList[f][1]
 		thisPath = ""
 		pACount = thePathArray.count
 		deleteCount = theFolderList[f][2]
 		
 		if deleteCount >= 0
 			then
 			(
 				for d = pACount to pACount - (deleteCount) by -1 do 
 				(
 					deleteitem thePathArray d
 				)
 				append thePathArray thisDir
 				
 				for p = 1 to thePathArray.count do
 					(
 						append thisPath (thePathArray[p] + "//")
 						
 					)
 				makedir thisPath
 			)
 			else
 			(
 				append thePathArray thisDir
 				for p = 1 to thePathArray.count do
 					(
 						append thisPath (thePathArray[p] + "//")
 					)
 				makedir thisPath
 			)
 	)
 	
 	endTime = timestamp() as float
 	format "Created % folders in % seconds
"  theFolderList.count ((endTime-startTime)/1000)
 close catFile
 )