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-06-28 16:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A program that deletes other folders except keeping a few specified folders View 3,573 Replies 15
Original Poster Posted 2007-06-01 19:31 ·  中国 广东 深圳 电信
初级用户
★★
Credits 135
Posts 61
Joined 2007-04-14 09:23
19-year member
UID 85115
Gender Male
Status Offline
Description: In the F:\Download folder, there are subfolders 1, 2, 3, 4, 5. The problem is to keep subfolders 1 and 2 and delete the remaining three folders. Use the following program. But it only keeps folder 1 and deletes the others.

for /f "skip=1 tokens=*" %%i in ('dir F:\Download\ /b /ad ^|find /v "F:\Download\1,2"') DO rd /s /q "F:\Download\%%i"
del F:\Download\*.* /a/q/f

Everyone, please help see how to modify the above program to achieve the purpose of keeping subfolders 1 and 2!

Thank you in advance!

[ Last edited by nbic on 2007-6-1 at 07:32 PM ]
Floor 2 Posted 2007-06-01 22:16 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
```
for /f %%i in ('dir F:\下载\ /b /ad ^|findstr "
Floor 3 Posted 2007-06-01 23:03 ·  中国 广东 深圳 电信
初级用户
★★
Credits 135
Posts 61
Joined 2007-04-14 09:23
19-year member
UID 85115
Gender Male
Status Offline
Originally posted by wudixin96 at 2007-6-1 10:16 PM:
for /f %%i in ('dir F:\下载\ /b /ad ^|findstr " 


Just tried it. It doesn't work.
Floor 4 Posted 2007-06-02 00:19 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
According to the LZ's idea, this p needs to be executed under F:\Download.

The result of dir F:\Download\ /b /ad is:

1
2
3
4
5

@echo off
for /f %%i in ('dir F:\Download\ /b /ad ^|findstr ""') DO rd /s /q F:\Download\%%i
pause


[ Last edited by wudixin96 on 2007-6-2 at 12:23 AM ]
Floor 5 Posted 2007-06-02 00:27 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by wudixin96 at 2007-6-2 00:19:
According to the original poster's idea. This p is to be executed under F:\Download.

The result of dir F:\Download\ /b /ad is:

1
2
3
4
5

@echo off
for /f %%i in ('dir F:\Download\ /b /ad ^|findstr &quo ...

This p is not to be executed under F:\Download
@echo off
for /f %%i in ('dir F:\Download\ /b /ad ^|findstr ""') DO rd /s /q F:\Download\%%i
pause
Floor 6 Posted 2007-06-02 00:29 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```
@echo off
for /f %%a in ('dir /b/ad F:\下载\? ^| findstr /v "1 2"') do rd /s/q F:\下载\%%a
```

[ Last edited by lxmxn on 2007-6-2 at 05:53 PM ]
Floor 7 Posted 2007-06-02 09:59 ·  中国 广东 深圳 电信
初级用户
★★
Credits 135
Posts 61
Joined 2007-04-14 09:23
19-year member
UID 85115
Gender Male
Status Offline
The method on floor 6 is good. Thanks.
Floor 8 Posted 2007-06-02 10:54 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
This is too simple. Consider something more complex.

The test path structure is as follows:
test
- test1
- test2
- test21
- test22
- test221
- test3
- test31
- test311
- test32

Requirements:

Delete all folders except test22 and test311.
The original directory structure is retained.
The test221 directory should be deleted.
The files under test31 should be deleted.
Floor 9 Posted 2008-03-07 11:01 ·  中国 新疆 昌吉回族自治州 电信
新手上路
Credits 12
Posts 5
Joined 2008-01-21 17:13
18-year member
UID 109187
Gender Male
Status Offline
Hehe, thank you everyone, learned it...
Floor 10 Posted 2008-04-11 16:38 ·  中国 广东 广州 荔湾区 电信
初级用户
Credits 70
Posts 38
Joined 2007-05-23 07:20
19-year member
UID 89024
Gender Male
Status Offline
I tried it using the method of moderator lxmxn, and such an error appeared on my computer: "findstr is not an internal or external command, nor is it a runnable program or batch file". What's the reason? Please help from the experts. Is it because of my DOS version?

[ Last edited by Li103Z on 2008-4-11 at 04:40 PM ]
Floor 11 Posted 2008-04-13 06:53 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by Li103Z at 2008-4-11 16:38:
I tried it using the method of moderator lxmxn, and the following error occurred on my computer: "findstr is not an internal or external command, nor is it a runnable program or batch file". What is the reason...

Please state your operating environment.
Floor 12 Posted 2008-04-17 14:27 ·  中国 上海 电信
初级用户
Credits 40
Posts 17
Joined 2007-08-10 14:49
18-year member
UID 94928
Gender Male
Status Offline
Having spaces is definitely not okay
Floor 13 Posted 2008-11-29 00:20 ·  中国 北京 鹏博士
新手上路
Credits 3
Posts 2
Joined 2008-11-25 21:33
17-year member
UID 132095
Gender Male
Status Offline
Floor 14 Posted 2008-11-29 09:59 ·  中国 广东 电信
初级用户
★★
Credits 131
Posts 119
Joined 2007-06-12 22:50
19-year member
UID 91125
Gender Male
From gz
Status Offline
Originally posted by nbic at 2007-6-1 07:31 PM:
Explanation: In the F:\Download Folder, there are subfolders 1, 2, 3, 4, 5. The problem is to keep subfolders 1 and 2. Delete the remaining three folders. Use the following program. It can only keep folder 1...



Please save the content as "Delete Folders and Files Other Than Folders.bat" and run it in the directory

@echo off
::Delete all files other than itself
for %%a in (*) do (if "%%a" neq "Delete Folders and Files Other Than Folders.bat" del "%%a" /q)
::Delete all folders other than the specified folders
for /d %%b in (*) do (if "%%b" neq "1" if "%%b" neq "2" rd "%%b" /q/s)
Attachments
Floor 15 Posted 2008-11-29 10:08 ·  中国 广东 电信
初级用户
★★
Credits 131
Posts 119
Joined 2007-06-12 22:50
19-year member
UID 91125
Gender Male
From gz
Status Offline
Originally posted by lxmxn at 2007-6-2 12:29 AM:

@echo off
for /f %%a in ('dir /b/ad F:\下载\? ^| findstr /v "1 2"') do rd /s/q F:\下载\%%a


[ Last edited by lxmxn on 2007-6-2 at 05:53 PM ]


File not found
Attachments
all.jpg
Forum Jump: