Create separate RARs from selected items, then delete them

in 2019 I post That. Then Dear Leo give me this script. This script works fine but needs some tweaks. This script Leaves the original file. But I want to move the original file into the rar file. Because the Original file Occupies double space in my Hard Drive. Here is the script:

function OnClick(clickData)
{
	var source = clickData.func.sourcetab;
	var cmd    = clickData.func.command;
	var work   = false;
	cmd.deselect = false; // Leave source files selected.
	cmd.ClearFiles();

	for (var e = new Enumerator(source.selected); !e.atEnd(); e.moveNext())
	{
		var f = e.item();

		if (IsEmptyDir(f))
		{
			var arcName = f.Path + "\\" + f.name_stem_m + ".rar";
			if (!DOpus.FSUtil.Exists(arcName))
			{
				work = true;
				cmd.AddLine("CreateFolder NAME=\"" + arcName + "\" ARCHIVE=.rar READAUTO=no NOUPDATESETTINGS");
			}
		}
		else
		{
			work = true;
			cmd.AddLine("Copy FILE=\"" + f.RealPath + "\" TO=\"" + f.Path + "\" ARCHIVE=.rar CREATEFOLDER=\"" + f.name_stem_m + ".rar\"");
		}
	}

	if (work)
	{
		cmd.Run();
	}
}

function IsEmptyDir(item)
{
	if (!item.is_dir)
	{
		return false;
	}

	var fe = DOpus.FSUtil.ReadDir(item.RealPath);

	return fe.complete;	
}

I don't generally recommend this because it may not handle all error situations, and leave you with a corrupt/incomplete archive and the source files deleted as well, but if you really want to, you can add this line:

		cmd.AddLine("Delete QUIET FILE=\"" + f.RealPath + "\"");

Like this:

function OnClick(clickData)
{
	var source = clickData.func.sourcetab;
	var cmd    = clickData.func.command;
	var work   = false;
	cmd.deselect = false; // Leave source files selected.
	cmd.ClearFiles();

	for (var e = new Enumerator(source.selected); !e.atEnd(); e.moveNext())
	{
		var f = e.item();

		if (IsEmptyDir(f))
		{
			var arcName = f.Path + "\\" + f.name_stem_m + ".rar";
			if (!DOpus.FSUtil.Exists(arcName))
			{
				work = true;
				cmd.AddLine("CreateFolder NAME=\"" + arcName + "\" ARCHIVE=.rar READAUTO=no NOUPDATESETTINGS");
			}
		}
		else
		{
			work = true;
			cmd.AddLine("Copy FILE=\"" + f.RealPath + "\" TO=\"" + f.Path + "\" ARCHIVE=.rar CREATEFOLDER=\"" + f.name_stem_m + ".rar\"");
		}

		cmd.AddLine("Delete QUIET FILE=\"" + f.RealPath + "\"");
	}

	if (work)
	{
		cmd.Run();
	}
}

function IsEmptyDir(item)
{
	if (!item.is_dir)
	{
		return false;
	}

	var fe = DOpus.FSUtil.ReadDir(item.RealPath);

	return fe.complete;	
}

But, personally, I would archive things, then delete them separately.

1 Like

I'd let WinRAR do the job and take the responsibility for deleting, it's a built-in feature:

Available via command line as well.

1 Like

Thank you so much lxp. Is That Possible to create a button to run the WinRAR program with check these 2 option

  1. Put each file into a separate archive
  2. Delete Files after archiving