B
B
Bohdan Zadorozhniy2020-12-07 16:37:29
cmd/bat
Bohdan Zadorozhniy, 2020-12-07 16:37:29

How to check by date +1 day when deleting old files?

There is a command to delete files older than 3 days
"FORFILES /p F:\backup\ /s /m *.* /d -3 /c "CMD /c del /Q @FILE""
I just don't understand the logic, so as a programmer, I’m no good, because I’m just an enikey, but how can I make the command delete files after checking that there are fresh files, and if there are no fresh files, then the script is not executed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2020-12-07
@Jektion

Bohdan Zadorozhniy you need to run a similar command to find out if there were fresh
FORFILES /p files. /s /m *.* /d !"today minus 3 days"! /c "cmd /c echo @FILE"
IF errorlevel 1 ECHO Skip Purge && GOTO :EOF
...
and to do this calculate the
date "today minus 3 days"
- this is traditionally done through some better language than cmd
e.g. jscsipt
example - if helped - mark as solution
check.cmd

@echo OFF
REM https://qna.habr.com/q/898855
SETLOCAL ENABLEDELAYEDEXPANSION
SET delta=%1
IF "%delta%"=="" SET delta=-3
FOR /f %%. IN ('cscript.exe /nologo add_days.js !delta!') DO SET CHECK_DATE=%%.
ECHO FORFILES /p . /s /m *.* /d !CHECK_DATE! /c "cmd /c echo @FILE"
FORFILES /p . /s /m *.* /d !CHECK_DATE! /c "cmd /c echo @FILE" 2>NUL 1>NUL
IF errorlevel 1 ECHO Skip Purge && GOTO :EOF
echo Call purge

GOTO :EOF

add_date.js
var days=parseInt(WScript.Arguments.Item(0));

Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }

var o = (new Date()).addDays(days);

WScript.Echo((1 + o.getMonth()) + '/' + o.getDate() + '/' + o.getFullYear());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question