Anda menggunakan Windows XP, jadi Anda harus menginstal Host Scripting Windows secara default. Dengan itu, inilah script WSH JScript untuk melakukan apa yang Anda butuhkan. Cukup salin kode ke nama file xtract.bat atau sesuatu di sepanjang baris tersebut (bisa apa saja asalkan memiliki ekstensi .bat
), dan jalankan:
xtract.bat example.tar.gz
Secara default, skrip akan memeriksa folder skrip, serta PATH
variabel lingkungan sistem Anda untuk 7z.exe. Jika Anda ingin mengubah tampilannya, Anda dapat mengubah variabel SevenZipExe di bagian atas skrip menjadi apa pun yang Anda inginkan dengan nama yang dapat dieksekusi. (Misalnya, 7za.exe atau 7z-real.exe) Anda juga dapat mengatur direktori default untuk dieksekusi dengan mengubah SevenZipDir. Jadi jika 7z.exe
ada C:\Windows\system32\7z.exe
, Anda akan meletakkan:
var SevenZipDir = "C:\\Windows\\system32";
Bagaimanapun, ini skripnya:
@set @junk=1 /* vim:set ft=javascript:
@echo off
cscript //nologo //e:jscript "%~dpn0.bat" %*
goto :eof
*/
/* Settings */
var SevenZipDir = undefined;
var SevenZipExe = "7z.exe";
var ArchiveExts = ["zip", "tar", "gz", "bzip", "bz", "tgz", "z", "7z", "bz2", "rar"]
/* Multi-use instances */
var WSH = new ActiveXObject("WScript.Shell");
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var __file__ = WScript.ScriptFullName;
var __dir__ = FSO.GetParentFolderName(__file__);
var PWD = WSH.CurrentDirectory;
/* Prototypes */
(function(obj) {
obj.has = function object_has(key) {
return defined(this[key]);
};
return obj;
})(this.Object.prototype);
(function(str) {
str.trim = function str_trim() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
})(this.String.prototype);
(function(arr) {
arr.contains = function arr_contains(needle) {
for (var i in this) {
if (this[i] == needle) {
return true;
}
}
return false;
}
})(this.Array.prototype);
/* Utility functions */
function defined(obj)
{
return typeof(obj) != "undefined";
}
function emptyStr(obj)
{
return !(defined(obj) && String(obj).length);
}
/* WSH-specific Utility Functions */
function echo()
{
if(!arguments.length) return;
var msg = "";
for (var n = 0; n < arguments.length; n++) {
msg += arguments[n];
msg += " ";
}
if(!emptyStr(msg))
WScript.Echo(msg);
}
function fatal(msg)
{
echo("Fatal Error:", msg);
WScript.Quit(1);
}
function findExecutable()
{
// This function searches the directories in;
// the PATH array for the specified file name;
var dirTest = emptyStr(SevenZipDir) ? __dir__ : SevenZipDir;
var exec = SevenZipExe;
var strTestPath = FSO.BuildPath(dirTest, exec);
if (FSO.FileExists(strTestPath))
return FSO.GetAbsolutePathName(strTestPath);
var arrPath = String(
dirTest + ";" +
WSH.ExpandEnvironmentStrings("%PATH%")
).split(";");
for(var i in arrPath) {
// Skip empty directory values, caused by the PATH;
// variable being terminated with a semicolon;
if (arrPath[i] == "")
continue;
// Build a fully qualified path of the file to test for;
strTestPath = FSO.BuildPath(arrPath[i], exec);
// Check if (that file exists;
if (FSO.FileExists(strTestPath))
return FSO.GetAbsolutePathName(strTestPath);
}
return "";
}
function readall(oExec)
{
if (!oExec.StdOut.AtEndOfStream)
return oExec.StdOut.ReadAll();
if (!oExec.StdErr.AtEndOfStream)
return oExec.StdErr.ReadAll();
return -1;
}
function xtract(exec, archive)
{
var splitExt = /^(.+)\.(\w+)$/;
var strTmp = FSO.GetFileName(archive);
WSH.CurrentDirectory = FSO.GetParentFolderName(archive);
while(true) {
var pathParts = splitExt.exec(strTmp);
if(!pathParts) {
echo("No extension detected for", strTmp + ".", "Skipping..");
break;
}
var ext = pathParts[2].toLowerCase();
if(!ArchiveExts.contains(ext)) {
echo("Extension", ext, "not recognized. Skipping.");
break;
}
echo("Extracting", strTmp + "..");
var oExec = WSH.Exec('"' + exec + '" x -bd "' + strTmp + '"');
var allInput = "";
var tryCount = 0;
while (true)
{
var input = readall(oExec);
if (-1 == input) {
if (tryCount++ > 10 && oExec.Status == 1)
break;
WScript.Sleep(100);
} else {
allInput += input;
tryCount = 0;
}
}
if(oExec. ExitCode!= 0) {
echo("Non-zero return code detected.");
break;
}
WScript.Echo(allInput);
strTmp = pathParts[1];
if(!FSO.FileExists(strTmp))
break;
}
WSH.CurrentDirectory = PWD;
}
function printUsage()
{
echo("Usage:\r\n", __file__, "archive1 [archive2] ...");
WScript.Quit(0);
}
function main(args)
{
var exe = findExecutable();
if(emptyStr(exe))
fatal("Could not find 7zip executable.");
if(!args.length || args(0) == "-h" || args(0) == "--help" || args(0) == "/?")
printUsage();
for (var i = 0; i < args.length; i++) {
var archive = FSO.GetAbsolutePathName(args(i));
if(!FSO.FileExists(archive)) {
echo("File", archive, "does not exist. Skipping..");
continue;
}
xtract(exe, archive);
}
echo("\r\nDone.");
}
main(WScript.Arguments.Unnamed);