![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-15 12:59 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » How to use batch processing to delete files that are 7 days old |
| Printable Version 8,126 / 27 |
| Floor1 lilankui | Posted 2006-09-08 10:50 |
| 新手上路 Posts 1 Credits 6 From 江西 | |
|
### Step 1: Understanding the Overall Purpose
The overall goal of this batch script is to obtain the date that is 7 days prior to the current date and store it in the `nowdate` variable. It uses a combination of VBScript and batch processing to achieve this date calculation. ### Step 2: Breaking Down the `for /f` Command The `for /f` loop is used for parsing the output of a command. Here's the breakdown of the specific line `for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k`: - **`for /f`**: This is the for-loop command for parsing formatted text. - **`"tokens=1,2,3* delims=-"`**: - `delims=-` specifies that the hyphen (`-`) character is used as the delimiter to split the input string. - `tokens=1,2,3*` means that it will extract the first, second, third, and any remaining tokens (after the third) from the string that has been split by the delimiter. - **`('cscript /nologo %tmp%\tmp.vbs')`**: This is the command whose output will be parsed. It runs the VBScript located at `%tmp%\tmp.vbs` and suppresses the logo output of cscript using `/nologo`. - **`do set d=%%k`**: For each iteration of the loop, it takes the third token (since `tokens=1,2,3*` and we are using `%%k` which corresponds to the third token) from the split string and assigns it to the variable `d`. In summary, this line is extracting the day part of the date that was calculated as 7 days prior to the current date from the output of the VBScript and storing it in the `d` variable. |
|
| Floor2 9527 | Posted 2006-09-08 22:11 |
| 银牌会员 Posts 438 Credits 1,185 From 北京 | |
|
There are too many things to consider when operating dates, at least when using batch processing. Reply to the LZ, he combined VBS scripts and batch processing to complete it. The code hasn't been tested, and I'm going to write a batch processing about this. There are really too many things to consider, hehe................
|
|
| Floor3 linqlou | Posted 2006-11-25 13:34 |
| 初级用户 Posts 14 Credits 32 | |
| Floor4 redtek | Posted 2006-11-25 21:31 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Tested the principle code above:)
Test system version: |
|
| Floor5 不得不爱 | Posted 2006-11-25 22:28 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j其实可以改为: for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do (set y=%%i set m=%%j set m=%%j) |
|
| Floor6 vkill | Posted 2006-11-25 23:38 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
forfiles /p "d:\test" /s /m *.* /d -7 /c "cmd /c del @path"
The command in 03 is just for the last modified time. To get the created time, it's better to use for + dir. [ Last edited by vkill on 2006-11-25 at 11:53 PM ] Attachments forfiles.rar (20.73 KiB) |
|
| Floor7 redtek | Posted 2006-11-26 00:27 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Help vkill brother post the help of FORFILES, a very convenient tool~:)
Built-in in Windows Server 2003 system~:) [ Last edited by redtek on 2006-11-25 at 11:28 AM ] |
|
| Floor8 wenjia | Posted 2007-03-14 23:41 |
| 新手上路 Posts 1 Credits 2 | |
| Floor9 edgeos | Posted 2007-06-06 11:25 |
| 新手上路 Posts 1 Credits 2 | |
|
Just solved my problem, thank you!
|
|
| Floor10 qq43142691 | Posted 2007-06-06 16:57 |
| 中级用户 Posts 152 Credits 326 | |
|
:: DateDel2.bat - Delete files modified before a specified number of days in the specified path
:: Will Sort - 2005-9-2 - CMD@WinXP :: Notes: Parameter %1 specifies the number of days, where yesterday is 1, the day before yesterday is 2, and so on :: Parameter %1 specifies the file path, and if omitted, the current path will be processed :: Core algorithm: Ritchie Lawrence, updated 2002-08-13. Version 1.1 :: Source: http://www.cn-dos.net/forum/viewthread.php?tid=16676 @echo off & setlocal ENABLEEXTENSIONS call :Date2Day %date:~0,10% sdays set /a sdays-=%1 call :Day2Date %sdays% difdate echo The following files modified before %difdate% in %cd% will be deleted: for /r %2 %%f in (*.*) do if "%%~tf" LEQ "%difdate%" echo "%%f" goto :EOF :Date2Day setlocal ENABLEEXTENSIONS for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do ( set yy=%%a & set mm=%%b & set dd=%%c ) set /a dd=100%dd%%%100,mm=100%mm%%%100 set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2 set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633 endlocal&set %2=%j%&goto :EOF :Day2Date setlocal ENABLEEXTENSIONS set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5 set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10 (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%) endlocal&set %2=%yy%-%mm%-%dd%&goto :EOF |
|
| Floor11 ways1204 | Posted 2007-10-25 16:55 |
| 新手上路 Posts 1 Credits 2 | |
| Floor12 wangyang0215 | Posted 2007-12-20 14:23 |
| 新手上路 Posts 1 Credits 2 | |
|
Good post
|
|
| Floor13 egx2000 | Posted 2007-12-20 17:04 |
| 新手上路 Posts 1 Credits 2 | |
Originally posted by qq43142691 at 2007-6-6 04:57 PM: Does this script only work in pure DOS? Why does Windows 2003 prompt that there is no operand? |
|
| Floor14 qzwqzw | Posted 2007-12-20 19:59 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
It seems from the comments that there is no problem with 03. The problem usually occurs with command - line parameters. Carefully check whether the passed parameters are in accordance with the requirements
|
|
| Floor15 asasd | Posted 2007-12-29 13:30 |
| 新手上路 Posts 1 Credits 2 | |
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |