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-08-12 23:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Asking for "a bat for regularly cleaning folders" (waiting for the 2nd day) View 2,887 Replies 16
Original Poster Posted 2009-12-15 14:46 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
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 ]
Floor 2 Posted 2009-12-15 15:12 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 3 Posted 2009-12-15 22:14 ·  中国 山东 德州 联通
新手上路
Credits 5
Posts 4
Joined 2009-11-19 14:48
16-year member
UID 154975
Gender Male
Status Offline
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.
Floor 4 Posted 2009-12-16 09:05 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
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
Floor 5 Posted 2009-12-16 09:06 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
Thanks, brother on the 3rd floor.
Floor 6 Posted 2009-12-16 12:56 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
Floor 7 Posted 2009-12-16 16:02 ·  中国 北京 中电华通通信有限公司
高级用户
★★★
据说是李先生
Credits 609
Posts 400
Joined 2008-04-23 15:55
18-year member
UID 116706
Gender Male
Status Offline
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┃
┗━━━━━━┛
Floor 8 Posted 2009-12-16 16:04 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
Returning to LS, the basis for cleaning is "creation time"
Floor 9 Posted 2009-12-16 16:05 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
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
Floor 10 Posted 2009-12-16 16:14 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
Floor 11 Posted 2009-12-16 17:11 ·  中国 北京 中电华通通信有限公司
高级用户
★★★
据说是李先生
Credits 609
Posts 400
Joined 2008-04-23 15:55
18-year member
UID 116706
Gender Male
Status Offline
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┃
┗━━━━━━┛
Floor 12 Posted 2009-12-16 19:14 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 13 Posted 2009-12-16 20:39 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
hanxiaofeng888 +1 2009-12-16 20:59
Floor 14 Posted 2009-12-16 20:59 ·  中国 山东 德州 联通
新手上路
Credits 5
Posts 4
Joined 2009-11-19 14:48
16-year member
UID 154975
Gender Male
Status Offline
The person upstairs is amazing. Using it so cleverly and concisely. Learned it.
Floor 15 Posted 2009-12-17 08:51 ·  中国 上海 电信
新手上路
Credits 10
Posts 10
Joined 2009-12-14 14:59
16-year member
UID 156571
Gender Male
Status Offline
Thank you very much: qinchun36 and Hanyeguxing;
I will study seriously
Forum Jump: