Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use batch processing to delete files before a specific time

Shulou Source: shulou.com Published: 2022-06-03 08:37:59 09月22日 Update

This article is about how to use batch processing to delete files before a specific time. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Using the for command to read the result of dir / tc / Omurd / a can be divided into four paragraphs, the first two paragraphs are the date and time, the third paragraph is the directory or file size, and the fourth paragraph is the file name or directory name. Then compare paragraphs 1 and 2 with the required time to decide whether to delete the file or not

@ echo off rem cd / d f:\ temp rem mDateTime format: yyymmddHHmm set mDateTime=200608161907 for / f "skip=4 tokens=1,2 3 proc set aDate=%1 set aTime=%2 set aType=%3 set aFile=%4 set aDatetime=%aDate:~0,4%%aDate:~5,2%%aDate:~8,2%%aTime:~0,2%%aTime:~3,2% if 4* delims= "% I in ('dir / a /-c / tc / OMID') do call: proc% I% j% k% l goto end: proc set aDate=%1 set aTime=%2 set aType=%3 set aFile=%4 set aDatetime=%aDate:~0,4%%aDate:~5,2%%aDate:~8,2%%aTime:~0,2%%aTime:~3,2% if / I"% aDateTime% "lss"% mDateTime% "(if NOT"% aType% "=" DIR "attrib"% aFile% " "- r-h-s & & del"% aFile% "): end

But there will be some problems above, mainly because if can't compare large numbers. 200608161907 is too big for if. We can compare the date first, and then the time, but there are more if statements, so let's compromise and use two-digit years:

Rem mDateTime format: ymmddHHmm

Set mDateTime=0608161907

……

Set aDatetime=%aDate:~2,2%%aDate:~5,2%%aDate:~8,2%%aTime:~0,2%%aTime:~3,2%

Windows2003 has a command Forfiles, such as:

Forfiles / D-30 / C "cmd / c echo @ fname is from 30 days ago. 2003 Forfiles.exe files can be copied and used under 2000.

In addition, it is easier to keep the last n files, for example, the last 30 bak files:

For / f "skip=30"% I in ('dir * .bak / tc / omurd / b') do del% I

In batch processing, change% I to% I

Thank you for reading! On "how to use batch processing to delete files before a specific time" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: File time content command date more format directory article good practical size year number file name article directory name see knowledge result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Apple vpn Shulou Information