For deleting old file in batch

Closed
vishnu kumar - Dec 3, 2015 at 10:20 AM
BrianGreen Posts 1005 Registration date Saturday January 17, 2015 Status Moderator Last seen September 30, 2021 - Dec 3, 2015 at 03:25 PM
Hello,


I Want a Batch file for deleting old files in a folder along with the parameters and it's explanation
Please help me ...
It's an urgent work Please do needful.


Related:

1 response

BrianGreen Posts 1005 Registration date Saturday January 17, 2015 Status Moderator Last seen September 30, 2021 149
Dec 3, 2015 at 03:25 PM
Hi vishnu Kumar

I have come up with the following code. It asks for a folder path to check and delete files from. Then it confirms you want to delete the oldest file. Finally it deletes the file.


@echo off
setlocal
CLS

set /p id=Enter path for file you want to delete:


if [%id%] == [] (
echo.
echo "you did not enter a path. Please restart this .bat file"
echo.
pause
exit
)

echo.
echo.

set Folder=%id%
set FileMask=*.*
set OldestFile=
for /f "delims=" %%a in ('dir /b /o:d "%Folder%\%FileMask%" 2^>NUL') do (
set OldestFile=%%a
)

if "%OldestFile%"=="" (
echo No files found in '%Folder%' matching '%FileMask%'!
) else (
echo "Are you sure you want to delete this file:
echo.
echo "%Folder%\%OldestFile%"
echo.
echo.
Pause
del "%Folder%\%OldestFile%"
)




Im sure you can add a loop to make it delete files up to a desired date if you want to.

I really appreciate thank you messages as a payment for solving issues   :o)
0