Thank you @jinsight and @Leo for the fast help.
I understand you are suggesting instead of using directly the command
Copy MOVE
I should use the command
Copy EXTRACT
and later
Copy ARCHIVE
to recompress
But in the uncompress world, should I use Copy MOVE for changing from the subdirectory to the main level or do I have to do differently.
By the way @jinsight I was very surprised how short was your code. What I get from ChatGPT was much longuer
function OnClick(clickData)
{
var source = clickData.func.sourcetab;
var cmd = clickData.func.command;
cmd.deselect = false; // Leave source files selected.
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var tempDir = "C:\\TempProcessing";
// Crear el directorio temporal
if (!objFSO.FolderExists(tempDir)) {
objFSO.CreateFolder(tempDir);
}
// Procesar cada archivo seleccionado
for (var e = new Enumerator(source.selected); !e.atEnd(); e.moveNext())
{
var f = e.item();
var tempArchivePath = tempDir + "\\" + objFSO.GetFileName(f.RealPath);
// Copiar el archivo al directorio temporal
if (objFSO.FileExists(f.RealPath)) {
objFSO.CopyFile(f.RealPath, tempArchivePath);
// Procesar el archivo temporalmente
ConvertArchive(tempArchivePath, f.RealPath, tempDir);
} else {
WScript.Echo("El archivo no existe: " + f.RealPath);
}
}
// Eliminar el directorio temporal después del procesamiento
if (objFSO.FolderExists(tempDir)) {
objFSO.DeleteFolder(tempDir, true);
}
}
function ConvertArchive(tempArchivePath, originalPath, tempDir)
{
var objShell = new ActiveXObject("Shell.Application");
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var extractFolder = tempDir + "\\" + objFSO.GetBaseName(tempArchivePath);
var archiveName = objFSO.GetBaseName(tempArchivePath);
var cbzPath = tempDir + "\\" + archiveName + ".cbz";
if (objFSO.GetExtensionName(tempArchivePath).toLowerCase() === "zip" || objFSO.GetExtensionName(tempArchivePath).toLowerCase() === "cbz") {
ExtractZip(tempArchivePath, extractFolder, objShell, objFSO);
} else if (objFSO.GetExtensionName(tempArchivePath).toLowerCase() === "rar" || objFSO.GetExtensionName(tempArchivePath).toLowerCase() === "cbr") {
ExtractRar(tempArchivePath, extractFolder, objShell, objFSO);
} else {
WScript.Echo("Tipo de archivo no soportado. Usa un archivo .zip, .rar, .cbz, o .cbr.");
return;
}
MoveFilesToTopLevel(extractFolder, objFSO);
RemoveEmptyDirs(extractFolder, objFSO);
CreateZip(extractFolder, cbzPath, objShell, objFSO);
WaitUntilFileIsReady(cbzPath, objFSO);
var originalSize = objFSO.GetFile(tempArchivePath).Size;
var resultSize = objFSO.GetFile(cbzPath).Size;
if (IsSizeAcceptable(originalSize, resultSize)) {
// Mover el archivo resultante de vuelta a la ubicación original
objFSO.MoveFile(cbzPath, originalPath);
} else {
WScript.Echo("Error: El archivo resultante parece incompleto. No se eliminará el archivo original.");
}
// Eliminar archivos temporales
if (objFSO.FileExists(tempArchivePath)) {
objFSO.DeleteFile(tempArchivePath);
}
if (objFSO.FolderExists(extractFolder)) {
objFSO.DeleteFolder(extractFolder, true);
}
}
// Funciones auxiliares
function ExtractZip(zipPath, destFolder, objShell, objFSO) {
if (!objFSO.FolderExists(destFolder)) {
objFSO.CreateFolder(destFolder);
}
var objZip = objShell.NameSpace(zipPath);
if (objZip) {
objShell.NameSpace(destFolder).CopyHere(objZip.Items());
} else {
WScript.Echo("No se pudo acceder al archivo ZIP: " + zipPath);
}
}
function ExtractRar(rarPath, destFolder, objShell, objFSO) {
var command = "unrar x -y \"" + rarPath + "\" \"" + destFolder + "\"";
objShell.ShellExecute("cmd.exe", "/c " + command, "", "open", 0);
}
function MoveFilesToTopLevel(folderPath, objFSO) {
if (objFSO.FolderExists(folderPath)) {
var subfolders = new Enumerator(objFSO.GetFolder(folderPath).SubFolders);
for (; !subfolders.atEnd(); subfolders.moveNext()) {
var subfolder = subfolders.item();
var files = new Enumerator(subfolder.Files);
for (; !files.atEnd(); files.moveNext()) {
var file = files.item();
file.Move(folderPath + "\\" + file.Name);
}
}
} else {
WScript.Echo("El directorio no existe: " + folderPath);
}
}
function RemoveEmptyDirs(folderPath, objFSO) {
if (objFSO.FolderExists(folderPath)) {
var subfolders = new Enumerator(objFSO.GetFolder(folderPath).SubFolders);
for (; !subfolders.atEnd(); subfolders.moveNext()) {
var subfolder = subfolders.item();
RemoveEmptyDirs(subfolder.Path, objFSO);
if (objFSO.GetFolder(subfolder.Path).Files.Count === 0 && objFSO.GetFolder(subfolder.Path).SubFolders.Count === 0) {
objFSO.DeleteFolder(subfolder.Path);
}
}
}
}
function CreateZip(sourceFolder, zipPath, objShell, objFSO) {
var objZipFile, objSourceFolder;
if (!objFSO.FileExists(zipPath)) {
objZipFile = objFSO.CreateTextFile(zipPath, true);
objZipFile.Write("PK" + String.fromCharCode(5) + String.fromCharCode(6) + new Array(19).join(String.fromCharCode(0)));
objZipFile.Close();
}
objZipFile = objShell.NameSpace(zipPath);
objSourceFolder = objShell.NameSpace(sourceFolder);
if (objZipFile && objSourceFolder) {
objZipFile.CopyHere(objSourceFolder.Items());
} else {
WScript.Echo("No se pudo crear el archivo ZIP: " + zipPath);
}
objZipFile = null;
objSourceFolder = null;
}
function WaitUntilFileIsReady(filePath, objFSO) {
var size1, size2;
size1 = objFSO.GetFile(filePath).Size;
do {
size2 = objFSO.GetFile(filePath).Size;
if (size1 === size2) {
var startTime = new Date().getTime();
while (new Date()..getTime() < startTime + 1000) {
}
break;
} else {
size1 = size2;
}
} while (true);
}
function IsSizeAcceptable(originalSize, resultSize) {
var sizeDifference = Math.abs(originalSize - resultSize);
var tolerance = originalSize * 0.1; // Tolerancia del 10%
return sizeDifference <= tolerance;
}
I suppose this is due to the fact that DOPUS functions are very well optimized. 