help! how to compress file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xhai
Posts: 39
Joined: 13 Jun 2013 09:33

help! how to compress file

#1 Post by xhai » 27 Mar 2017 01:21

hello,

I want to compress a specific folder and togerther with a file..
let's say I'm gonna zip a file

ex.

in my desktop i have this structure

inside my compress folder I have this

Fruits
- Folder A
- a.txt
- b.txt
- Folder B
- a.txt
-b.txt

a.bin
b.bin
c.bin

I want to zip Fruits folder + b.bin into a one zip file ex.hellow.zip
then delete Fruits folder and b.bin

I think I get what I want but the problem I can't delete the Fruit folder and it's sub folder

@echo off
:compress
set path=Fruits
set path2=b.bin
set z=compressed.zip
7z.exe a -tzip compressed.zip "%path%"
7z.exe a -tzip compressed. zip "%path2%"

if exist "%path%" (
for /d %%p in ("%path%"\*.*") do rmdir "%%p" /s /Q
del /s /q %path%\*.*



thank you


aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help! how to compress file

#3 Post by aGerman » 27 Mar 2017 15:22

Never overwride the predefined path variable if you don't know for sure what you are doing!

npocmaka_'s link reminds me of a script that I wrote a couple of years ago. Adapted to your example.

Code: Select all

@if (@a)==(@b) @end /* Batch part:
@echo off &setlocal
set "p1=fruits"
set "p2=b.bin"
cscript //nologo //e:jscript "%~f0" "ex.hellow.zip" "%p1%" "%p2%"
rd /s /q "%p1%"
del /f "%p2%"
exit /b

JScript part: */
if (WScript.Arguments.Count() < 2) {WScript.Quit();}
var objFSO      = new ActiveXObject('Scripting.FileSystemObject'),
    objWShell   = new ActiveXObject('WScript.Shell'),
    objShellApp = new ActiveXObject('Shell.Application');
objWShell.CurrentDirectory = objFSO.GetParentFolderName(WScript.ScriptFullName);
var strZipName = objFSO.GetAbsolutePathName(WScript.Arguments(0));
if (objFSO.GetExtensionName(strZipName).toLowerCase() != "zip") {strZipName += '.zip';}
var strZipPath = objFSO.GetParentFolderName(strZipName);
if (!objFSO.FolderExists(strZipPath)) {objFSO.CreateFolder(strZipPath);}
if (!objFSO.FileExists(strZipName)) {
  var objF = objFSO.OpenTextFile(strZipName, 2, true, 0);
  objF.Write('PK\5\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
  objF.Close();
}
var strTemp = objWShell.ExpandEnvironmentStrings('%temp%'),
    objTemp = objShellApp.NameSpace(strTemp),
    objZip  = objShellApp.NameSpace(strZipName);
for (var n = 1; n < WScript.Arguments.Count(); ++n) {
  var F = objFSO.GetAbsolutePathName(WScript.Arguments(n));
  if (objFSO.FileExists(F) || objFSO.FolderExists(F)) {AddToZip(F);}
}

function AddToZip(strFName) {
  var strName = objFSO.GetFileName(strFName),
      EraseF = objZip.ParseName(strName),
      i = 0;
  if (EraseF != null) {
    if (objFSO.FileExists(strTemp + '\\' + strName)) {objFSO.DeleteFile(strTemp + '\\' + strName, true);}
    if (objFSO.FolderExists(strTemp + '\\' + strName)) {objFSO.DeleteFolder(strTemp + '\\' + strName, true);}
    i = objZip.Items().Count;
    objTemp.MoveHere(EraseF.Path, 1548);
    while (objZip.Items().Count == i) {WScript.Sleep(100);}
    if (objFSO.FileExists(strTemp + '\\' + strName)) {objFSO.DeleteFile(strTemp + '\\' + strName, true);}
    if (objFSO.FolderExists(strTemp + '\\' + strName)) {objFSO.DeleteFolder(strTemp + '\\' + strName, true);}
  }
  i = objZip.Items().Count;
  objZip.CopyHere(strFName, 1548);
  while (objZip.Items().Count == i) {WScript.Sleep(100);}
}

Steffen

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: help! how to compress file

#4 Post by xhai » 27 Mar 2017 20:30

got problem b.bin doesn't add to ex.hellow.zip

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help! how to compress file

#5 Post by aGerman » 28 Mar 2017 11:01

Sorry but I can't reproduce your problem.

Steffen

before:
before.PNG
before.PNG (15.12 KiB) Viewed 4599 times


after:
after_1.PNG
after_1.PNG (14.11 KiB) Viewed 4599 times
after_2.PNG
after_2.PNG (12.16 KiB) Viewed 4599 times

Post Reply