-2

Could you please help me with a command for batch file to delete the files with Name of the file.

I need to delete the files which are older than 6 months according to their File name. I need to schedule the script in Task scheduler, So it will delete the files in specific location every day which are older than 6 months from current date according to their file name.

Location of files:

K:\Bck

File Names:

Backup_2020-10-22.txt
Backup_2020-10-21.txt
Backup_2019-09-16.txt
Backup_2018-05-17.txt
Backup_2017-04-16.txt
  • 3
    This topic has been posted many times here on SO. I suggest you do a search? – Gerhard Oct 23 '20 at 09:52
  • Thanks for your suggestion Gerhard, i have searched many sites but could not find the correct solution. The commands which i found were deleting the files which are older than 6 months according to their Modified/Created date and not with File Name. My requirement is different and need to delete according to the File name which is older than 6 months. – sudharsan raj Oct 23 '20 at 10:14
  • which to reiterate what @Gerhard has already said, [there are a multitude of questions on this site](https://stackoverflow.com/search?q=%5Bbatch-file%5D+delete+file+name+older) with answers explaining how to achieve what your duplicate question asks. – T3RR0R Oct 23 '20 at 10:27
  • Does this answer your question? [Batch file to delete files older than N days](https://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – T3RR0R Oct 23 '20 at 10:27
  • Does this answer your question? [Batch file logic to delete files based on date in filename](https://stackoverflow.com/questions/16613625/batch-file-logic-to-delete-files-based-on-date-in-filename) – Squashman Oct 23 '20 at 14:46

1 Answers1

1

Here is a modified version of Aacini's post, from back in 2013. I only amended it to use powershell to get today's date, instead of using the locale specific %date% variable and some changes to accomodate your environment. So I take no credit for this whatsoever.

@echo off
setlocal EnableDelayedExpansion
pushd "K:\Bck"
for /f "tokens=*" %%i in ('PowerShell -Command "& {Get-Date -format 'yyyyMMdd'}"') do set todayDate=%%i
call :DateToJDN %todayDate% todayJDN=

for /F "tokens=1-4* delims=_-" %%a in ('dir /B /A-D "Backup_*-*-*.txt"') do (
   call :DateToJDN %%b%%c%%~nd fileJDN=
   set /A daysOld=todayJDN - fileJDN
   if !daysOld! gtr 180 (
      echo File "%%a_%%b-%%c-%%d" is !daysOld! days old
      del "%%a_%%b-%%c-%%d" /Q
   )
)
popd
goto :eof

:DateToJDN yyyymmdd jdn=
set yyyymmdd=%1
set /A yyyy=%yyyymmdd:~0,4%, mm=1%yyyymmdd:~4,2% %% 100, dd=1%yyyymmdd:~6% %% 100
set /A a=(mm-14)/12, %2=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
exit /B

This will NOT yet delete anything. It will simply echo the commands to the screen. If you are sure the echo'd results resemble what you want, you can remove the echo from the line echo del "%%a_%%b_%%c_%%d" /Q

You also need to set your days accordingly in the line if !daysOld! gtr 180 (

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Thank you for the reply, I am getting result as below, i removed echo from del command line, but that file is not deleting. Do you have any suggestion. Log: File "Backup_2019-10-21.txt__" is 708 days old – sudharsan raj Oct 27 '20 at 12:34
  • I am not getting any error message, but it is not deleting the files which are older than 180 days. Also i am getting Delimiters underscore "txt__" at the end of file name, Why i am getting like that. "Backup_2019-10-21.txt__"? – sudharsan raj Oct 27 '20 at 13:47
  • Yes, Sorry i have checked the format and updated now. It was wrong before. I'll test your latest update and get back to you. Thanks for your kind help. Much appreciated. – sudharsan raj Oct 27 '20 at 14:00
  • Thank you very much Gerhard, The script is working. Thanks for your help. – sudharsan raj Oct 27 '20 at 14:22
  • Remember to change your script accordingly when your file layouts change. These scripts are specific to your formats. – Gerhard Oct 27 '20 at 14:23
  • Thanks for your help, may i know how to close this Query? this is resolved by you. – sudharsan raj Oct 27 '20 at 14:26