XYplorer: Quick Tips with 7-Zip

XYplorer is one of my favourite Windows apps and it has been a little while since I’ve shared anything about how I’m using it. So, when I added a new script this week that uses 7-Zip I thought I’d draft a quick post about it along with a related script also using 7-Zip.

XYplorer & 7-Zip

For those of you not familiar with them, XYplorer is an alternative to Windows Explorer and is a file management application extraordinaire. As well as being incredibly flexible and configurable, it comes with its own scripting language, which allows you to take it even further; though the good news is that the scripts in this post are actually quite straight forward and you can always just copy and paste them.

7-Zip is a compression utility that is open source and provides excellent performance and compression/decompression features, including, but not limited to, its own very nice ‘7z’ file format.

Pre-Script

Hopefully you know how to add scripts to your XYplorer instance, but if not, the first thing you should do is take the scripting tour, and it will have you up and running in no time.

I use script library files broken out into particular function areas, and then I access them from menus on toolbar buttons, so the following three scripts I keep in the same file.

… but I only mentioned two scripts earlier. Well that’s because this first script is barely a script. It’s a function that is used by both of the other scripts.

function get_7zpath()
{
	return 'C:\Program Files\7-Zip\7z.exe';
}

This function just returns the path to the 7z.exe file. A command line utility that’s part of the 7-Zip installation.

You may ask why I use a function rather than a variable. That is because I have a habit of sometimes dynamically changing paths based on some other criteria to utilise different versions - e.g. on different machines. For that reason, I got into the habit of using functions for common paths.

Script 1 : Zip Contents of Folders

Now you might cry out that you can already Zip a folder, and you don’t need 7-Zip to do that. Correct, but 7-Zip has a really strong command line when it comes to options, and this particular folder zipping doesn’t Zip the folder, but rather the folder’s contents.

When you right click and select the option to Zip a folder, it takes the folder and puts it into the Zip file. This script instead ignore the folder, takes the content of the folder and puts that in the Zip file. This removes that annoying root level folder that you otherwise have to navigate through when opening the Zip file.

This script will also run against multiple folders. It will Zip each into a separate folder rather than all into one. Just select your folders, select this script to run, and the Zip files will appear alongside the folders, named after each folder.

"7Zip a Folder"
	//Zips just the content, not the folder itself
	foreach($token, <get selecteditemspathnames |>)
	{
		status "7zip Generation - $token", "000000", "progress";

		//Archive items inside the selected folder
		runret('"' . get_7zpath() . '" a "' . $token . '.7z" "' . $token . '\*"');
	}
	status "7zip Archive Generation Complete", "000000", "ready";

Script 2 : Convert Zip Files to 7-Zip Files

In every case I’ve ever tried, 7-Zip’s own compression format is at least as good as the Zip format in terms of the amount it will compress the data. In fact, in the vast majority of cases it is an improvement.

Converting a Zip file to a 7-Zip file is at least a two-stage process. You can probably guess what it is. That’s right. Extract the files from the Zip file. Copy those files into a 7z file. Then if you’re happy, you can add the third step of removing the interim files that were extracted from the Zip file and once you are happy the 7z archive is okay, the Zip file too.

This second script automates some of this process. It will extract, compress and tidy the extracted files, and it will do this for each Zip file selected. This means I can easily select a set of Zip files and have them re-compressed as 7-Zip files automatically. I could also delete the Zip files, but I like to manually check any key files before I do that, so I leave it as a manual step; though now I think about it, maybe I’ll add a copy of this script but with an extra step to delete the selected Zip files at the end. Just for those occasions where it isn’t critical that I check all of the re-compressions worked perfectly.

"Convert ZIP to 7Z Format"
	//Zips just the content, not the folder itself
	foreach($token, <get selecteditemspathnames |>)
	{
		status "zip to 7zip Conversion - $token", "000000", "progress";

		//Get the zip file path without the ".zip"
		set $pathNoExt, gettoken("$token", -5, "", , 1);
		
		//Extract the files from the zip to a folder
		runret('"' . get_7zpath() . '" x -y -o"' . $pathNoExt . '" "' . $token . '"');

		//Compress the content of the folder to a 7z
		runret('"' . get_7zpath() . '" a -y -r -t7z "' . $pathNoExt . '" "' . $pathNoExt . '\*"');
		
		//Delete the folder
		delete 1, 0, "$pathNoExt"; 
	}
	status "zip to 7zip Conversion Complete", "000000", "ready";

I think these are handy little scripts for making the dull task of data archiving that much less irritable. I hope you find them useful too.

Author: Stephen Millard
Tags: | xyplorer |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read