1

How do you all delete the newest file in a folder using batch script?

All results I have found ONLY show how to delete the oldest files or delete them after N days.

Links are : Batch file to delete files older than N days and Batch Script to delete oldest folder in a given folder

Thank you

Community
  • 1
  • 1
NewLearner
  • 149
  • 1
  • 3
  • 12

3 Answers3

2
for /F %%a in ('dir /B /O-D /A-D %the_directory%') do echo("%the_directory%\%%a"&goto deldone
:deldone

would be my approach. It rudely escapes from the for loop having deleted the first filename encountered in a sorted-in-reverse-date-order (/o-d) list.

This saves having to read the entire list. Not such a problem with a few files, but can be a pain with thousands.

The required DEL command is merely ECHOed for testing purposes. After you've verified that the command is correct, change ECHO(DEL to DEL to actually delete the files.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I thought of this method as well, but although the performance is for sure better, I decided not to use it here as it might cause trouble if inserted into a script in case it is placed within another parenthesised block of code... – aschipfl Nov 05 '16 at 16:17
  • @aschipfl: fair enough, but there are limits. With your example, you'd possibly get better performance with `if not defined file set "file=%%F"`. – Magoo Nov 05 '16 at 16:24
  • Ah, yes, querying the variable might be faster than multiplicately overwriting it, thanks! – aschipfl Nov 05 '16 at 22:35
1

I would do this:

@echo off
set latest=
set the_directory=your_directory

for /F %%a in ('dir /B /OD /A-D %the_directory%') do set latest=%%a

del %the_directory%\%latest%

run dir on files only, with sorting on modification date. Then loop and keep last echoed file. Delete it (replace by echo to test!)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

The following code snippet does what you want:

set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
    dir /B /A:-D /O:D /T:C "*.*"
') do (
    set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd

The dir command returns files only (/A:-D) sorted by date in ascending order (/O:D) using the creation date (/T:C), which are located in \path\to\folder and match the pattern *.*.
The for /F loop walks through all the files returned by dir and assigns its name to variable FILE, overwriting it in each iteration, hence the final value is the name of the newest file. The del command finally deletes the found file. The if query covers the case when no files are found in the given location.


Here is a slightly modified variant as recommended by Magoo's comment, which might be a bit more performant than the aforementioned one:

set "FILE="
pushd "\path\to\folder" || exit /B 1
for /F "eol=| delims=" %%F in ('
    dir /B /A:-D /O:-D /T:C "*.*"
') do (
    if not defined FILE set "FILE=%%F"
)
if defined FILE del "%FILE%"
popd

This avoids multiplicately overwriting of variable FILE by querying whether it has already been defined. Note the reversed sort order of dir here.

However, the approach shown in Magoo's answer is still better in performance.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99