Abhijeet Todkari
Abhijeet Todkari

Reputation: 13

How to zip multiple folders using Batch script

i have below Batch file script.

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"

This creates zip files of all 10 folders but it creates subfolder in zip file with same name and i dont want that. direct files should be visible in zip file. is there any way to edit above script to solve this problem.

Upvotes: 1

Views: 13742

Answers (3)

Thiago Guimarães
Thiago Guimarães

Reputation: 380

Create a text file, and copy and paste the following:

START /W powershell Compress-Archive folder-name foldername.zip
START powershell Move-Item -Path "path-of-the-zipped-file" -Destination "path-you-want-the-file-to-be-moved-to"`

Save as zip.bat

Upvotes: 2

Compo
Compo

Reputation: 38708

If I understand your issue correctly, the easiest way to perform the task is to first step into each directory, (to make it 'current'), zip everything in the current directory, (default), then step back out again.

@For /D %%G In (*)Do @PushD "%%G"&&"%ProgramFiles%\7-Zip\7z.exe" a -tzip "..\%%G.zip" -r&PopD

Upvotes: 2

npocmaka
npocmaka

Reputation: 57282

check zipjs.bat. Try to put your folders in a list like in the example bellow (zipjs.bat should be in the same directory):

@echo off

set "folders_list=C:\folder1;C:\folder2;C:\folder3"
set "destination=C:\my.zip"

del "%destination%" /Q /F >nul 2>&1

for %%a in ("%folders_list%:;=";"%") do (
  if not exist "%destination%" (
    call zipjs.bat zipItem -source "%%~fa" -destination "%destination%" -force no
  ) else (
    call zipjs.bat addToZip -source  "%%~fa" -destination "%destination%" -force no
  )

)

EDIT. After some clarifications made by the OP:

@echo off

set "folders_list=C:\folder1;C:\folder2;C:\folder3"
set "destination=C:\my.zip"

del "%destination%" /Q /F >nul 2>&1

for %%a in ("%folders_list%:;=";"%") do (
  if not exist "%destination%" (
    call zipjs.bat zipDirItems -source "%%~fa" -destination "%destination%" -force no
  ) else (
    for %%# in ("%%a\*") do (
       call zipjs.bat addToZip -source  "%%~f#" -destination "%destination%" -force no
    )
  )

)

Upvotes: 1

Related Questions