Delete only empty file/empty directory or which contain empty files

Hello,

I made this button so I wanted to share it with you in case it is useful for you as it is for me :slight_smile:

The needed :

  • I wanted to delete SECURELY some folder and files which are empty (empty folder or folders containing empty files, and empty files).

So I wrote this button in order to warm me if some files or directories are not empty (in that case I know that the files or directories are not really empty are I supposed).

The button :slight_smile:

@nodeselect
@script jscript

function CheckIfDirectoryNotContainFiles(path) {
	flag = false;
	
	DOpus.Output("Working on path : \"" + path + "\"");
	
	var folderEnum = DOpus.FSUtil.ReadDir(path, true);

	while (!folderEnum.complete)
	{
		childElementPath = folderEnum.next();
		
		type = DOpus.FSUtil.GetType(childElementPath);
		
		DOpus.Output("Discovered Child Element path : \"" + childElementPath + "\" of type \"" + type + "\".");
		
		if (type == "dir") {
			result = CheckIfDirectoryNotContainFiles(childElementPath);

			if (result == true)
			{
				DOpus.Output("Setting flag to true because there is a directory \"" + childElementPath + "\" inside this directory \"" + path + "\" which contain a not empty file.");
				
				flag = true;

				break;
			}
		}
		else
		{
			result = CheckIfFileIsEmpty(childElementPath);

			if (result == true)
			{
				DOpus.Output("Setting flag to true because there is a file \"" + childElementPath + "\" inside this directory \"" + path + "\" which is not empty file.");
				
				flag = true;
				
				break
			}		
		}
	}

	return flag;
}

function CheckIfFileIsEmpty(path) {
	flag = false;
	
	fso = new ActiveXObject("Scripting.FileSystemObject");

	file = fso.GetFile(path);
	fileSize = file.size;
	
	DOpus.Output("Discover file : \"" + path + "\" with size \"" + fileSize + "\".");

	if (file.size > 0)
	{
		DOpus.Output("Setting flag to true cause the file is not empty.");

		flag = true;
	}
	else
	{
		DOpus.Output("Setting flag to false cause the file is empty.");
	}

	return flag;
}

function OnClick(ClickData) {

	var cmd = ClickData.func.command;
	cmd.deselect = false;

	var tab = ClickData.func.sourcetab;
	var count = tab.selstats.selfiles;
	var notEmptyFiles = [];
	var notEmptyDirs = [];

	//DOpus.Output (count);
	
	var enumFiles  = new Enumerator(tab.selected);
	
	for (enumFiles; !enumFiles.atEnd(); enumFiles.moveNext())
	{	
		path = enumFiles.item()
		
		type = DOpus.FSUtil.GetType(path);
		
		DOpus.Output("Type : " + type);

		
		if (type == "dir") {
			flag = CheckIfDirectoryNotContainFiles(path);

			if (flag)
			{
				notEmptyDirs.push(DOpus.FSUtil.NewPath(path).filepart);
			}
		}
		else
		{
			flag = CheckIfFileIsEmpty(path);

			if (flag)
			{
				notEmptyFiles.push(DOpus.FSUtil.NewPath(path).filepart);
			}		
		}
	}
	
	if (notEmptyDirs.length > 0 || notEmptyFiles.length > 0)
	{
		DOpus.Output("No Empty");
		
		var msg = ""
		
		msg += "At least"
		
		if (notEmptyDirs.length > 0) {
			msg += " one directory"
		}
		
		if (notEmptyFiles.length > 0) {
			if (notEmptyDirs.length > 0) {
				msg += " and"
			}
			
			msg += " one file"
		}
		
		msg += " not empty !"
		msg += "\n"
		msg += "\n"

		if (notEmptyDirs.length > 0) {
			msg += "Directories : " + '\n'
		}
		for (var i = 0; i < notEmptyDirs.length; i++)
		{
			msg += "- " + notEmptyDirs[i] + '\n'
		}
		
		if (notEmptyFiles.length > 0) {
			msg += "Files : " + '\n'
		}
		for (var i = 0; i < notEmptyFiles.length; i++)
		{
			msg += "- " + notEmptyFiles[i] + '\n'
		}
		
		DOpus.Dlg.Request(msg, "Cancel", "Cancel", dlg);
	}
	else
	{
		DOpus.Output("Empty");

		var dlg = DOpus.Dlg
		var msg = ""
		msg += "All directories/files are empty !"
		msg += "\n"
		msg += "Confirm deletion ?"
		var result = DOpus.Dlg.Request(msg, "Confirm|Cancel", "Confirm", dlg);
		
		DOpus.Output (result);
		
		if (result == 1)
		{
			cmd.RunCommand("Delete QUIET");
		}
	}
}

3 Likes

Thanks for sharing your script!

It might be best to comment out all the DOpus.Output(...) debugging since most people won't want that in their script logs.

The Delete command's SKIPNOTEMPTY and FAILNOTEMPTY arguments might also be of interest. (Note that they need to be combined with NORECYCLE, and are slightly different to what the script does, but similar in some ways.)

1 Like

Hi Leo :slight_smile:

Thank for you comment.

I didn't know these arguments, very good idea :slight_smile:
Also, this code allow us to know which files/directories are not "empty", so it's a little thing in more.

Very happy you like it :slight_smile: Hope it will be useful for other people :slight_smile:

1 Like

This is a very Helpful script.
But if the button can delete the empty folder which contain empty file from the selected folders that would be more helpful.
Thanks for the script

I would expect we could select all the items in the list, click the button, and all empty files and folders would be deleted leaving the other non empty items untouched. But the script only prevents user from deleting anything as long the selection contains non empty items. I don’t see the point.

Made some changes to make this button more useful to me, it might be what you wanted.
:exclamation:ReadDir flags is set to "a", so that archives will be considered as files rather than directories.

xml:

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" separate="yes" textcol="none">
	<label>Delete Empty Items</label>
	<tip>Delete all empty items in selection or the current folder.</tip>
	<icon1>#delete</icon1>
	<function type="script">
		<instruction>@nodeselect</instruction>
		<instruction>@script jscript</instruction>
		<instruction />
		<instruction>function CheckIfDirectoryIsEmpty (path) {</instruction>
		<instruction>    flag = true;</instruction>
		<instruction>    //DOpus.Output(&quot;Working on path : \&quot;&quot; + path + &quot;\&quot;&quot;);</instruction>
		<instruction>    var folderEnum = DOpus.FSUtil.ReadDir(path, &quot;r&quot;);</instruction>
		<instruction>    while (!folderEnum.complete) {</instruction>
		<instruction>        childElementPath = folderEnum.next();</instruction>
		<instruction>        type = DOpus.FSUtil.GetType(childElementPath, &quot;a&quot;);</instruction>
		<instruction>        //DOpus.Output(&quot;Discovered child element path : \&quot;&quot; + childElementPath + &quot;\&quot; of type \&quot;&quot; + type + &quot;\&quot;.&quot;);</instruction>
		<instruction>        if (type == &quot;dir&quot;) {</instruction>
		<instruction>            result = CheckIfDirectoryIsEmpty(childElementPath);</instruction>
		<instruction>            if (result == false) {</instruction>
		<instruction>                //DOpus.Output(&quot;Setting flag to false because there is a directory \&quot;&quot; + childElementPath + &quot;\&quot; inside this directory \&quot;&quot; + path + &quot;\&quot; which contain a not empty file.&quot;);</instruction>
		<instruction>                flag = false;</instruction>
		<instruction>                break;</instruction>
		<instruction>            }</instruction>
		<instruction>        } else {</instruction>
		<instruction>            result = CheckIfFileIsEmpty(childElementPath);</instruction>
		<instruction>            if (result == false) {</instruction>
		<instruction>                //DOpus.Output(&quot;Setting flag to false because there is a file \&quot;&quot; + childElementPath + &quot;\&quot; inside this directory \&quot;&quot; + path + &quot;\&quot; which is not empty file.&quot;);</instruction>
		<instruction>                flag = false;</instruction>
		<instruction>                break</instruction>
		<instruction>            }</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction>    return flag;</instruction>
		<instruction>}</instruction>
		<instruction />
		<instruction>function CheckIfFileIsEmpty (path) {</instruction>
		<instruction>    flag = true;</instruction>
		<instruction>    fso = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);</instruction>
		<instruction>    file = fso.GetFile(path);</instruction>
		<instruction>    fileSize = file.size;</instruction>
		<instruction>    //DOpus.Output(&quot;Discover file : \&quot;&quot; + path + &quot;\&quot; with size \&quot;&quot; + fileSize + &quot;\&quot;.&quot;);</instruction>
		<instruction>    if (file.size &gt; 0) {</instruction>
		<instruction>        //DOpus.Output(&quot;Setting flag to false cause the file is not empty.&quot;);</instruction>
		<instruction>        flag = false;</instruction>
		<instruction>    } else {</instruction>
		<instruction>        //DOpus.Output(&quot;Setting flag to true cause the file is empty.&quot;);</instruction>
		<instruction>    }</instruction>
		<instruction>    return flag;</instruction>
		<instruction>}</instruction>
		<instruction />
		<instruction>function OnClick (ClickData) {</instruction>
		<instruction>    var cmd = ClickData.func.command;</instruction>
		<instruction>    cmd.deselect = true;</instruction>
		<instruction>    var tab = ClickData.func.sourcetab;</instruction>
		<instruction>    var selected = tab.selected;</instruction>
		<instruction>    var count = tab.selstats.selfiles;</instruction>
		<instruction>    var emptyFiles = [];</instruction>
		<instruction>    var emptyDirs = [];</instruction>
		<instruction>    //DOpus.Output (count);</instruction>
		<instruction>    if (selected.count == 0) { //if no item is selected</instruction>
		<instruction>        selected = ClickData.func.sourcetab.all; //handle all item in the current directory</instruction>
		<instruction>    }</instruction>
		<instruction>    var enumFiles = new Enumerator(selected);</instruction>
		<instruction>    for (enumFiles; !enumFiles.atEnd(); enumFiles.moveNext()) {</instruction>
		<instruction>        path = enumFiles.item()</instruction>
		<instruction>        type = DOpus.FSUtil.GetType(path, &quot;a&quot;);</instruction>
		<instruction>        //DOpus.Output(&quot;Type : &quot; + type);</instruction>
		<instruction>        if (type == &quot;dir&quot;) {</instruction>
		<instruction>            flag = CheckIfDirectoryIsEmpty(path);</instruction>
		<instruction>            if (flag == true) {</instruction>
		<instruction>                emptyDirs.push(DOpus.FSUtil.NewPath(path).filepart);</instruction>
		<instruction>                cmd.AddFile(path);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else {</instruction>
		<instruction>            flag = CheckIfFileIsEmpty(path);</instruction>
		<instruction>            if (flag == true) {</instruction>
		<instruction>                emptyFiles.push(DOpus.FSUtil.NewPath(path).filepart);</instruction>
		<instruction>                cmd.AddFile(path);</instruction>
		<instruction>            }</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction>    if (emptyDirs.length &gt; 0 || emptyFiles.length &gt; 0) {</instruction>
		<instruction>        //DOpus.Output(&quot;Found empty item&quot;);</instruction>
		<instruction>        var msg = &quot;&quot;</instruction>
		<instruction>        msg += &quot;Found &quot;</instruction>
		<instruction>        if (emptyDirs.length &gt; 0) {</instruction>
		<instruction>            msg += emptyDirs.length + &quot; empty folder&quot;</instruction>
		<instruction>            if (emptyDirs.length &gt; 1) {</instruction>
		<instruction>                msg += &quot;s&quot;</instruction>
		<instruction>            }</instruction>
		<instruction>        }</instruction>
		<instruction>        if (emptyFiles.length &gt; 0) {</instruction>
		<instruction>            if (emptyDirs.length &gt; 0) {</instruction>
		<instruction>                msg += &quot; and &quot;</instruction>
		<instruction>            }</instruction>
		<instruction>            msg += emptyFiles.length + &quot; empty file&quot;</instruction>
		<instruction>            if (emptyFiles.length &gt; 1) {</instruction>
		<instruction>                msg += &quot;s&quot;</instruction>
		<instruction>            }</instruction>
		<instruction>            msg += &quot;!&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        msg += &quot;\n&quot;</instruction>
		<instruction>        msg += &quot;\n&quot;</instruction>
		<instruction>        if (emptyDirs.length &gt; 0) {</instruction>
		<instruction>            msg += &quot;Empty folder&quot;</instruction>
		<instruction>                if (emptyDirs.length &gt; 1) {</instruction>
		<instruction>                    msg += &quot;s&quot;</instruction>
		<instruction>                }</instruction>
		<instruction>            msg += &quot;:&quot; + &quot;\n&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        for (var i = 0; i &lt; emptyDirs.length; i++) {</instruction>
		<instruction>            msg += &quot;- &quot; + emptyDirs[i] + &quot;\n&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        if (emptyFiles.length &gt; 0) {</instruction>
		<instruction>            if (emptyDirs.length &gt; 0) {</instruction>
		<instruction>                msg += &quot;\n&quot;</instruction>
		<instruction>            }</instruction>
		<instruction>            msg += &quot;Empty file&quot;</instruction>
		<instruction>                if (emptyFiles.length &gt; 1) {</instruction>
		<instruction>                    msg += &quot;s&quot;</instruction>
		<instruction>                }</instruction>
		<instruction>            msg += &quot;:&quot; + &quot;\n&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        for (var i = 0; i &lt; emptyFiles.length; i++) {</instruction>
		<instruction>            msg += &quot;- &quot; + emptyFiles[i] + &quot;\n&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        msg += &quot;\n&quot;</instruction>
		<instruction>        msg += &quot;Confirm deletion?&quot;</instruction>
		<instruction>        var dlg = DOpus.Dlg</instruction>
		<instruction>        var result = DOpus.Dlg.Request(msg, &quot;Confirm|Select Only|Cancel&quot;, &quot;Confirm&quot;, dlg);</instruction>
		<instruction>        if (result == 1) {</instruction>
		<instruction>            //DOpus.Output (&quot;Deleted&quot;);</instruction>
		<instruction>            //DOpus.Output (&quot;&quot;);</instruction>
		<instruction>            cmd.RunCommand(&quot;Select FROMSCRIPT DESELECTNOMATCH&quot;);</instruction>
		<instruction>            cmd.RunCommand(&quot;Delete QUIET&quot;);</instruction>
		<instruction>        } else if (result == 2) {</instruction>
		<instruction>            //DOpus.Output (&quot;Selected&quot;);</instruction>
		<instruction>            //DOpus.Output (&quot;&quot;);</instruction>
		<instruction>            cmd.RunCommand(&quot;Select FROMSCRIPT DESELECTNOMATCH&quot;);</instruction>
		<instruction>        } else {</instruction>
		<instruction>            //DOpus.Output (&quot;Canceled&quot;);</instruction>
		<instruction>            //DOpus.Output (&quot;&quot;);</instruction>
		<instruction>        }</instruction>
		<instruction>    } else {</instruction>
		<instruction>        //DOpus.Output(&quot;No empty item&quot;);</instruction>
		<instruction>        //DOpus.Output (&quot;&quot;);</instruction>
		<instruction>        var msg = &quot;&quot;</instruction>
		<instruction>        msg += &quot;No empty item was found&quot;</instruction>
		<instruction>        if (tab.selected.count != 0) { //if no item is selected</instruction>
		<instruction>            msg += &quot; in seleted items&quot;</instruction>
		<instruction>        }</instruction>
		<instruction>        msg += &quot;!&quot;</instruction>
		<instruction>        var dlg = DOpus.Dlg</instruction>
		<instruction>        var result = DOpus.Dlg.Request(msg, &quot;Confirm&quot;, &quot;Confirm&quot;, dlg);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>
1 Like