China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-28 03:18
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use batch processing to delete files that are 7 days old View 7,521 Replies 27
Original Poster Posted 2006-09-08 10:50 ·  中国 广东 东莞 电信
新手上路
Credits 6
Posts 1
Joined 2006-09-05 10:48
19-year member
UID 61890
Gender Male
From 江西
Status Offline
### 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.
Floor 2 Posted 2006-09-08 22:11 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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................
我今后在论坛的目标就是做个超级坏人!!!
Floor 3 Posted 2006-11-25 13:34 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 32
Posts 14
Joined 2006-11-22 12:05
19-year member
UID 71377
Gender Male
Status Offline
Floor 4 Posted 2006-11-25 21:31 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Tested the principle code above:)



  1. C:\TEMP\Q Get Date>type d.vbs
  2. wscript.echo dateadd("d",-6,date)
  3. wscript.echo date

  4. C:\TEMP\Q Get Date>cscript /nologo d.vbs
  5. 2006-11-19
  6. 2006-11-25

  7. C:\TEMP\Q Get Date>echo %date%
  8. 2006-11-25 星期六

  9. C:\TEMP\Q Get Date>date
  10. Current date: 2006-11-25 星期六
  11. Enter new date: (year month day)
2006-11-25 08:31


Test system version:

Name: Windows Server 2003, Enterprise Edition
Version: 5.2 Build 3790
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 5 Posted 2006-11-25 22:28 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
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)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +1 2006-11-26 00:27
Floor 6 Posted 2006-11-25 23:38 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +1 2006-11-26 00:28
Attachments
forfiles.rar (20.73 KiB, Credits to download 1 pts, Downloads: 123)
Floor 7 Posted 2006-11-26 00:27 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Help vkill brother post the help of FORFILES, a very convenient tool~:)
Built-in in Windows Server 2003 system~:)



FORFILES
{yyyy-MM-dd | dd}]

Description:
Select a file (or a group of files) and execute a command on that file. This is helpful for batch jobs.

Parameter list:
/P pathname Indicates the path to start searching. The default folder is the current working directory (.).

/M searchmask Search for files according to the search mask. The default search mask is '*'.

/S Instructs forfiles to recurse into subdirectories. Like "DIR /S".

/C command Indicates the command to be executed for each file. The command string should be enclosed in double quotes.

The default command is "cmd /c echo @file". The following variables
can be used in the command string:
@file - Returns the file name.
@fname - Returns the file name without the extension.
@ext - Returns only the file extension.
@path - Returns the full path of the file.
@relpath - Returns the relative path of the file.
@isdir - Returns "TRUE" if the file type is a directory;
Returns "FALSE" if it is a file.
@fsize - Returns the file size in bytes.
@fdate - Returns the date when the file was last modified.
@ftime - Returns the time when the file was last modified.

To include special characters in the command line, use the hexadecimal code in the form 0xHH
(for example, 0x09 is tab).
Internal CMD.exe commands should start with "cmd /c".

/D date Select files whose last modification date is greater than or equal to (+),
or less than or equal to (-) the date specified in "yyyy-MM-dd" format;
or select files whose last modification date is greater than or equal to (+)
the current date plus "dd" days, or less than or equal to (-) the current
date minus "dd" days. The valid "dd" days can be
any number in the range 0 - 32768. If not specified,
"+" is taken as the default symbol.

/? Display help message.

Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C "cmd /c type @file | more"
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C "cmd /c echo @path 0x09 was changed before 30. "
FORFILES /D 2001-01-01
/C "cmd /c echo @fname was new as of January 1, 2001. "
FORFILES /D +2006-11-25 /C "cmd /c echo @fname is new today. "
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"



[ Last edited by redtek on 2006-11-25 at 11:28 AM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 8 Posted 2007-03-14 23:41 ·  中国 新疆 克拉玛依 电信
新手上路
Credits 2
Posts 1
Joined 2007-03-14 01:55
19-year member
UID 81643
Gender Male
Status Offline
Floor 9 Posted 2007-06-06 11:25 ·  中国 北京 大兴区 联通
新手上路
Credits 2
Posts 1
Joined 2007-06-06 11:04
19-year member
UID 90413
Gender Male
Status Offline
Just solved my problem, thank you!
Floor 10 Posted 2007-06-06 16:57 ·  中国 广东 广州 黄埔区 电信
中级用户
★★
Credits 326
Posts 152
Joined 2007-05-04 06:16
19-year member
UID 87412
Gender Male
Status Offline
:: 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
Floor 11 Posted 2007-10-25 16:55 ·  中国 浙江 杭州 电信
新手上路
Credits 2
Posts 1
Joined 2007-10-25 14:14
18-year member
UID 100746
Gender Male
Status Offline
Floor 12 Posted 2007-12-20 14:23 ·  中国 江苏 苏州 昆山市 电信
新手上路
Credits 2
Posts 1
Joined 2007-12-20 13:48
18-year member
UID 106232
Gender Male
Status Offline
Good post
Floor 13 Posted 2007-12-20 17:04 ·  中国 广东 广州 海珠区 电信
新手上路
Credits 2
Posts 1
Joined 2007-12-20 13:20
18-year member
UID 106228
Gender Male
Status Offline
Originally posted by qq43142691 at 2007-6-6 04:57 PM:
:: 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, yesterday is 1, the day before yesterday is 2, and so on
:: ...

Does this script only work in pure DOS? Why does Windows 2003 prompt that there is no operand?
Floor 14 Posted 2007-12-20 19:59 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
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
Floor 15 Posted 2007-12-29 13:30 ·  中国 浙江 杭州 联通
新手上路
Credits 2
Posts 1
Joined 2007-12-29 11:44
18-year member
UID 107108
Gender Male
Status Offline
Forum Jump: