Compare Files & Delete

Discuss and share scripts and script files...
CookieMonster

Compare Files & Delete

Post by CookieMonster »

XY Compare files feature only compares if the files selected in a folder are different and what the difference is in size. I want to compare the exact files between to panes that are the same, highlight those files, then an option to tell me to chose which pane of the highlighted files I want to delete. If this function is built into XY I'd like to know, otherwise does a script exists ?

Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: Compare Files & Delete

Post by Filehero »

What's your definition of two files "being the same" except file size?

Or is "folder sync" what you're really after? Then you need a little patience, it's on the road map.


Cheers,
Filehero

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

This script should work...

It uses hash comparison to make sure that files are really the same.

Code: Select all

	$hash = "md5";

	// Don't edit anything below if you don't know what your doing
	$inactivePanePath = get("path", "i");
	$inactivePaneFiles = folderreport("filesrel", "r", $inactivePanePath, , , "|");
	$inactivePaneFilesCount = gettoken($inactivePaneFiles, "count", "|");
	$activePaneFiles = folderreport("filesrel", "r", , , , "|");
	$activePaneFilesCount = gettoken($activePaneFiles, "count", "|");

 	$longList = ($activePaneFilesCount >= $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;
 	$shortList = ($activePaneFilesCount < $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;

 	$existAndEqual = "";
	foreach ($file, $shortList, "|") {
		if (strpos($longList, $file) != -1) {
			if (filesequal("<curpath>\$file", "$inactivePanePath\$file", "$hash") == 1) {
				$existAndEqual = $existAndEqual . $file . "|";
			}
		}
	}

	if ($existAndEqual) {
		// A foreach loop would eliminate the flickering
		selectitems $existAndEqual;
		focus "PI";
		selectitems $existAndEqual;
		focus "PI";
	}
	
	$deleteOnPane = inputselect("Delete files on pane:", "Active|Inactive", , 2);
	if ($deleteOnPane == "Active") {
		delete 1, 0, ":list";
	} else {
		// Switching focus and using ":list" would work as well
		$deleteList = "";
		foreach ($item, $existAndEqual, "|") {
			if ($item) {
				$deleteList = $deleteList . "$inactivePanePath\$item" . "|";
			}
		}
		delete 1, 0, $deleteList;
	}
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

highend wrote:This script should work...

It uses hash comparison to make sure that files are really the same.

Code: Select all

	$hash = "md5";

	// Don't edit anything below if you don't know what your doing
	$inactivePanePath = get("path", "i");
	$inactivePaneFiles = folderreport("filesrel", "r", $inactivePanePath, , , "|");
	$inactivePaneFilesCount = gettoken($inactivePaneFiles, "count", "|");
	$activePaneFiles = folderreport("filesrel", "r", , , , "|");
	$activePaneFilesCount = gettoken($activePaneFiles, "count", "|");

 	$longList = ($activePaneFilesCount >= $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;
 	$shortList = ($activePaneFilesCount < $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;

 	$existAndEqual = "";
	foreach ($file, $shortList, "|") {
		if (strpos($longList, $file) != -1) {
			if (filesequal("<curpath>\$file", "$inactivePanePath\$file", "$hash") == 1) {
				$existAndEqual = $existAndEqual . $file . "|";
			}
		}
	}

	if ($existAndEqual) {
		// A foreach loop would eliminate the flickering
		selectitems $existAndEqual;
		focus "PI";
		selectitems $existAndEqual;
		focus "PI";
	}
	
	$deleteOnPane = inputselect("Delete files on pane:", "Active|Inactive", , 2);
	if ($deleteOnPane == "Active") {
		delete 1, 0, ":list";
	} else {
		// Switching focus and using ":list" would work as well
		$deleteList = "";
		foreach ($item, $existAndEqual, "|") {
			if ($item) {
				$deleteList = $deleteList . "$inactivePanePath\$item" . "|";
			}
		}
		delete 1, 0, $deleteList;
	}
Folder Sync may be the answer, depends on it's functionality.

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

The script works beautifully, except it doesn't tell me which files are are not identical between the panes and select them. If there could be a condition to allow me to select whether to select the files that are not identical or the files that are identical, then I can proceed with which pane to delete the files and whether to delete the files that are identical between the panes or not identical.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

If there could be a condition to allow me to select whether to select the files that are not identical or the files that are identical
But those "non identical" files are still files, that exist on both panes (but are different in their hash value)?
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

highend wrote:
If there could be a condition to allow me to select whether to select the files that are not identical or the files that are identical
But those "non identical" files are still files, that exist on both panes (but are different in their hash value)?
Both files may be identical in each pane, if one pane has (4) files which are not in the other pane, I'd like to have the option to select, for example the identical files, and delete the non-identical files, or vise-versa.

Flora_RMC
Posts: 295
Joined: 28 Dec 2012 09:50
Location: Windows 10 home 1909 - monitor 100% standard (1366x768)

Re: Compare Files & Delete

Post by Flora_RMC »

Tried this script and it's amazing. Congratulations, Highend! :appl: :appl:
It works like the SyncSelect command already in XY, except SyncSelect - the basic option - compares only the files' names (= identical names are selected), while this script compares the whole file (via hash comparison) and selects the identical ones.
In order to just have the identical files selected, you can try using the first part - cutting off the DeletePane's section, like this:

Code: Select all

   $hash = "md5";

   // Don't edit anything below if you don't know what your doing
   $inactivePanePath = get("path", "i");
   $inactivePaneFiles = folderreport("filesrel", "r", $inactivePanePath, , , "|");
   $inactivePaneFilesCount = gettoken($inactivePaneFiles, "count", "|");
   $activePaneFiles = folderreport("filesrel", "r", , , , "|");
   $activePaneFilesCount = gettoken($activePaneFiles, "count", "|");

    $longList = ($activePaneFilesCount >= $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;
    $shortList = ($activePaneFilesCount < $inactivePaneFilesCount) ? $activePaneFiles : $inactivePaneFiles;

    $existAndEqual = "";
   foreach ($file, $shortList, "|") {
      if (strpos($longList, $file) != -1) {
         if (filesequal("<curpath>\$file", "$inactivePanePath\$file", "$hash") == 1) {
            $existAndEqual = $existAndEqual . $file . "|";
         }
      }
   }

   if ($existAndEqual) {
      // A foreach loop would eliminate the flickering
      selectitems $existAndEqual;
      focus "PI";
      selectitems $existAndEqual;
      focus "PI";
   }
The selected files you get (in both panes) are the identical ones. You can reverse the selections with Ctrl+Shift+I.
Surely there is a better way to adjust this script to your needs, but I don't know how (I am really incapable when it comes to this matter... sorry; I just butchered Highend's script :( )

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

I've made the script more generic.
Now you can choose if you want to select all
1. identical or
2. not identical or
3. all missing files

and delete either of these selections in either
1. the active
2. the inactive pane

Ok?

Code: Select all

	$hash = "md5";

	// Don't edit anything below if you don't know what your doing
	$compareType = inputselect("Show identical, non identical or missing files:", "Identical|Not identical|Missing", , 2);

	$iPanePath = get("path", "i");
	$iPaneFiles = folderreport("filesrel", "r", $iPanePath, , , "|");
	$iPaneFilesCount = gettoken($iPaneFiles, "count", "|");
	$aPaneFiles = folderreport("filesrel", "r", , , , "|");
	$aPaneFilesCount = gettoken($aPaneFiles, "count", "|");

	$moreFiles = ($aPaneFilesCount >= $iPaneFilesCount) ? $aPaneFiles : $iPaneFiles;
	$lessFiles = ($aPaneFilesCount < $iPaneFilesCount) ? $aPaneFiles : $iPaneFiles;

	$cmpIsEqual = (($compareType == "Identical") || ($compareType == "Not identical")) ? "true" : "";
	$processList = (($compareType == "Identical") || ($compareType == "Not identical")) ? $lessFiles : $moreFiles;
	$haystackList = (($compareType == "Identical") || ($compareType == "Not identical")) ? $moreFiles : $lessFiles;

	$isEqual    = "";
	$isMissing  = "";
	$isNotEqual = "";
	foreach ($file, $processList, "|") {
		if (strpos($haystackList, $file) != -1) {
			if ($cmpIsEqual) {
				if (filesequal("<curpath>\$file", "$iPanePath\$file", "$hash") == 1) {
					$isEqual = $isEqual . $file . "|";
				} else {
					$isNotEqual = $isNotEqual . $file . "|";
				}
			}
		} else {
			$isMissing = $isMissing . $file . "|";
		}
	}

	$selItems = ($compareType == "Identical") ? $isEqual : (($compareType == "Not identical") ? $isNotEqual : $isMissing);

	focus "PI";
	selectitems $selItems;
	focus "PI";
	selectitems $selItems;

	$deleteOnPane = inputselect("Delete files on pane:", "Active|Inactive", , 2);
	if ($deleteOnPane == "Active") {
		delete 1, 0, ":list";
	} else {
		// Switching focus and using ":list" would work as well
		$deleteList = "";
		foreach ($item, $selItems, "|") {
			if ($item) {
				$deleteList = $deleteList . "$iPanePath\$item" . "|";
			}
		}
		delete 1, 0, $deleteList;
	}
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

The script asks me if I want to select the active, non-active, etc, then XY locks up ? I'll try again, I've tried twice with the same negative results.

Is the script finding identical or non-identical files between two different panes, or one pane ? If it's only one pane that is why it could be crashing. I'm expecting it find non-identical files between different drive / folders in two different panes.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

I'm expecting it find non-identical files between different drive / folders in two different panes.
And this is exactly what it does. Apart from that, it works flawlessly on my system.
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

highend wrote:
I'm expecting it find non-identical files between different drive / folders in two different panes.
And this is exactly what it does. Apart from that, it works flawlessly on my system.
One drive/partition in one pane, another drive/partition in another pane. I originally ran the script, which prompted me what action I want to take, XY crashes after I choose an action. I tested by selecting some files in one pane then run the script, I got a prompted for which action to take, XY crashes. Why isn't it working for me, why does it keep crashing, frustrating.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

With the same two folders opened on each try, does it crash on "identical, "non identical and "missing" (ofc only one of them must be selected)?

How many files are in each folder, how large are the biggest ones?

Simple way to find out what's happening:

Edit the script (add one line to it so it looks like:

Code: Select all

	foreach ($file, $processList, "|") {
		status "Processing file: $file", , "progress";
		if (strpos($haystackList, $file) != -1) {
If the above doesn't help, try:

Put a "step;" command before the foreach loop and try it again.

Does it crash always on the same file (if yes, which one)?

Code: Select all

	$isNotEqual = "";
	step;
	foreach ($file, $processList, "|") {
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: Compare Files & Delete

Post by CookieMonster »

I let the script run, and it literally took almost :30 minutes before the next prompt appeared, it very slow. It's as though the script is checking the entire HDD. The files in each folder are huge, a few gigs each.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Compare Files & Delete

Post by highend »

Not the script is slow, the computation of the md5 hashes is slow. Because XY does not support threading the entire application locks up while performing the hash calculations.

Just set the first variable to an empty string:

Code: Select all

$hash = "";
Nothing more can be done to make it faster (apart from not using bytewise comparison and instead relying on file times, size, etc.).
One of my scripts helped you out? Please donate via Paypal

Post Reply