I
I
Iron_Harvest2020-02-17 01:54:12
Backup
Iron_Harvest, 2020-02-17 01:54:12

How to create the correct batch file for archiving with conditions?

I need to create an archive using the first bat file and have only the last 5 archives left and the rest deleted. And with the second bat file, you need to unzip the last created archive into the specified directory.
The first bat archive was written, but the deletion of the old ones does not work, and accordingly it is not possible to make the same algorithm for unzipping the last archive. Also, in order to check the performance, you need to create a file in the DB_Updat.txt notepad and write down the full path to the file that you will archive there.

chcp 1251
SET Arc=5

rem Изменить на свой каталог

set OutDir=C:\Users\user\Desktop\mmm
if not exist "%OutDir%" md "%OutDir%"
set h=%time:~0,2%
set h=%h: =0%
set FullBackupDir=%OutDir%\%date%_%h%%time:~3,2%%time:~6,2%\
md %FullBackupDir%
set ArcName=%FullBackupDir%\BD_Update_%date%.zip
set [email protected]:\Users\user\Desktop\DB_Updat.txt
start /wait WinRAR a -y -m5 -ep -dh %ArcName% %DB_Update%

SET Index=0

rem Не удаляет старые архивы....

for /f "tokens=* delims=" %%D in ('dir %OutDir% /ad /b /o-d') do (
  if not %%D=="" (
    if not Arc==0 (
      set /a Arc-=1
    ) else (


And here is the second batch file to extract the last created archive, but it does not work properly, it only displays the name of the last created directory with the archive as "text".
chcp 1251
set InDir=C:\Users\user\Desktop\mmm
set OutDir=C:\Users\user\Desktop\mmm!!!
for /f "delims=" %%d in ('dir /b/s/a-d/od "%InDir%\*.*"') do set "$d=%%~nxd %%~td"
start /wait WinRAR e -y  %$d% %OutDir%

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Olgeir, 2020-02-18
@Iron_Harvest

1. How to delete all files except the last 5
for /f "skip=5" %%a in ('dir /ad /b /s /od ....') do del %%a
2. for /f %% a in ('dir /ad /b /s /od ....') do call asdf %%~nxd
:asdf
set p=%~1
start /wait WinRAR e -y %p% %OutDir%
goto :eof

A
Artem @Jump, 2020-02-17
Tag

Too lazy to delve into and try ....
Offhand -
RMDIR deletes directories. You cannot delete a directory if it contains files.
Therefore, if it does not delete, then the directory is not empty.
rd /s

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question