Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip and delete on the same file using 7-Zip

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
like image 331
Sophie Kobzantsev Avatar asked Oct 29 '22 21:10

Sophie Kobzantsev


1 Answers

Let us assume the folder E:\Logs\SmartLogger contains the files:

  • 7za920.zip
  • 7-zip.chm
  • license.txt
  • readme.txt

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:

  • 7za920.zip ... not touched
  • 7-zip.zip ... contains 7-zip.chm
  • license.zip ... contains license.txt
  • readme.zip ... contains readme.txt

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:

  • 7za920.zip ... not touched
  • 7-zip.chm.zip ... contains 7-zip.chm
  • license.txt.zip ... contains license.txt
  • readme.txt.zip ... contains readme.txt

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 --help

And see also the Microsoft article about Using command redirection operators for an explanation of >nul.

like image 114
Mofi Avatar answered Nov 10 '22 21:11

Mofi