Delete all files except the most recent via command line, script or CMD.EXE?
It is easy to delete all files except the most recent via command line, script or CMD.EXE (for Windows 11, 10, ... and MS Server)!
The most popular script to delete, for example, all files with the extension *.txt and to keep the latest 7 files, which can then be easily automated via autostart or the scheduler under Windows, or can be executed directly via the address bar of Windows Explorer!
1.) Example via the cmd and as a bat file!
For the command prompt:
for /f "skip=7 eol=: delims=" %F in ('dir /b /o-d /a-d *.txt') do @del "%F"
For the * .bat file:
for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d /a-d *.txt') do @del "%%F"
Brief explanation of what actually happens here!
With the section dir / b / od / ad * .txt
This lists the * .txt files. The /o-d stands for listing by date -d = youngest first, this list is saved or filed in %F!
More about you: https://www.colorconsole.de/console/en/021.htm
With for the list is processed with skip=7 the first 7 are left out and not deleted ;-) By del
More about for: https: //www.colorconsole.de/console/en/032.htm
2.) Information about deleting with automation!
This can then be done via the autostart folder through a link to the corresponding BAT file!
See: ►► Autostart of programs when starting Windows?
Or: ►► How can I start the CMD program at a certain point in time, with examples?
3.) Execute the example command via the address bar!
For the address bar you use cmd /k
at the beginning
( ... see Image-1 Point 1
cmd /k for /f "skip=7 eol=:delims="% F in ('dir /b /o-d /a-d *.txt') do @del "% F"
Here as an example in ► Qud-Explore Q-Dir also
works in ► MS Windows Explorer
| (Image-1) Delete all files except the very latest via the command line!
4.) ... Example to delete the FRITZ *. * Export files except the youngest 7!
:: -- Remember the current directory set mypath=%cd%
:: - Change to the directory , in which the data should be deleted
cd /d %FRITZ%
:: - delete all *FRITZ*. *export, sorting by date backwards and omitting the first 7
for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d /a-d %DATEN_FILTER%') do @del "%%F"
:: - Back to the old directory
cd /d %mypath%
FAQ 106: Updated on: 26 August 2023 10:52
It is quite easy to delete files that are younger than a certain date via the command line under Windows 11, 10, and MS Server OS Everyone probably
It is easy to rename files in batches using a script or command prompt in lower and / or upper case letters on Windows 11, 10, and MS Server OS Everyone
It is quite easy to find out the serial number of your hard disk under Windows 11, 10, or MS Server 2022, at a command prompt There are free hard
Using the command prompt, it is easy to find out the PC serial number and manufacturer under Windows 11, 10, and MS Server OS Everyone has ► read out
Locking Windows 11, 10, or MS Server 2022, 2019, is also possible via the command prompt It is not the fastest way to lock your Windows 11, 10,
It is easy to give more fonts for Windows 11, 10, command prompt Everyone has ► changed the color, font, etc. for the command prompt / CMD may
It is easy to list hidden files via the command prompt in Windows 11, 10, 1. Show hidden files with the command prompt 2. Show hidden files
»» My question is not there in the FAQ
|Asked questions on this answer:|
- That is different. If it is a script, I can certainly adapt it myself; if a compiled program, there should be an input option for the file type.
- I have several programs that save backup or log files to directories on a daily basis. I have such a mini program DelAge32.exe, which all files older than z. B. deletes 7 days. That works fine, except when I'm not at the computer for more than 7 days. Then the directory is empty when you start up for the first time. So I'm looking for a tool that will leave the last 7 files untouched and delete all older ones. Have I now described it clearly?
- There are some tools that delete files older than a certain date, also via the command line. I'm looking for something that will delete files except for the most recent ones. Probably not a lot of programming effort for professionals?
- You don't need any programming knowledge for this. So, data in a folder should be deleted that are younger than xxx days - hours?
Keywords: windows, 11, 10, console, delete, files, except, most, recent, command, line, script, easy, server, popular, Questions, Answers, Software