I'm trying to create a batch script that will zip all the contents in each subdirectory except the latest (or latest few). I'm currently attempting in Windows with 7-Zip but the directory is technically on a Linux server so any suggestions geared towards a Linux command is welcome.
The directory structure is like this:
Directory-Parent
---Sub-Directory-1
--------File1
--------File2
--------File3
---Sub-Directory-2
--------File1
--------File2
I would like to run a batch at the Directory-Parent
level that will create a zip in each sub-directory of all the files except the latest 1 (or few if possible). I also want to add the year to the end of the zip file name.
So the result would be:
Directory-Parent
-Sub-Directory-1
--------File1
--------Sub-Directory-12019.zip
---Sub-Directory-2
--------File1
--------Sub-Directory-22019.zip
I've tried a nested for
loop but can't seem to get it. I've tried the for
command with skip and dir
in the set (IN), but can't get it to work.
I currently have the following script.
SET theYear=2019
For /D %%d in (*.*) do 7z a "%%d\%%d%theYear%.zip" ".\%%d\*"
This accomplishes it all except I don't know how to exclude the latest file (newest file according to last modification time) in each folder.
This batch file can be used for this task:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FilesToIgnore=1"
set "theYear=%DATE:~-4%"
set "ListFile=%Temp%\%~n0.lst"
del "%ListFile%" 2>nul
for /D %%I in (*) do call :CompressFiles "%%I"
goto EndBatch
:CompressFiles
pushd %1
set "ZipFile=%~nx1%theYear%.zip"
for /F "skip=%FilesToIgnore% eol=| delims=" %%J in ('dir /A-D /B /O-D /TW 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"%ZipFile%"') do >>"%ListFile%" echo %%J
if exist "%ListFile%" (
echo Compressing files in directory %1 ...
7z.exe a -bd -bso0 -i"@%ListFile%" -mx9 -scsDOS -- "%ZipFile%"
del "%ListFile%"
)
popd
goto :EOF
:EndBatch
endlocal
The batch file sets environment variable theYear
dynamically from region dependent date string of dynamic environment variable DATE
. Please execute in a command prompt window echo %DATE:~-4%
and verify if output is the current year because of echo %DATE%
outputs current local date with last four characters being the year.
The batch file ignores the FilesToIgnore
newest files in each directory. Ignored is also the ZIP file if already existing from a previous execution of the batch file. The ZIP file is never included in number of FilesToIgnore
because of filtered out already by findstr
which filters output of command dir
which outputs the file names without path in current directory ordered by last modification time with newest files output first and oldest files output last.
Please read help of 7-Zip for the used switches and parameters.
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.
del /?
dir /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
popd /?
pushd /?
set /?
setlocal /?
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
and |
. The redirection operators >
and |
must be escaped with caret character ^
on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line with using a separate command process started in background.
Update: 7-Zip version 19.00.0.0 outputs a warning and creates an empty ZIP file in each subdirectory on using the command line as written below and used initially in batch file. I first thought this is a bug of 7-Zip version 19.00.0.0 because of this version should support also --
according to its help and 7-Zip version 16.04.0.0 works on using the command line:
7z.exe a -bd -bso0 -mx9 -scsDOS -- "%ZipFile%" "@%ListFile%"
It was necessary to remove --
from this command line to get it working with 7-Zip version 19.00.0.0.
So I reported this issue and the author of 7-Zip quickly replied explaining why the usage of --
results in searching for a file starting with @
in file name since 7-Zip version 19.00.0.0:
We need some way to add
@file
and-file
names from command line.
So--
stops@
and-
parsing, and 7-Zip searches exact names.
It's more safe to use-i@listfile
instead.
So I updated the batch file code and specify the list file with option -i
which is the most safe method to specify a list file.