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-07-01 18:23
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to write such a batch processing.. Thanks View 1,918 Replies 10
Original Poster Posted 2006-06-28 13:14 ·  中国 江苏 南京 电信
初级用户
Credits 26
Posts 9
Joined 2006-06-28 13:10
20-year member
UID 57682
Status Offline
I want to keep only one NET folder on drive E. Delete all folders and files except this folder. And don't change the attributes of the NET folder. How to write such a batch script? Thanks for the advice.
Floor 2 Posted 2006-06-28 14:00 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Briefly introduce two methods:

1. Traditional for method:

@echo off
pushd e:\ rem Switch to the root directory of drive E
del /q *.* rem Delete all files in the root directory
for /d %%i in (*) do if /i not "%%i" == "NET" rd /s /q "%%i" rem If the directory is not NET, delete it
popd rem Return to the initial directory


2. WMIC
Using WMIC, not only the probability of errors is much smaller, but also the execution speed is good.
wmic fsdir where "drive='e:' and filename!='NET'" call delete /NOINTERACTIVE
This sentence can delete other directories on drive E except net. However, because during testing, there was a situation where files in multi-level directories could not be completely deleted at once, so it is best to add a statement to judge whether there is only the net directory in the current directory:

@echo off
pushd e:\
del /q *.*

:start
dir /b e: | findstr /v /i "net" || goto end
call :deldir
goto start

:deldir
wmic fsdir where "drive='e:' and filename!='net'" call delete /NOINTERACTIVE
goto :eof

:end
popd


CMD is profound. Of course, there are more than these two methods to achieve the purpose of the landlord. It can also use special files, directory names, directory special attributes and other methods. Looking forward to the next floor to explore.
Floor 3 Posted 2006-06-28 16:21 ·  中国 江苏 南京 电信
初级用户
Credits 26
Posts 9
Joined 2006-06-28 13:10
20-year member
UID 57682
Status Offline
Floor 4 Posted 2006-06-28 16:25 ·  中国 江苏 南京 电信
初级用户
Credits 26
Posts 9
Joined 2006-06-28 13:10
20-year member
UID 57682
Status Offline
Method 1 works very well.. Method 2 is not the kind I want. Because it will delete the files in the NET folder but not the folder. Thank you very much for your technical support.. Learned it..
Floor 5 Posted 2006-06-28 16:35 ·  中国 江苏 南京 电信
初级用户
Credits 26
Posts 9
Joined 2006-06-28 13:10
20-year member
UID 57682
Status Offline
Floor 6 Posted 2006-08-31 03:11 ·  中国 重庆 璧山区 电信
中级用户
★★
Credits 235
Posts 109
Joined 2006-08-24 00:52
19-year member
UID 61161
Gender Male
Status Offline
Please clarify the specific content that needs to be translated. You only provided "知道的告诉我一下 。QICQ:82015930", and it seems incomplete. Please provide the full text that needs to be translated.
Floor 7 Posted 2006-09-01 21:10 ·  中国 上海 长宁区 电信
新手上路
Credits 18
Posts 7
Joined 2006-09-01 08:31
19-year member
UID 61668
Status Offline
Thanks, I've also learned a trick
Floor 8 Posted 2006-09-19 04:39 ·  中国 广东 广州 越秀区 电信
中级用户
★★
Credits 259
Posts 112
Joined 2006-09-18 04:55
19-year member
UID 62928
Gender Male
Status Offline
Heheh, not bad. I'm here to give a thumbs up!~
Floor 9 Posted 2006-09-19 08:11 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Floor 10 Posted 2006-09-20 05:33 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 70
Posts 27
Joined 2006-09-19 19:23
19-year member
UID 63080
Gender Male
From 河南
Status Offline
DEL attributes]] names
ERASE attributes]] names

names Specify one or more file or directory lists. Wildcards can be used to delete multiple files. If a directory is specified, all files in the directory will be deleted.

/P Prompt for confirmation before deleting each file.
/F Force deletion of read-only files.
/S Delete specified files from all subdirectories.
/Q Quiet mode. Do not require confirmation when deleting global wildcards.
/A Select files to delete based on attributes.
attributes R Read-only files S System files
H Hidden files A Archive files
- Prefix indicating "no"

Can be accomplished with attrib, but it will change the attributes of the folder

[ Last edited by uime on 2006-9-20 at 05:35 ]
Floor 11 Posted 2006-09-20 10:09 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Heheh~ Learned two new commands
pushd
popd
业精于勤而荒于嬉,形成于思而毁于随。
Forum Jump: