# Trying to remove "redundant" folders..

## Recommended Posts

Hi,

I thought this would be a quickee utility to code but several hours later I'm still at it.

In a nutshell, I'm trying to recursively eliminate unnecessary folders in some areas to reduce the amount of clicks it takes to access their content. By "unnecessary" I mean any folder that only contains another folder and no files. For example, often when I unzip files I'll wind up with "C:\unzipped_file_dir\unzipped_file_dir\STUFF", or when trying to use an actionscript file as reference I have to navigate through several empty folders to reach the classes. e.g."com\foo\bar\utils\events\Class.as"

Using the above scenarios as examples, I'd like to end up with "C:\unzipped_file_dir\STUFF" and "com\Class.as"

I think the best way to do it is a "looped-loop" similar to this _delEmpty func I found on the board:

Func _delEmpty($dir)$folderList = _FileListToArray($dir, "*", 2) If @error <> 4 Then For$i = 1 to $folderList[0] _delEmpty($dir & "\" & $folderList[$i])
Next
EndIf
$fileList = _FileListToArray($dir, -1, 0)
If @error = 4 Then DirRemove($dir) EndFunc but it got tricky in a hurry for me because the filelist changes once you move files into the parent. I then tried this mess, which actually worked for the most part. It first gets the entire dir structure to an array and then walks thru to create an array of dirs that need to be emptied into their parent. Before performing the move it reverses the array so that all dirs will still exist when they're targetted. I think the biggest prob I had here was that the names often match subdir-names so the move actually put it right back where it started. I switched it up to a copy/remove setup but since I didn't account for checking parent's parent's before moving it would move from c to b to a rather then c to a. This code is probably ridiculous but I'm still posting it so I don't come across as "mooching". Feel free to taunt it. Func _flattenRedundantFolders($dir)
Local $aQueuedMoves[1] Local$folderList = _DirGetFolderStructure($dir);array of all dirs recursed For$i = 1 To $folderList[0];for each dir$arr = DirGetSize($folderList[$i], 3);get file/folder count
If $arr[1] = 0 And$arr[2] = 1 Then;if 0 files and 1 folder
$str =$folderList[$i + 1] & ">" &$folderList[$i];create string-> subdir + ">" + currdir _ArrayAdd($aQueuedMoves, $str);add string to array EndIf Next _ArrayReverse($aQueuedMoves);reverse array so that deepest dirs get moved first

For $i = 0 To UBound($aQueuedMoves) - 1
$sDirs =$aQueuedMoves[$i]$sPathFrom = StringLeft($sDirs, (StringInStr($sDirs, ">") - 1));split str at ">"
$sPathTo = StringTrimLeft($sDirs, StringInStr($sDirs, ">")) DirCopy($sPathFrom, $sPathTo, 1) If DirGetSize($sPathFrom) = DirGetSize($sPathTo) / 2 Then DirRemove($sPathFrom, 1)
ConsoleWrite("Copied " & $sPathFrom & " to " &$sPathTo & @LF)
EndIf
Next
ConsoleWrite("end")
EndFunc  ;==>_flattenRedundantFolders

Any help?

Thanks!

## Create an account

Register a new account