|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
『楼 主』:
求问“关于定期清理文件夹的bat”(等待中第2天了)
使用 LLM 解释/回答一下
想做一个计划任务,定期1个月清理d:\视频录像的资料。
但不知道如何写这样的BAT文件,请好心人帮助。
Last edited by sj2009 on 2009-12-16 at 16:56 ]
Want to make a scheduled task to regularly clean up the materials in d:\Video Recordings once a month. But don't know how to write such a BAT file. Please help from kind - hearted people.
Last edited by sj2009 on 2009 - 12 - 16 at 16:56 ]
|
|
2009-12-15 14:46 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
你要怎么清理呢?
如果是要每个月的这一天删除d根目录下(不含子目录)所有mpg文件,则可以:
for /f "tokens=3 delims=-:/ " %%i in ("%date%") do schtasks /create /tn "Hanyeguxing" /tr "cmd /c del d:\*.mpg /f /q /a" /sc monthly /d %%i
Last edited by Hanyeguxing on 2009-12-15 at 15:25 ]
How would you clean up?
If you want to delete all mpg files under the D root directory (excluding subdirectories) on this day of each month, you can:
for /f "tokens=3 delims=-:/ " %%i in ("%date%") do schtasks /create /tn "Hanyeguxing" /tr "cmd /c del d:\*.mpg /f /q /a" /sc monthly /d %%i
Last edited by Hanyeguxing on 2009-12-15 at 15:25 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2009-12-15 15:12 |
|
|
hanxiaofeng888
新手上路

积分 5
发帖 4
注册 2009-11-19
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
如果只做BAT让它每个月清理文件,那这个BAT必须一直开着
你可以这样。把下面的代码保存为BAT,把它添加到计划任务里,让它每个月启动一回就可以了 这样是不是很简短 呵呵
del /q d:\视频录像的资料\*.*
上面的只能删除d:\视频录像的资料\下的所有文件,不能删除文件夹和子目录下的文件
del /q /s d:\视频录像的资料\*.*
删除d:\视频录像的资料\下的所有文件和子目录下的所有文件,不能删除文件夹
If you only make a BAT to clean files every month, then this BAT must be kept on all the time.
You can do it like this. Save the following code as a BAT, add it to the task scheduler, and let it start once a month. Is this very concise? Hehe.
del /q d:\Video Recording Materials\*.*
The above can only delete all files under d:\Video Recording Materials\, and cannot delete folders and files in subdirectories.
del /q /s d:\Video Recording Materials\*.*
Delete all files under d:\Video Recording Materials\ and all files in subdirectories, but cannot delete folders.
|
|
2009-12-15 22:14 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
回2楼达人,我表达的不够准确,不好意思啦。
其实我想写的BAT文件,是为做一个计划任务,
定期1个月清理d:\视频录像的资料\*.*的视频文件,
就好比现在是2009.12月,我要清理前面10月和11月的视频文件,麻烦你再写下
Reply to the expert in floor 2, I didn't express it accurately enough, sorry.
Actually, the BAT file I want to write is to make a scheduled task,
Regularly clean up the video files *.* in d:\Video Recordings\ every month,
For example, now it's December 2009, I need to clean up the video files of October and November before, please write it again
|
|
2009-12-16 09:05 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
|
2009-12-16 09:06 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
|
2009-12-16 12:56 |
|
|
qinchun36
高级用户
    据说是李先生
积分 609
发帖 400
注册 2008-4-23
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
请先右键看一下你那些文件的属性
你要清理的依据是 “创建时间” 还是 “修改时间”
如果是后者的话可以帮你搞一下,前者如果用bat的话必须调用WMIC,太麻烦了我写不来。。。
Please first right-click and check the properties of your files.
Are you going to clean up based on "Creation Time" or "Modification Time"
If it's the latter, I can help with that. If it's the former, using a bat would need to call WMIC, which is too cumbersome for me...
|

┏━━━━━━┓
┃据说是李先生┃
┠──────┨
┃*ntRSS┃
┗━━━━━━┛ |
|
2009-12-16 16:02 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
回LS,清理的依据是 “创建时间”
Returning to LS, the basis for cleaning is "creation time"
|
|
2009-12-16 16:04 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
请教如何清理以日期命名的文件夹
其实我想写的BAT文件,是为做一个计划任务,
定期2个月清理d:\视频录像的资料\20091201\目录下的视频文件,
20091201是以日期命名的文件夹,
就好比现在是2009.12月,我要清理前面10月和11月的视频文件
Ask how to clean up folders named by dates
Actually, what I want to write is a BAT file to make a scheduled task,
regularly clean up the video files in the directory d:\Video Recordings\20091201\ every 2 months,
20091201 is a folder named by date,
just like now it's December 2009, I need to clean up the video files from October and November before that
|
|
2009-12-16 16:05 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
|
2009-12-16 16:14 |
|
|
qinchun36
高级用户
    据说是李先生
积分 609
发帖 400
注册 2008-4-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
靠,累死我了,给你写了一个,先别忙着运行,给你说明下先。
1、不要重复发帖,这种行为是很被鄙视的。
2、每次只能搞一个文件夹,因此每次要设置,改这个代码,通配符会吧,像
set files=d:\视频录像的资料\20091201\*.*
这样的,或者改成 *.rmvb
3、记住这个处理的是『创建日期』,请仔细理解。
4、先确认你机器安装了WMIC,具体做法是在命令行窗口中执行 wmic
安装之后才能运行这个批处理。
@echo off
:: 设置要处理几个月之内的(不包括这个月)
:: 小于 12 ,不然我没测试是否出错
set /a n=2
:: 设置要处理的文件的通配符表示(只能每次处理一个文件夹的)
set files=d:\视频录像的资料\20091201\*.*
color 17
title (C)2009 qinchun36 『据说是李先生』
setlocal enabledelayedexpansion
:init
set /a tty=%date:~0,4%
set /a ttm=%date:~5,2%
if %ttm% leq %n% (
set /a tsy=!tty!-1
set /a tsm=12+!ttm!-!n!
if !tsm! lss 10 set tsm=0!tsm!
set /a tsym=!tsy!!tsm!
) else (
set /a tsym=%date:~0,4%%date:~5,2%-!n!
)
set /a ts=!tsym!00
set /a te=%date:~0,4%%date:~5,2%00
echo.
echo 将要删除符合 『%files%』 的,
echo.
echo 『创建日期』在 %ts% 和 %te% 之间的文件!
echo.
set /p ching=是否继续?^(Y/N^)
if /i "!ching!"=="Y" goto process
exit
:process
echo.
echo =====================================================
for %%i in (%files%) do (
set f=%%~si
set fw=!f:\=\\!
for /f "skip=1 delims=." %%a in ('wmic datafile where name^='!fw!' get CreationDate') do set a=%%a
set /a ta=!a:~0,8!
if !ta! lss !te! (
if !ta! gtr !ts! (
echo 删除 !ta! !f!
del !f! /f /q
)
)
)
echo =====================================================
echo.
echo 处理完毕。
pause>nul
exit
Oh my god, I'm exhausted. I wrote one for you. Don't rush to run it, let me explain first.
1. Don't post repeatedly. This behavior is very despised.
2. Only one folder can be handled each time, so you need to set it each time. Modify this code. Do you know wildcards? Like
set files=d:\Video Recording Materials\20091201\*.*
or change it to *.rmvb
3. Remember that this processes the 'creation date', please understand it carefully.
4. First confirm that your machine has WMIC installed. The specific method is to execute wmic in the command line window. Only after installation can this batch file be run.
@echo off
:: Set how many months ago to handle (excluding this month)
:: Less than 12, otherwise I haven't tested if it goes wrong
set /a n=2
:: Set the wildcard representation of the files to handle (only one folder can be handled each time)
set files=d:\Video Recording Materials\20091201\*.*
color 17
title (C)2009 qinchun36 『It is said to be Mr. Li』
setlocal enabledelayedexpansion
:init
set /a tty=%date:~0,4%
set /a ttm=%date:~5,2%
if %ttm% leq %n% (
set /a tsy=!tty!-1
set /a tsm=12+!ttm!-!n!
if !tsm! lss 10 set tsm=0!tsm!
set /a tsym=!tsy!!tsm!
) else (
set /a tsym=%date:~0,4%%date:~5,2%-!n!
)
set /a ts=!tsym!00
set /a te=%date:~0,4%%date:~5,2%00
echo.
echo The files that match 『%files%』 and
echo.
echo whose 『creation date』 is between %ts% and %te% will be deleted!
echo.
set /p ching=Continue?^(Y/N^)
if /i "!ching!"=="Y" goto process
exit
:process
echo.
echo =====================================================
for %%i in (%files%) do (
set f=%%~si
set fw=!f:\=\\!
for /f "skip=1 delims=." %%a in ('wmic datafile where name^='!fw!' get CreationDate') do set a=%%a
set /a ta=!a:~0,8!
if !ta! lss !te! (
if !ta! gtr !ts! (
echo Delete !ta! !f!
del !f! /f /q
)
)
)
echo =====================================================
echo.
echo Processing completed.
pause>nul
exit
|

┏━━━━━━┓
┃据说是李先生┃
┠──────┨
┃*ntRSS┃
┗━━━━━━┛ |
|
2009-12-16 17:11 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
首先,类似于多长时间运行一次批处理等,在任务计划中设置.
在任务计划中运行这个批处理:
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "路径" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j%%k%%l%%m" gtr "时间1" if "%%i%%j%%k%%l%%m" lss "时间2" @del "路径\%%n%%o" /a /f /q>nul 2>nul
其中:
1,路径是指要操作的目录,例如 d:\视频录像的资料
2,时间是比较时间,例如以 2009年10月12日09时13分为分隔点,则写成 200910120913,其中:时间1是时间段的开始,时间2是时间段的结束,本批处理将删除这个时间段之间 创建的所有文件。
3,本批处理每次仅能处理 一个目录(文件夹),且不处理其 子目录(文件夹)。
4,本批处理依赖dir的输出格式,所以运行前请测试dir命令输出,是否符合本批处理要求。在cmd 中运行 dir "路径" /a:-d /t:c
看输出是否下面示例样子。
驱动器 C 中的卷没有标签。
卷的序列号是 303A-5C0B
C:\Documents and Settings\寒夜孤星 的目录
2009-10-12 09:13 2,883,584 NTUSER.DAT
2009-10-12 09:13 24,576 ntuser.dat.LOG
2009-10-12 09:13 178 ntuser.ini
2009-10-12 21:48 976 ntuser.pol
4 个文件 2,909,314 字节
0 个目录 12,642,504,704 可用字节
如果只删除某个时间点之前的文件,则可以这样写:
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "路径" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j%%k%%l%%m" lss "时间" @del "路径\%%n%%o" /a /f /q>nul 2>nul
Last edited by Hanyeguxing on 2009-12-16 at 19:54 ]
First of all, similar to how often to run the batch processing, it is set in the task schedule.
Run this batch processing in the task schedule:
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "path" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j%%k%%l%%m" gtr "time1" if "%%i%%j%%k%%l%%m" lss "time2" @del "path\%%n%%o" /a /f /q>nul 2>nul
Among them:
1. Path refers to the directory to be operated, for example d:\video recording materials
2. Time is the comparison time. For example, taking October 12, 2009 09:13 as the dividing point, it is written as 200910120913, where: time1 is the start of the time period, time2 is the end of the time period, and this batch processing will delete all files created between this time period.
3. This batch processing can only process one directory (folder) at a time and does not process its subdirectories (folders).
4. This batch processing depends on the output format of dir, so please test the dir command output before running to see if it meets the requirements of this batch processing. Run dir "path" /a:-d /t:c in cmd
See if the output is like the following example.
Volume in drive C has no label.
Volume Serial Number is 303A-5C0B
Directory of C:\Documents and Settings\Hanye Guxing
2009-10-12 09:13 2,883,584 NTUSER.DAT
2009-10-12 09:13 24,576 ntuser.dat.LOG
2009-10-12 09:13 178 ntuser.ini
2009-10-12 21:48 976 ntuser.pol
4 File(s) 2,909,314 bytes
0 Dir(s) 12,642,504,704 bytes free
If you only want to delete files before a certain time point, you can write it like this:
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "path" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j%%k%%l%%m" lss "time" @del "path\%%n%%o" /a /f /q>nul 2>nul
Last edited by Hanyeguxing on 2009-12-16 at 19:54 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2009-12-16 19:14 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
如果要删除前几个月的文件,则可以这样写:
@echo off&setlocal enabledelayedexpansion
set n=5
for /f "tokens=1,2 delims=-:/ " %%a in ("%date%") do (set a1=%%a&set a2=%%a&set b=%%b
if "%%b" lss "10" set b=!b:~1,1!)
set/a b1=%b%-%n%&set/a b2=%b%-1
if %b2% leq 0 set/a a2=%a2%-1&set/a b2=12
if %b2% leq 9 set b2=0%b2%
if %b1% leq 0 set/a a1=%a1%-1&set/a b1=12+%b1%
if %b1% leq 9 set b1=0%b1%
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "路径" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j" geq "%b1%" if "%%i%%j" leq "%b2%" @del "路径\%%n%%o" /a /f /q>nul 2>nul
其中set n=设置月份的长度,例如,当前为12月,要删除前5个月份的文件,则设置set n=5,即检查的月份为07月,08月,09月,10月,11月。
注意,n必须为1到12之间的数字。
Last edited by Hanyeguxing on 2009-12-16 at 20:59 ]
If you want to delete files from the previous few months, you can write it like this:
@echo off&setlocal enabledelayedexpansion
set n=5
for /f "tokens=1,2 delims=-:/ " %%a in ("%date%") do (set a1=%%a&set a2=%%a&set b=%%b
if "%%b" lss "10" set b=!b:~1,1!)
set/a b1=%b%-%n%&set/a b2=%b%-1
if %b2% leq 0 set/a a2=%a2%-1&set/a b2=12
if %b2% leq 9 set b2=0%b2%
if %b1% leq 0 set/a a1=%a1%-1&set/a b1=12+%b1%
if %b1% leq 9 set b1=0%b1%
for /f "skip=5 tokens=1,2,3,4,5,7* delims=-:/ " %%i in ('dir "路径" /a:-d /t:c') do if not "%%m" == "" if "%%i%%j" geq "%b1%" if "%%i%%j" leq "%b2%" @del "路径\%%n%%o" /a /f /q>nul 2>nul
Among them, set n= sets the length of the month. For example, if it is currently December, and you want to delete files from the previous 5 months, then set set n=5, that is, the months to be checked are July, August, September, October, November.
Note that n must be a number between 1 and 12.
Last edited by Hanyeguxing on 2009-12-16 at 20:59 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2009-12-16 20:39 |
|
|
hanxiaofeng888
新手上路

积分 5
发帖 4
注册 2009-11-19
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
楼上的 厉害 运用的如此巧妙和精简 学习了
The person upstairs is amazing. Using it so cleverly and concisely. Learned it.
|
|
2009-12-16 20:59 |
|
|
sj2009
新手上路

积分 10
发帖 10
注册 2009-12-14
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
非常感谢:qinchun36和Hanyeguxing;
我会认真学习
Thank you very much: qinchun36 and Hanyeguxing;
I will study seriously
|
|
2009-12-17 08:51 |
|