I've wrote a batch file (using 7-Zip) that takes files on one folder and zipping each separately to another folder created in the batch file, then I deleted from the original folder the files.
I've tried to write it so it will zip into the same folder, but it deletes everything because of my last command that states to delete files from the same folder.
The original folder is called SmartLogger and the new one is just SmartLoggerZipped.
I need to zip the files on SmartLogger and leave them at the same folder but then to delete the original files in the folder.
@echo off
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
SET SrcDir=E:\Logs\SmartLogger
SET DestDir=E:\Logs\SmartLoggerZipped
IF NOT EXIST "%DestDir%" MD "%DestDir%"
ECHO.
ECHO Compressing files and folders in E:\Logs\SmartLogger
ECHO drive and moving to E:\Logs\SmartLoggerZipped and
ECHO then delete from E:\Logs\SmartLogger
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE
Let us assume the folder E:\Logs\SmartLogger contains the files:
The goal is to compress each file in this folder into a ZIP file with same file name ignoring all already existing *.zip files in that folder to be able to run this batch file once each day.
@echo off
for %%A in ("E:\Logs\SmartLogger\*") do (
if /I not "%%~xA" == ".zip" 7za.exe a -tzip -mx5 -y -- "%%~dpnA.zip" "%%~A" >nul && del /Q /F "%%~A"
)
The folder E:\Logs\SmartLogger contains after execution of above batch file:
Or the goal is to compress each file in this folder into a ZIP file with same file name and file extension ignoring all already existing *.zip files in that folder to be able to run this batch file once each day and append the file extension .zip to each compressed file.
@echo off
for %%A in ("E:\Logs\SmartLogger\*") do (
if /I not "%%~xA" == ".zip" 7za.exe a -tzip -mx5 -y -- "%%~A.zip" "%%~A" >nul && del /Q /F "%%~A"
)
The folder E:\Logs\SmartLogger contains after execution of above batch file:
The difference between the two batch files is "%%~dpnA.zip" versus "%%~A.zip".
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?if /?for /?7za --helpAnd see also the Microsoft article about Using command redirection operators for an explanation of >nul.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With