Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Windows cmd.exe Command-Prompt ► ««« »»»

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! 

Contents:

1.) ... Example via the cmd and as a bat file!
2.) ... information on deleting with automation!
3.) ... Execute example command via the address bar!
4.) ... Example to delete the FRITZ *.* Export files except the youngest 7!




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!
Delete all files except the most recent via the command line!

-
▲ Back to the top ▲


4.) ... Example to delete the FRITZ *. * Export files except the youngest 7!



:: -- Remember the current directory set mypath=%cd%
set mypath=%cd%

set FRITZ="C:\Users\%username%\Programmdaten\AVM\"

set DATEN_FILTER="*FRITZ*.*export"


:: - 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: 24 August 2021 08:57 Windows
Windows-Console

Files that are younger than the date can be deleted via the command line!


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
Windows-Console

Ability to batch rename files in lower and upper case?


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
Windows-Console

Serial number of the hard disk under Windows 11, 10, ... via command prompt!


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
Windows-Console

Find out the PC serial number and manufacturer via command prompt?


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
Windows-Console

Lock the PC via the command prompt, Windows 11, 10, ...!


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,
Windows-Console

Share more fonts for Windows 11, 10, ... command prompt!


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
Windows-Console

List hidden files via the command prompt in Windows 11, 10, ...!


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:
  1. 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.
  2. 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?
  3. 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?
  4. 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




    

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + DesktopOK
  + ProcessKO
  + WinBin2Iso
  + ThisIsMyFile
  + TheAeroClock
  + CoronaKO
  + MatriX.CoronaKO
  + Find.Same.Images.OK
  + 3D.Benchmark.OK
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Windows 2021 | Windows 10 | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2023 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu
0.077


► Safely remove hardware as windows 11 desktop shortcut? ◄

► Windows 11 download folder and tips! ◄

► What are energy options? ◄

This website uses cookies to ensure you get the best experience on our website more Infos & Privacy Policy
....