![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-09 20:56 |
47,811 topics / 349,897 posts / today 2 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » Automatically delete files and folders |
| Printable Version 2,124 / 8 |
| Floor1 pengxb | Posted 2006-12-25 12:38 |
| 初级用户 Posts 17 Credits 169 | |
|
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?
|
|
| Floor2 namejm | Posted 2006-12-25 22:57 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The following is a demonstration code, which must be run on a partition other than drive E:
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: 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: |
|
| Floor3 ccwan | Posted 2006-12-25 23:12 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Although it is the code of moderator namem, I also added points only after trying and succeeding.
Hehe... |
|
| Floor4 everest79 | Posted 2006-12-25 23:57 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
::------------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 一下 |
|
| Floor5 a9319751 | Posted 2006-12-26 00:12 |
| 中级用户 Posts 170 Credits 439 | |
|
```
@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" ``` |
|
| Floor6 jmz573515 | Posted 2006-12-26 10:49 |
| 银牌会员 Posts 464 Credits 1,212 | |
|
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! |
|
| Floor7 fyosa | Posted 2006-12-28 10:05 |
| 新手上路 Posts 2 Credits 6 | |
|
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?
|
|
| Floor8 formatcat | Posted 2007-04-18 04:42 |
| 新手上路 Posts 5 Credits 10 | |
|
Be careful, but it's a nice piece of code. Thanks.
|
|
| Floor9 sparkmo | Posted 2008-09-25 16:01 |
| 新手上路 Posts 2 Credits 3 | |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |