Enjoy:
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"
See forfiles
documentation for more details.
For more goodies, refer to An A-Z Index of the Windows XP command line.
If you don't have forfiles
installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at %WinDir%\system32\
. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.
Later versions of Windows and Windows Server have it installed by default.
For Windows 7 and newer (including Windows 10):
The syntax has changed a little. Therefore the updated command is:
forfiles /p "C:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path"
See Windows Batch File (.bat) to get current date in MMDDYYYY format:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%
If you prefer the time in 24 hour/military format, you can replace the second FOR line with this:
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
C:> .\date.bat
2008-10-14_0642
If you want the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:
@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]
C:>test.cmd
Local date is [2012-06-19 10:23:47.048]
Best Answer
Stay out of C: and C:\Windows. Never put it on a file share (what if it isn't available)? I'd just use a subdirectory of C:\Program Files (to be more precise, of %ProgramFiles% - the user's system may not be installed on C:, for instance).