A batch file that removes a file that was not last modified today

I just want to know:

How can I remove 'log.txt' if the last modification was not today?

With a batch file.

I'm just talking about 1 FILE here!

+1


a source to share


5 answers


You can use forfiles

:



forfiles /D -1 /M log.txt /C "cmd /c del @file"

      

+1


a source


PowerShell should replace the package.



if ((dir log.txt) .LastWriteTime -lt [datetime] :: today) {del log.txt}
+2


a source


Unfortunately, you cannot do this in a reliable and general way (*).

Powershell, Cygwin, Unix Tools are great solutions if you can be sure they will be installed on the target machine.

I wrote a small utility that takes a wildcard path with a number of days and deletes all files matching a path that is greater than the specified number of days. In my environment, this was more convenient than installing a third party package.

(*) The following will work for your specific case (modification date is not today) as long as the short date format in your locale includes a century (i.e. 10 characters). But it doesn't generalize to N days, and I don't like relying on the computer's regional settings for this kind of thing:

for %%i in (log.txt) do SET FILETIME=%%~ti
IF NOT "%FILETIME:~0,10%" == "%DATE%" DEL /f log.txt

      

+1


a source


The package does something very simple. If you can use Cygwin or install Unix Tools, consider this:

find log.txt -mtime +1 -exec rm {} \;

      

UPDATE
Found something that might be helpful for you using a clean batch. You should be able to modify and adapt it according to your needs.

0


a source


For which you can use jscript or vbscript. Both of these scripting languages ​​have been installed on MS platforms since the mid 90s

0


a source







All Articles