With Batch script, how do I delete the newest file from a folder, that is no older then 15 seconds from script Runtime

63 Views Asked by At

Many users use OBS video recording software. less of those users, use the recording software to record speed-runs. Failed speed runs are numerous for speed runners. Like me, they usually just end recording and immediately start a new recording so not to break stride.

Unfortunately. OBS recording software has a personal policy about offering any way to cancel or delete a current recording, vie hotkey or otherwise. They are against it, and would rather users sort through explorer view and pick out what files they want to delete, with no automation involved.

For speed runners, this will either interrupt their practice and stride in perfecting a path, or, as in my case... result in HOURS worth of footage to sort through re-watching failure after failure for sometimes, thousands of files to find the one file that was a success of the day.

To this effect, i have been studying other pages on stackOverflow for snips and pieces to put together to make a program to look through the folder my recordings are in. find -only- the most recent file in the folder, (so to skip looping through allllll the files in that folder) and check if the file found is less then 15 SECONDS young. and if so. Delete it.

I do not want it to delete the most recently modified file in the folder. if that file has not been accessed/modified/appended for more then 15 seconds.

15 seconds is plenty of time to decide that my current recording is bad and should be deleted from the SSD so its space can potentially be used for the next recording. This will also help prevent accidental deletion of Older recordings because a CAT is standing on the keyboard or some CATastrophe similar.

on one post i read, the following would sort from the most recently modified file then break as soon as it found the first file, thus being a very fast solution.

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

Batch File delete newest file

This solution however does not check if the modified file was within 15 seconds before the CURRENT system time.

I've seen others speak of how difficult dealing with region date and time formatting is with batch... And having worked with it a little first hand i will say its grueling. Id prefer a simple unix time conversion or the like. But im not sure what rout to go for efficiency. There seems to be a number of ways to go, all of which get complex fast.

forfiles
find
foreach

i read elsewhere that

date +%s 

will get the full time seconds since epoch, i assume that is based on local time region.

Could some one help me put together a script that quickly gets the last modified file in a folder, and checks if it is within 15 seconds, then deletes it or sends it to recycle bin or just a different folder. or even Echos it? I know seasoned programmers here tend to feel awkward about posting code that deletes something completely so moving to a different folder would be fine.

I have tried some Autohotkey code to do the same. But that is not a universal solution compared to Batch, and im sure it is a bit slower.

here is all i have so far for making this work. It is still missing a check for 15 seconds or less ago...

@echo off
set vidPath=
REM Notes for above;  Remember the trailing\backslash\  Quotes not required above.
if "%vidPath%" == ""
    goto errVidPath
if NOT exist "%vidPath%"
    goto err404
for /F %%a in ('dir /B /O-D /A-D %vidPath%') do echo(del "%vidPath%\%%a"&goto deldone
:deldone
echo Completed Search for the most recent file.  However it may have been a file older then 15 seconds. oops? Sorry!
pause
goto eof
:err404
echo VidPath= "%vidPath%"  Err: No directory found at this location. Did you remember a trailing backslash\ ?
pause
goto eof
:errVidPath
echo Err: VidPath= was blank, you must alter line 2 "set vidPath=" to the path you are saving OBS Videos to in order to check for files modified in the last 15 seconds to delete.
pause
goto eof
:eof
exit

As mentioned, It is still missing a check for 15 seconds or less ago... So it will conceivably delete the last file, modified in the folder, regardless of how old it is. This is BAD. Help?

0

There are 0 best solutions below