Vous utilisez Windows XP, donc vous devriez avoir Windows Scripting Host installé par défaut. Ceci étant dit, voici un JScript WSH script pour faire ce dont vous avez besoin. Il suffit de copier le code dans un nom de fichier xtract.bat ou quelque chose de ce genre (cela peut être n'importe quoi du moment que cela a l'extension .bat
), et exécutez :
xtract.bat example.tar.gz
Par défaut, le script vérifiera le dossier du script, ainsi que le dossier de votre système. PATH
variable d'environnement pour 7z.exe. Si vous voulez changer la façon dont il recherche des choses, vous pouvez changer la variable SevenZipExe en haut du script pour ce que vous voulez que le nom de l'exécutable soit. (Par exemple, 7za.exe ou 7z-real.exe) Vous pouvez également définir un répertoire par défaut pour l'exécutable en modifiant SevenZipDir. Ainsi, si 7z.exe
est à C:\Windows\system32\7z.exe
tu mettrais :
var SevenZipDir = "C:\\Windows\\system32";
Quoi qu'il en soit, voici le script :
@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);