0

I have a batch file

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "my_date=%YYYY%%MM%%DD%" & set "time=%HH%%Min%"

set "minFiles=10"

setlocal EnableDelayedExpansion

rar a -t -rr D:\Backup\Arch\Backup_%_my_date%.rar D:\Backup\Backup.bak

for /f %%A in ('dir "D:\Backup\Arch\*.rar" /a-d-s-h /b ^| find /v /c ""') do if %%A leq %minFiles% goto :eof
:loop
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "D:\Backup\Arch\*.rar"') do (
    del "%%a"
    goto :breakLoop
    )
:breakLoop
pause
for /f %%A in ('dir "D:\Backup\Arch\*.rar" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop

It is supposed to rar a file and then check if there is more than 10 files in folder delete oldest file. It works well until in folder D:\Backup\Arch\ are 10 rar files. But if in folder are 10 rar files and I start *.bat I get a new rar file in D:\Backup\Arch\ and now they are 11. It's supposed to go to next line delete one file if there are 11, but it doesn't delete. Why ? rar is taking 2 or 3 hours.

When program is here

rar a -t -rr D:\Backup\Arch\Backup_%_my_date%.rar D:\Backup\Backup.bak

and it's archiving file when does it go to next line

for /f %%A in ('dir "D:\Backup\Arch\*.rar" /a-d-s-h /b ^| find /v /c ""') do if %%A leq %minFiles% goto :eof

It goes when rar is finished or right after command rar is send ? How I can make program to go to the next line right after rar is finished ?

If I start a different bat with this part of code it works perfect and delete files

for /f %%A in ('dir "D:\Backup\Arch\*.rar" /a-d-s-h /b ^| find /v /c ""') do if %%A leq %minFiles% goto :eof
:loop
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "D:\Backup\Arch\*.rar"') do (
    del "%%a"
    goto :breakLoop
    )
:breakLoop
pause
for /f %%A in ('dir "D:\Backup\Arch\*.rar" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop
6
  • 1
    You want somebody to debug the batch file, can't you ask the guy that wrote it?
    – barlop
    Commented Jun 14, 2019 at 8:50
  • 2
    As your backup file names contain the date in a sortable format, why don't you simply sort by names backwards (dir /B /O-N) and use for /f "skip=%minfiles% delims=" ?
    – LotPings
    Commented Jun 14, 2019 at 8:56
  • @LotPings you think he wrote that? and btw changing a line of a program can potentially ruin it. What makes you think that making that change is not going to ruin the whole thing, he may have to change other lines to adjust for that. And I doubt that'd fix his problem anyway
    – barlop
    Commented Jun 14, 2019 at 9:22
  • you are lucky to have got an answer, try what lotpings wrote in his answer
    – barlop
    Commented Jun 14, 2019 at 9:48
  • @barlop - it doesn't matter who wrote it. It is a reasonable question
    – lx07
    Commented Jun 14, 2019 at 10:50

1 Answer 1

2

As per my comment, I suggest to:

  • use for /f with the skip option
  • and parse dir output sorted reverse by name dir /B /A-D /O-N
  • BTW you created a variable my_date but tried to use the nonexistent %_my_date%

:: Q:\Test\2019\06\14\SU_1448654.cmd
@Echo off & setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "my_date=%dt:~0,8%
set "minFiles=10"

rar a -t -rr D:\Backup\Arch\Backup_%my_date%.rar D:\Backup\Backup.bak

for /f "skip=%minFiles% delims=" %%a in ('dir /B /A-d /O-N "D:\Backup\Arch\Backup_*.rar"') do (
    Echo del "%%a"
)
pause

If the output looks OK, remove the Echo in front of the del command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .