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-09 21:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Automatically delete files and folders View 2,125 Replies 8
Original Poster Posted 2006-12-25 12:38 ·  中国 上海 浦东新区 电信
初级用户
Credits 169
Posts 17
Joined 2004-11-05 00:00
21-year member
UID 33346
Gender Male
Status Offline
There are 4 folders under my E drive. Can I create a batch script to protect these 4 folders from being deleted and automatically delete redundant files and folders?
Floor 2 Posted 2006-12-25 22:57 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The following is a demonstration code, which must be run on a partition other than drive E:

@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag echo "e:\%%i"
)
pause

test.txt must be placed on a partition other than drive E, and the content inside is the folder names without paths, one record per line, for example:

Folder 1
Folder 2
Folder 3
Folder 4

If you are satisfied with the effect of the above demonstration code, you can use the following code to delete all other files or folders under drive E except the specified folders:

@echo off
for /f "delims=" %%i in ('dir /a /b e:\') do (
set flag=
for /f "delims=" %%j in (test.txt) do if /i "%%i"=="%%j" set flag=1
if not defined flag del /a /f "e:\%%i" 2>nul||rd /s "e:\%%i" 2>nul
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-25 23:10
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-12-25 23:12 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Although it is the code of moderator namem, I also added points only after trying and succeeding.
Hehe...
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2006-12-25 23:57 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
::------------winxp sp2 disk clear--------------
::::::::::::::::::::Main Configuration::::::::::::::::::::
::Number of partitions you need to clean=3
::Which partitions are they=D:\;E:\;F:\
::----------------------------------------------
@echo off
:dc
cls
title=winxp sp2 disk clear
SETLOCAL ENABLEDELAYEDEXPANSION
SET list=C:\%random%.dat
if /i "%1" == "list" cls&goto parlist
for /f "tokens=2 delims==" %%i in ('findstr /B "::Number of partitions you need to clean" %0') do SET show=%%i
for /f "tokens=2,3,4,5,6 delims==;" %%i in ('findstr /B "::Which partitions are they" %0') do SET par1=%%i&SET par2=%%j&SET par3=%%k&SET par4=%%l&SET par5=%%m&SET par6=%%n
SET par|find "par%show%"&&goto list
echo config error...
pause>nul
goto dcend

:list
echo.>%list%
for /f "tokens=2 delims=>" %%i in ('findstr /B "::>" %0') do echo %%i>>%list%
goto partition

:parlist
set /p disk=Please enter the drive letter of the partition for which you need to create a list (e.g.: "C:" without "\")
ECHO %disk%|findstr /b /e /i "C: D: E: F: G: H: I: J: K: L: M: N:"||CLS&&ECHO Input error! &&goto parlist
echo.>%list%
for /f "delims=" %%i in ('dir /a:d /b %disk%\') do ECHO ::^>%disk%\%%i>>%list%
cls
set /p ch=%disk% root directory list completed, do you need to add it to this script? (Y/N):
if /i "%ch%" == "y" copy %0+"%list%" %0
notepad.exe %list%&goto dcend

:partition
SET par=!par%show%!
SET /A show=%show%-1
for /f "delims=" %%i in ('dir /A:D /B %par%') do findstr /B /E /C:"%par%\%%i" %list% >nul || rd /s /q "%par%%%i"
del /f /q /a %par%*.*
if %show%==0 goto dcend
goto partition

:dcend
del /f /q %list%
pause


:::::::::::::::::Excluded Directory Settings:::::::::::::::::
::>D:\LanGame
::>D:\My Virtual Machines
::>D:\RECYCLER
::>D:\System Volume Information
::>E:\DATA
::>E:\div
::>E:\DOW
::>E:\ghost
::>E:\ISO
::>E:\moliyo
::>E:\RECYCLER
::>E:\SOFTWARE
::>E:\System Volume Information
::>E:\World of Warcraft
::>F:\DOW
::>F:\Downloads
::>F:\L
::>F:\My Virtual Machines
::>F:\RECYCLER
::>F:\System Volume Information
::>F:\Test 一下
Floor 5 Posted 2006-12-26 00:12 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
```
@echo off
::If there are multiple paths, or exclude multiple folders:::
::Or exclude multiple files, please set manually:::::
::Multiple /c:"" parameters can be followed after findstr::::::
::For example: /c:"game" /c:"qq" /c:"net" ::::::
::Separate with spaces:::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::
:: code by zzlike 2006-12-11 CMD@XP::::
::::::::Delete folders:::::::::::::::::::::
for /f "delims=" %%i in ('dir /b /ad "d:\" "e:\" ^|findstr /i /v /b /e /c:"netgame" /c:"ghost" /c:"Game Menu"') do attrib -s -h -r -a /s /d "d:\%%i" && rd /s /q "d:\%%i"
::::::::Delete files:::::::::::::::::::::::
for /f "delims=" %%i in ('dir /b /a-d "d:\" "e:\" ^|findstr /i /v /b /e /c:"Exclude Delete Folder.bat" /c:"New Text Document.txt"') do del /a /f /q "d:\%%i"
```
bat c c++
Floor 6 Posted 2006-12-26 10:49 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Oh no~~
I accidentally deleted all files on drive E while writing VBS, and it was a heavy loss! :(
Fortunately, the code is written, but I don't have the courage to post it up :)
Here I would like to remind everyone to use the "delete" command with caution!
Floor 7 Posted 2006-12-28 10:05 ·  中国 广东 东莞 电信
新手上路
Credits 6
Posts 2
Joined 2006-12-25 14:54
19-year member
UID 74538
Gender Male
Status Offline
For example, there are multiple folders named after dates in a folder, and you want to keep the folders of the current date minus 7 days. How to write it with batch processing?
Floor 8 Posted 2007-04-18 04:42 ·  中国 浙江 杭州 萧山区 电信
新手上路
Credits 10
Posts 5
Joined 2006-12-27 22:39
19-year member
UID 74739
Gender Male
Status Offline
Be careful, but it's a nice piece of code. Thanks.
Floor 9 Posted 2008-09-25 16:01 ·  中国 广东 广州 白云区 电信
新手上路
Credits 3
Posts 2
Joined 2008-09-25 11:00
17-year member
UID 126571
Gender Male
Status Offline
Forum Jump: