cmd - Delete the oldest file from the files in the folder and subfolders -
setlocal set folder=c:\test\ set filemask=*.* set oldestfile= /f "delims=" %%a in ('dir /b /o:d "%folder%\%filemask%" 2^>nul') ( set oldestfile=%%a goto break ) :break if "%oldestfile%"=="" ( echo no files found in '%folder%' matching '%filemask%'! ) else ( echo del "%folder%\%oldestfile%" ) pause
here delete oldest file (the file in folder or directory in folder). how delete oldest file files in folder , subfolders? necessary subfolders checked.
@echo off setlocal enableextensions disabledelayedexpansion set "folder=c:\test\" set "filemask=*.*" set "oldestfile=" %%f in ("%folder%\.") /f "tokens=2,*" %%a in (' robocopy "%%~ff" "%%~ff" %filemask% /njh /njs /nc /ns /l /is /ndl /ts /s ^| 2^>nul sort ^| find ":" ') set "oldestfile=%%b" & goto :done :done echo "%oldestfile%"
this uses robocopy
list /l
files time stamp in yyyy/mm/dd hh:nn:ss
format, sort list oldest file first , once information of first file has been retrieved, show it.
Comments
Post a Comment