7z All Files

From TheAlmightyGuru
Jump to: navigation, search

7z All Files is a JScript program that uses 7-Zip to compress every individual file in the folder in which the script is executed into a 7z compressed file using optimal compression settings. You must have 7-Zip installed on your computer. If the path is different than the source, simply change the path in the code.

Source

// Copyright 2015-2020, Dean Tersigni.
// This program zips every file and folder (that isn't already a 7z file) in the executed directory into its own individual zip file.
// It requires that 7-Zip be installed at "C:\Program Files\7-Zip".

var oFSO = new ActiveXObject( "Scripting.FileSystemObject" );
var oWSH = new ActiveXObject( "WScript.Shell" );

var sFolder = oWSH.CurrentDirectory;						// Get the current folder.
var oFolder = oFSO.GetFolder( sFolder );					// Create an object reference to the current folder.
var oFiles = oFolder.files;							// Get a file object based on all the files.

var eFiles = new Enumerator( oFiles );						// Create an enumerator on the file object.

var sFilePath;
for( ; !eFiles.atEnd(); eFiles.moveNext() ) {					// Loop through the files.
 	oFile = eFiles.item();							// Get the path of the current file.
	
	var sFile = oFile.Name;							// Get the file name.
	
	if( sFile.toLowerCase() != "7z all files.js" ) {			// Don't compress this file.
		if( GetExtension( sFile ).toLowerCase() != "7z" ) {

			var sFileStem = GetFileNameWithoutExtension( sFile );	// Get the file name without its extension.
			
			var sNewFile = sFileStem + ".7z";
			
			oWSH.run('"C:\\Program Files\\7-Zip\\7z.exe" a "' + sNewFile + '" "' + sFile + '" -t7z -mx9 -ms=30f10m -mhc=on -r', 0, true);
			// Delete the old file. Uncomment the following line to have the program clean up after itself,
			// otherwise, you'll have to do it yourself.
			//oFile.Delete( sFile );
		}
	}
}

// Returns the name of a file without the extension.
function GetFileNameWithoutExtension( sFileName ) {
	var iRight = sFileName.lastIndexOf( "." );
	var sStem = sFileName.substr( 0, iRight );
	return( sStem );
}

// Returns the extension of a file path.
function GetExtension( sFileName ) {
	var iLeft = sFileName.lastIndexOf( "." );
	var sExtension = sFileName.substr( iLeft + 1 );

	return( sExtension );
}