Mark
Mark

Reputation: 45

Zip All Files in Folder

I have been using a snippet of VBA for years to zip all files in a folder. Today we tried this on a co workers computer and the code appears to go through its iterations but there is not any files output. The only difference is the new machine is 32 bit and the old code is for 64 bit. Is there any reason the 32 bit file would not work with VBA?

Sub ZipIndividualFiles1()
    Dim file As Variant

   Const source = "C:\Users\co01\Desktop\TEST"
   Const DEST = "C:\Users\co01\Desktop\Zipped"
   Const PATH_TO_7Z = "C:\Program Files\7-Zip\7z.exe"

   For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(source).Files
      Shell PATH_TO_7Z & " a -tzip """ & DEST & "\" & file.Name &     ".zip"" """ & file.Path & """"
   Next
End Sub

7 zip exists at the PATH_TO_7Z path. We even tried re-installing it. The program runs to completion without error.

Upvotes: 1

Views: 2236

Answers (1)

Directionsky
Directionsky

Reputation: 106

Hi i found this code that will do the job, that you need to place it the folder you need to zip and just run it and you can save as batch file execute via VBA.

I learned this recently so i like to indicate the changes that can be made.

  1. If you need the script to zip all files as individual zip then you need to modify "%CurrDirName%.zip" to "%%a.zip"

  2. If you need the script to zip all contents in to one you can change "%%a.zip" to "%CurrDirName%.zip

  3. If you need to provide a name the simple you can hard coded the name there "Hardcoded.zip"

  4. If you need to zip only certain file types you can add them in set extension

  5. If you need to zip excluding certain file types you -x!*.bat, here .bat is what i am excluding

Hope it helps

@echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for %%I in (.) do set CurrDirName=%%~nxI
set extension=.*
for %%a in (*%extension%) do "%sevenzip%" a "%CurrDirName%.zip" "%%a" -x!*.bat
pause
[/CODE]

Upvotes: 2

Related Questions