|
lilankui
新手上路

积分 6
发帖 1
注册 2006-9-5 来自 江西
状态 离线
|
『楼 主』:
怎样用批处理删除7天前的文件
使用 LLM 解释/回答一下
@echo
Rem 取7天之前的日期,取回放入变量nowdate
echo wscript.echo dateadd("d",-6,date) >%tmp%\tmp.vbs
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 d=%%k
if %m% LSS 9 set m=0%m%
if %d% LSS 9 set d=0%d%
set nowdate=%y%-%m%-%d%
我想知道for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
这几句怎么解释,哪位高手可告诉我
### 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.
|
|
2006-9-8 10:50 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
对于日期的操作要考虑的太多了,至少用批处理是这样,回LZ 他是结合VBS脚本和批处理来完成的,代码没有测试,准备写一个关于这样的批处理,要考虑的实在是太多了,呵呵................
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................
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-9-8 22:11 |
|
|
linqlou
初级用户
 
积分 32
发帖 14
注册 2006-11-22
状态 离线
|
|
2006-11-25 13:34 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
测试了一下上面的原理代码:)
- C:\TEMP\Q 取日期>type d.vbs
- wscript.echo dateadd("d",-6,date)
- wscript.echo date
- C:\TEMP\Q 取日期>cscript /nologo d.vbs
- 2006-11-19
- 2006-11-25
- C:\TEMP\Q 取日期>echo %date%
- 2006-11-25 星期六
- C:\TEMP\Q 取日期>date
- 当前日期: 2006-11-25 星期六
- 输入新日期: (年月日)
2006-11-25 08:31
测试系统的版本:
名称: WindowsServer 2003, Enterprise Edition
版本: 5.2 内部版本 3790
Tested the principle code above:)
- C:\TEMP\Q Get Date>type d.vbs
- wscript.echo dateadd("d",-6,date)
- wscript.echo date
- C:\TEMP\Q Get Date>cscript /nologo d.vbs
- 2006-11-19
- 2006-11-25
- C:\TEMP\Q Get Date>echo %date%
- 2006-11-25 星期六
- C:\TEMP\Q Get Date>date
- Current date: 2006-11-25 星期六
- 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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-25 21:31 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
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)
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)
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-11-25 22:28 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
forfiles /p "d:\test" /s /m *.* /d -7 /c "cmd /c del @path"
03里面的命令,不过是最后修改的时间,要创建时间还是for + dir 好
Last edited by vkill on 2006-11-25 at 11:53 PM ]
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 ]
附件
1: forfiles.rar (2006-11-25 23:52, 20.73 KiB, 下载附件所需积分 1 点
,下载次数: 123)
|
|
2006-11-25 23:38 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
帮vkill兄贴上 FORFILES 的帮助,很方便的工具~:)
Windows Server 2003系统自带:)
FORFILES
{yyyy-MM-dd | dd}]
描述:
选择一个文件(或一组文件)并在那个文件上
执行一个命令。这有助于批处理作业。
参数列表:
/P pathname 表示开始搜索的路径。默认文件夹是当前工作的
目录 (.)。
/M searchmask 根据搜索掩码搜索文件。默认搜索掩码是 '*'。
/S 指导 forfiles 递归到子目录。像 "DIR /S"。
/C command 表示为每个文件执行的命令。命令字符串应该
用双引号括起来。
默认命令是 "cmd /c echo @file"。下列变量
可以用在命令字符串中:
@file - 返回文件名。
@fname - 返回不带扩展名的文件名。
@ext - 只返回文件的扩展。
@path - 返回文件的完整路径。
@relpath - 返回文件的相对路径。
@isdir - 如果文件类型是目录,返回 "TRUE";
如果是文件,返回 "FALSE"。
@fsize - 以字节为单位返回文件大小。
@fdate - 返回文件上一次修改的日期。
@ftime - 返回文件上一次修改的时间。
要在命令行包括特殊字符,字符请以 0xHH
形式使用十六进制代码(例如,0x09 为 tab)。
内部 CMD.exe 命令前面应以 "cmd /c" 开始。
/D date 选择文件,其上一次修改日期大于或等于 (+),
或者小于或等于 (-) 用 "yyyy-MM-dd" 格式指定的日期;
或选择文件,其上一次修改日期大于或等于 (+)
当前日期加 "dd" 天,或者小于或等于 (-) 当前
日期减 "dd" 天。有效的 "dd" 天数可以是
0 - 32768 范围内的任何数字。如果没有指定,
"+" 被当作默认符号。
/? 显示帮助消息。
例如:
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 在 30 前就被更改。"
FORFILES /D 2001-01-01
/C "cmd /c echo @fname 在 2001年1月1日就是新的。"
FORFILES /D +2006-11-25 /C "cmd /c echo @fname 今天是新的。"
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 ]
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,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-26 00:27 |
|
|
wenjia
新手上路

积分 2
发帖 1
注册 2007-3-14
状态 离线
|
|
2007-3-14 23:41 |
|
|
edgeos
新手上路

积分 2
发帖 1
注册 2007-6-6
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
正好解决我的问题,谢谢诶!
Just solved my problem, thank you!
|
|
2007-6-6 11:25 |
|
|
qq43142691
中级用户
  
积分 326
发帖 152
注册 2007-5-4
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
:: DateDel2.bat - 删除指定路径下指定日数以前修改的文件
:: Will Sort - 2005-9-2 - CMD@WinXP
:: 注意事项:参数%1指定日数,昨天为1,前天为2,依次类推
:: 参数%1指定文件路径,省略时将处理当前路径
:: 核心算法:Ritchie Lawrence, updated 2002-08-13. Version 1.1
:: 出处: 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 将删除 %cd% 下 %difdate% 前的修改的以下文件:
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
:: 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
|
|
2007-6-6 16:57 |
|
|
ways1204
新手上路

积分 2
发帖 1
注册 2007-10-25
状态 离线
|
|
2007-10-25 16:55 |
|
|
wangyang0215
新手上路

积分 2
发帖 1
注册 2007-12-20
状态 离线
|
|
2007-12-20 14:23 |
|
|
egx2000
新手上路

积分 2
发帖 1
注册 2007-12-20
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by qq43142691 at 2007-6-6 04:57 PM:
:: DateDel2.bat - 删除指定路径下指定日数以前修改的文件
:: Will Sort - 2005-9-2 - CMD@WinXP
:: 注意事项:参数%1指定日数,昨天为1,前天为2,依次类推
:: ...
这个脚本只能在纯DOS下用吗?win2003怎么提示没有操作数的?
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?
|
|
2007-12-20 17:04 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
从注释上看来03下没有问题
问题通常出在命令行参数上
仔细检查一下传递的参数是否按照要求
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
|
|
2007-12-20 19:59 |
|
|
asasd
新手上路

积分 2
发帖 1
注册 2007-12-29
状态 离线
|
|
2007-12-29 13:30 |
|
|