home

Dos batch file (call powershell) to zip all files in a folder
Author Nigel Rivett

For the following create a file with extension .bat, copy the code into the file and run it.

This takes a filename and extension
Removes the last 5 characters
Creates zip files from the result and compresses the applicable files into it
The source files will be deleted (per zip file) on successful compression

The last 5 characters is chosen for those files with a date in the filename before the extension.
To change this:
	alter the expression !s:~0,-5! to get the distinct filoenames for the zip
	In the subroutine subzip change the zipcmd variable path to select the correct files

Therefore if you have files
Test_20180101.txt 
Test_20180110.txt 
Test_20180111.txt 
Test_20180112.txt 
Test_20180123.txt 

Given the fillename test and extension txt
It will create zip files 
Test_2018010.zip
	Test_20180101.txt
Test_2018011.zip
	Test_20180110.txt 
	Test_20180111.txt 
	Test_20180112.txt 
Test_2018012.zip
	Test_20180123.txt


The result will be logged in the file ZipAllFiles_Log.txt
It is recommended that the 'bat file is placed in the source folder and run.
This will create the zip, log and tmp files in the source folder

log file output
	 
	Test_20180101.txt 
	Test_20180110.txt 
	Test_20180111.txt 
	Test_20180112.txt 
	Test_20180123.txt 
	powershell "compress-Archive -path C:\test\Powershell_zip\src2\Test_2018010*.txt -DestinationPath C:\test\Powershell_zip\src2\Test_2018010.zip" 
	powershell "compress-Archive -path C:\test\Powershell_zip\src2\Test_2018011*.txt -DestinationPath C:\test\Powershell_zip\src2\Test_2018011.zip" 
	powershell "compress-Archive -path C:\test\Powershell_zip\src2\Test_2018012*.txt -DestinationPath C:\test\Powershell_zip\src2\Test_2018012.zip" 






bat file code


set s_path=c:\test\Powershell_zip\src2
set d_path=c:\test\Powershell_zip\zip
set fname=test
set fext=txt

rem use this if the .bat file is in the source folder (recommended)
set s_path=%cd%
set d_path=%cd%




set tmp1=%d_path%\tmp1.txt
set tmp2=%d_path%\tmp2.txt
set LogFile=%d_path%\ZipAllFiles_Log.txt
set f=%fname%*.%fext%

if exist "%LogFile%" (
	set /p var=log file exists - aborting, press enter
	goto end
)

echo. > %tmp1%
echo. > %tmp2%
echo. > %LogFile%
SETLOCAL ENABLEDELAYEDEXPANSION

rem write all input filenames to tmp1.txt
for /f "delims=" %%a in ('dir /B %s_path%\%f%') do (
set s=%%a
echo !s:~0,-5! >> %tmp1%
echo !s! >> %LogFile%
)

rem write all output filenames to tmp2.txt
rem for /f "tokens=1 delims=," %%a in ('Sort %tmp1%') do if not defined prev set prev=""
set prev=""
for /f "tokens=1 delims=," %%a in ('Sort %tmp1%') do call :subwritetmp2 %%a 

rem zip files
for /f "tokens=1 delims=," %%a in ('Sort %tmp2%') do call :subzip %%a 

goto end


:subwritetmp2
if "%1" EQU "%prev%" goto :EOF
echo %1 >> %tmp2%
set prev=%1
goto :EOF

:subzip
set s=%1
echo !s!
if "!s!" EQU "" goto :EOF
if "!s:~0,4!" EQU "ECHO" goto :EOF
echo !s! >> %LogFile%
set zipcmd="compress-Archive -path !s_path!\!s!*.!fext! -DestinationPath !d_path!\!s!.zip"
echo powershell !zipcmd! >> %LogFile%
powershell !zipcmd!
if %ERRORLEVEL% neq 0 goto fail
del /Q !s_path!\!s!*.!fext!
goto :EOF

:fail
echo Failure
set /p var=press enter
exit 1
:end
set /p var=press enter


home