2014-06-20, 20: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
Subscribe to our Windows Secrets Newsletter - It's Free!
Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!
+ Get this BONUS — free!
Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!
2014-06-23, 02:39 #2
- Join Date
- Dec 2009
- Thanked 205 Times in 195 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".