Remove Redundant Subfolders

This script removes redundant subfolders from all selected folders. I use it mainly to clean up the sometimes surprisingly deep structure of folders that appears after extracting archives.

A subfolder is considered redundant, if it meets all following conditions:

  • It contains no files
  • It contains exactly one subfolder
  • It has the same name as its parentfolder

Example:

Getting run on D:\MyArchive, the script will turn

D:\MyArchive\MyArchive\MyArchive\MyArchive\MyArchive\FinallySomeGoodStuff.txt

into

D:\MyArchive\FinallySomeGoodStuff.txt
function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fso = new ActiveXObject('Scripting.FileSystemObject');

    cmd.deselect = false;
    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.ClearOutput();
    DOpus.Output('Enumerating...\n');

    for (var i = 0; i < tab.selected_dirs.count; ++i) {
        var folderItem = fso.GetFolder(tab.selected_dirs(i));

        if (folderItem.Files.count != 0) continue;
        if (folderItem.SubFolders.count != 1) continue;

        var folderEnum = new Enumerator(folderItem.SubFolders);
        var subFolderItem = folderEnum.item();
        if (folderItem.Name != subFolderItem.Name) continue;

        var cmdLine = 'Copy MOVE FILE="' + subFolderItem + '" TO="' + folderItem.ParentFolder + '"';
        DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);

        --i; // let's do this folder again, in case it now has a new subfolder that meets the criteria
    }
    DOpus.Output('\n... done.');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>Remove Redundant Subfolders</label>
	<tip>Remove single subfolders that have the same name as selected folders</tip>
	<icon1>#move</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fso = new ActiveXObject(&apos;Scripting.FileSystemObject&apos;);</instruction>
		<instruction />
		<instruction>    cmd.deselect = false;</instruction>
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction>    DOpus.Output(&apos;Enumerating...\n&apos;);</instruction>
		<instruction />
		<instruction>    for (var i = 0; i &lt; tab.selected_dirs.count; ++i) {</instruction>
		<instruction>        var folderItem = fso.GetFolder(tab.selected_dirs(i));</instruction>
		<instruction />
		<instruction>        if (folderItem.Files.count != 0) continue;</instruction>
		<instruction>        if (folderItem.SubFolders.count != 1) continue;</instruction>
		<instruction />
		<instruction>        var folderEnum = new Enumerator(folderItem.SubFolders);</instruction>
		<instruction>        var subFolderItem = folderEnum.item();</instruction>
		<instruction>        if (folderItem.Name != subFolderItem.Name) continue;</instruction>
		<instruction />
		<instruction>        var cmdLine = &apos;Copy MOVE FILE=&quot;&apos; + subFolderItem + &apos;&quot; TO=&quot;&apos; + folderItem.ParentFolder + &apos;&quot;&apos;;</instruction>
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        cmd.RunCommand(cmdLine);</instruction>
		<instruction />
		<instruction>        --i; // let&apos;s do this folder again, in case it now has a new subfolder that meets the criteria</instruction>
		<instruction>    }</instruction>
		<instruction>    DOpus.Output(&apos;\n... done.&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>
How to

How to use buttons and scripts from this forum

8 Likes

Yes your code is very helpful for me.
Thanks for sharing with us.

This is PRO! Thanks Ixp!!!!