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