Saved Bookmarks
| 1. |
Solve : batch file that deletes a file based on date in filename? |
|
Answer» Hi everyone, Hi everyone, the only other date data files have is "Date last accessed"...I'm not looking to use date modified, accessed or created. I would like to base this off of the date I have in the filename.Do you have ADMIN privileges which allow you to download/install a small utility program?no, i do not have admin privileges.Quote from: tmoe30 on January 20, 2010, 03:12:32 AM I'm not looking to use date modified, accessed or created. I would like to base this off of the date I have in the filename. The date created and the date in the file NAME might be the same? Can you POST the batch file that renamed the date file? That same batch file might be modified to delete the date file?Quote from: tmoe30 on January 20, 2010, 03:12:32 AM I'm not looking to use date modified, accessed or created. I would like to base this off of the date I have in the filename. Is the following batch file similar to the batch file that renamed the testfile.txt to testfile01202010.txt C:\batch>type datename.bat Code: [Select]@echo off for /f "tokens=2-4 delims=/- " %%a in ('date /t') do set date2=%%a%%b%%c echo date2 = %date2% echo hello > testfile.txt ren testfile.txt testfile%date2%.txt dir testfile%date2%.txt rem AT 14:30 NEXT:90 "del c:\batch\testfile01202010.txt" rem The above has not been tested rem * below rem testfile01152010.txt Output: C:\batch>datename.bat date2 = 01202010 Volume in drive C has no label. Volume Serial Number is F4A3-D6B3 Directory of C:\batch 01/20/2010 07:45 PM 8 testfile01202010.txt 1 File(s) 8 bytes 0 Dir(s) 303,377,936,384 bytes free C:\batch> _______________________________________ ______ http://www.instructables.com/answers/Is-there-a-batch-script-that-you-can-compile-and-/ * Let the cron, the AT command or the scheduler do the date math. A better way of doing what you want is to use the built-in scheduler. To get to this, go to Control Panel then Scheduled Tasks. You can run any program you like (including batch files) from there. You can also set up the scheduler from a batch file, which is closer to what you're after. It can run once, or every hour, week etc. You need to use the AT command, so your set-up batch would be something like : rem AT 14:30 NEXT:90 "del c:\batch\testfile01202010.txt" rem The above has not been tested by Bill Richardson Quote from: tmoe30 on January 20, 2010, 03:12:32 AM I'm not looking to use date modified, accessed or created. I would like to base this off of the date I have in the filename. A slight modification from above. How to save 90 days, wakeup and delete? C:\batch>type datename.bat Code: [Select]@echo off for /f "tokens=2-4 delims=/- " %%a in ('date /t') do set date2=%%a%%b%%c echo date2 = %date2% echo hello > testfile.txt ren testfile.txt testfile%date2%.txt dir testfile%date2%.txt dir /b testfile%date2%.txt dir /b testfile%date2%.txt >> save90.txt rem AT 14:30 NEXT:90 "del c:\batch\testfile01202010.txt" rem The above has not been tested by Bill Richardson rem testfile01152010.txt Output: C:\batch>datename.bat date2 = 01202010 Volume in drive C has no label. Volume Serial Number is F4A3-D6B3 Directory of C:\batch 01/20/2010 08:01 PM 8 testfile01202010.txt 1 File(s) 8 bytes 0 Dir(s) 303,377,649,664 bytes free testfile01202010.txt C:\batch>Quote from: tmoe30 on January 20, 2010, 06:06:22 PM no, i do not have admin privileges. Okay. Try the script below. There is no provision for paths/filenames containing spaces and the script is not fully tested. Please ensure you have a secure testing environment when you remove the rem from the If/Del command line. Good luck. Code: [Select]@echo off cls setlocal enabledelayedexpansion :: With acknowledgement to Dias de verano :: Compares the current date -90 days with the date in the filename :: of files named testfilemmddyyyy.txt and deletes the file if it :: is 90 or 90+ days older than current. set vb=%temp%\newdate.vbs ( echo Newdate = (Date(^)-90^) echo Yyyy = DatePart("YYYY", Newdate^) echo Mm = DatePart("M" , Newdate^) echo Dd = DatePart("D" , Newdate^) echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd )>>%vb% FOR /F "tokens=1-3" %%A in ('cscript //nologo %vb%') do ( set Yyyy=%%A set Mm=%%B set Dd=%%C ) set olddate=%Yyyy%%Mm%%Dd% echo Old date = %olddate% del %vb% for /f "delims=." %%1 in ('dir /b testfile*.txt') do ( set filename=%%1 set filedate=!filename:~-8! set filedate=!filedate:~-4!!filedate:~0,2!!filedate:~2,2! echo Filename date = !filedate! rem if !filedate! leq !olddate! del !filename!.txt ) |
|