Results 1 to 2 of 2
2014-06-20, 21:33 #1
- Join Date
- Jun 2014
- Thanked 0 Times in 0 Posts
deleting a file in a folder that is auto created with dates so only last item is deleted
hello all i am looking for some help i have a program that auto creates a backup every 30 minutes but does not delete at interval and is filling up my hd i was wondering if anyone can help me i resurched a little and have tried to make a batch file in order to create a bat file this is what i haveCode:
@ECHO OFF for /f "usebackq delims== tokens= 2" %%i in ( `WMIC OS GET localdatetime /format:value` ) do set localTime=%%i set "YYYY=%localTime:~0,4%" set "M=%localTime:~4,1%" set "MM=%localTime:~5,1%" set "D=%localTime:~6,1%" set "DD=%localTime:~7,1%" if %M%==0 set M= if %D%==0 set D= cd /d "~%~dp0" FOR /F "tokens=*" %%X IN ('dir .\%YYYY%\%M%%MM%\%D%%DD%\Backup-serversave-%YYYY%-%M%%MM%-%D%%DD%--*.zip /b /o:-d') DO set FILENAME=%%~X rem FILENAME now contains the last element if "%FILENAME%" neq "" DEL "%FILENAME%" pause
2014-06-23, 03:39 #2
- Join Date
- Dec 2009
- Thanked 762 Times in 714 Posts
The line dir .\%YYYY%\%M%%MM%\%D%%DD%\Backup-serversave-%YYYY%-%M%%MM%-%D%%DD%--*.zip will look in the current directory for a folder named "2014\06\23" and a file "Backup-serversave-2014-06-23--*.zip".
In a batch file if you need to look in a specific directory you need to specify the full path somewhere, not use just a relative path. You can do this by changing to the base directory using a "CD" command or add the base directory to your path specification. Alternatively, specify a base directory and use "dir /s" to search all sub-directories. Using the sub-directory option you can remove all the date determination code.
See this post for some code samples.
To schedule programs you should use "Task Scheduler".