Difference between revisions of "7z All Files"

From TheAlmightyGuru
Jump to: navigation, search
 
Line 2: Line 2:
  
 
==Source==
 
==Source==
  // Copyright 2015, Dean Tersigni.
+
  // Copyright 2015-2020, Dean Tersigni.
  // This program zips every file and folder in the executed directory into its own individual zip file.
+
  // 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".
 
  // It requires that 7-Zip be installed at "C:\Program Files\7-Zip".
 
   
 
   
Line 9: Line 9:
 
  var oWSH = new ActiveXObject( "WScript.Shell" );
 
  var oWSH = new ActiveXObject( "WScript.Shell" );
 
   
 
   
  var sFolder = oWSH.CurrentDirectory; // Get the current folder.
+
  var sFolder = oWSH.CurrentDirectory; // Get the current folder.
  var oFolder = oFSO.GetFolder( sFolder ); // Create an object reference to 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 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 eFiles = new Enumerator( oFiles ); // Create an enumerator on the file object.
 
   
 
   
 
  var sFilePath;
 
  var sFilePath;
  for( ; !eFiles.atEnd(); eFiles.moveNext() ) { // Loop through the files.
+
  for( ; !eFiles.atEnd(); eFiles.moveNext() ) { // Loop through the files.
   oFile = eFiles.item(); // Get the path of the current file.
+
   oFile = eFiles.item(); // Get the path of the current file.
 
 
 
 
  var sFile = oFile.Name; // Get the file name.
+
  var sFile = oFile.Name; // Get the file name.
 
 
 
 
  if( sFile != "7Z All Files.js" ) { // Don't compress this file.
+
  if( sFile.toLowerCase() != "7z all files.js" ) { // Don't compress this file.
  var sFileStem = JustStem( sFile ); // Get the file name without its extension.
+
  if( GetExtension( sFile ).toLowerCase() != "7z" ) {
+
var sNewFile = sFileStem + ".7z";
+
var sFileStem = GetFileNameWithoutExtension( sFile ); // Get the file name without its extension.
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.
+
var sNewFile = sFileStem + ".7z";
//oFile.Delete( sFile );
+
 +
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.
 
  // Returns the name of a file without the extension.
  function JustStem( sFileName ) {
+
  function GetFileNameWithoutExtension( sFileName ) {
 
  var iRight = sFileName.lastIndexOf( "." );
 
  var iRight = sFileName.lastIndexOf( "." );
 
  var sStem = sFileName.substr( 0, iRight );
 
  var sStem = sFileName.substr( 0, iRight );
 
  return( sStem );
 
  return( sStem );
 +
}
 +
 +
// Returns the extension of a file path.
 +
function GetExtension( sFileName ) {
 +
var iLeft = sFileName.lastIndexOf( "." );
 +
var sExtension = sFileName.substr( iLeft + 1 );
 +
 +
return( sExtension );
 
  }
 
  }
  

Latest revision as of 20:39, 25 October 2020

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 );
}