中国DOS联盟论坛

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-09-15 12:42
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » A program that deletes other folders except keeping a few specified folders
Printable Version  4,009 / 15
Floor1 nbic Posted 2007-06-01 19:31
初级用户 Posts 61 Credits 135
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 ]
Floor2 wudixin96 Posted 2007-06-01 22:16
银牌会员 Posts 931 Credits 1,928
```
for /f %%i in ('dir F:\下载\ /b /ad ^|findstr "
Floor3 nbic Posted 2007-06-01 23:03
初级用户 Posts 61 Credits 135
Originally posted by wudixin96 at 2007-6-1 10:16 PM:
Floor4 wudixin96 Posted 2007-06-02 00:19
银牌会员 Posts 931 Credits 1,928
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



[ Last edited by wudixin96 on 2007-6-2 at 12:23 AM ]
Floor5 terse Posted 2007-06-02 00:27
银牌会员 Posts 946 Credits 2,404
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

Floor6 lxmxn Posted 2007-06-02 00:29
版主 Posts 4,938 Credits 11,386
```
@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 ]
Floor7 nbic Posted 2007-06-02 09:59
初级用户 Posts 61 Credits 135
The method on floor 6 is good. Thanks.
Floor8 qzwqzw Posted 2007-06-02 10:54
银牌会员 Posts 636 Credits 2,343
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.
Floor9 xjzhf4 Posted 2008-03-07 11:01
新手上路 Posts 5 Credits 12
Hehe, thank you everyone, learned it...
Floor10 Li103Z Posted 2008-04-11 16:38
初级用户 Posts 38 Credits 70
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 ]
Floor11 lxmxn Posted 2008-04-13 06:53
版主 Posts 4,938 Credits 11,386
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.
Floor12 lyaa Posted 2008-04-17 14:27
初级用户 Posts 17 Credits 40
Having spaces is definitely not okay
Floor13 hahaxiao Posted 2008-11-29 00:20
新手上路 Posts 2 Credits 3
Floor14 sea1112 Posted 2008-11-29 09:59
初级用户 Posts 119 Credits 131 From gz
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
删除文件夹以外的文件夹和文件.bat.rar (359 bytes)
Floor15 sea1112 Posted 2008-11-29 10:08
初级用户 Posts 119 Credits 131 From gz
Originally posted by lxmxn at 2007-6-2 12:29 AM:


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


File not found

Attachments
all.jpg (46.61 KiB)
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023