中国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-08-26 16:34
47,812 topics / 349,910 posts / today 0 new / 48,264 members
其它操作系统综合讨论区 » Delete all subdirectories under a directory (not including the parent directory) (Solved)
Printable Version  2,422 / 10
Floor1 haiou327 Posted 2008-01-01 15:31
高级用户 Posts 348 Credits 713
Question: delete all subdirectories under a directory (not including the parent directory)
If I want to delete all subdirectories under HAIOU, but keep the parent directory HAIOU itself.
Using RD/S/Q HAIOU will pull out the entire HAIOU directory by the roots.

[ Last edited by haiou327 on 2008-1-1 at 08:11 PM ]
Floor2 haiou327 Posted 2008-01-01 15:37
高级用户 Posts 348 Credits 713
RMDIR path
RD path

/S In addition to the directory itself, also deletes all subdirectories and
files under the specified directory. Used to delete a directory tree.


Using the /S parameter deletes the parent directory too
Floor3 chishingchan Posted 2008-01-01 16:35
银牌会员 Posts 538 Credits 1,284
My idea: (not convenient to test, please test it yourself. I don't know if there are any mistakes)
Floor4 haiou327 Posted 2008-01-01 19:15
高级用户 Posts 348 Credits 713
Test failed,
Floor5 haiou327 Posted 2008-01-01 19:29
高级用户 Posts 348 Credits 713
If there are a few hundred directories with random numbers in the HAIOU directory, and I want to delete those directories but keep the HAIOU directory,
how should a batch file like this be written.
C:\HAIOU
├─1
├─10
├─100
├─101
├─102
├─103
├─104
├─105
├─106
├─107
├─108
├─109
├─11
├─110
├─111
├─112
├─113
├─114
├─115
├─116
├─117
├─118
├─119
├─12
├─120
├─121
├─122
├─123
├─124
├─125
├─126
├─127
├─128
├─129
├─13
├─130
├─131
├─132
├─133
├─134
├─135
├─136
├─137
├─138
├─139
├─14
├─140
├─141
├─142
├─143
├─144
├─145
├─146
├─147
├─148
├─149
├─15
├─150
├─151
├─152
├─153
├─154
├─155
├─156
├─157
├─158
├─159
├─16
├─160
├─161
├─162
├─163
├─164
├─165
├─166
├─167
├─168
├─169
├─17
├─170
├─171
├─172
├─173
├─174
├─175
├─176
├─177
├─178
├─179
├─18
├─180
├─181
├─182
├─183
├─184
├─185
├─186
├─187
├─188
├─189
├─19
├─190
├─191
├─192
├─193
├─194
├─195
├─196
├─197
├─198
├─199
├─2
├─20
├─200
├─21
├─22
├─23
├─24
├─25
├─26
├─27
├─28
├─29
├─3
├─30
├─31
├─32
├─33
├─34
├─35
├─36
├─37
├─38
├─39
├─4
├─40
├─41
├─42
├─43
├─44
├─45
├─46
├─47
├─48
├─49
├─5
├─50
├─51
├─52
├─53
├─54
├─55
├─56
├─57
├─58
├─59
├─6
├─60
├─61
├─62
├─63
├─64
├─65
├─66
├─67
├─68
├─69
├─7
├─70
├─71
├─72
├─73
├─74
├─75
├─76
├─77
├─78
├─79
├─8
├─80
├─81
├─82
├─83
├─84
├─85
├─86
├─87
├─88
├─89
├─9
├─90
├─91
├─92
├─93
├─94
├─95
├─96
├─97
├─98
└─99

[ Last edited by haiou327 on 2008-1-1 at 07:33 PM ]
Floor6 haiou327 Posted 2008-01-01 20:07
高级用户 Posts 348 Credits 713
Already solved, thanks for chishingchan's hint, but it's FOR, not IF

Posting the code
for /f %%a in ('dir /ad /b /s c:\haiou') do rd /s/q %%a

[ Last edited by haiou327 on 2008-8-1 at 10:51 PM ]
Floor7 chishingchan Posted 2008-01-03 16:35
银牌会员 Posts 538 Credits 1,284

Already solved, thanks for chishingchan's hint, but it's FOR, not IF


You're right, I just made a typo.
Floor8 lslcxlsl Posted 2008-07-24 15:52
初级用户 Posts 24 Credits 52
You play DOS commands so well, there are really a lot of talented people in the DOS Union. I still need to work harder
Floor9 fuuma Posted 2008-07-25 23:07
初级用户 Posts 14 Credits 32
Actually you can use RD /S /Q and then create a HAIOU folder again
Floor10 lnlhg Posted 2010-07-27 09:13
新手上路 Posts 3 Credits 6
Originally posted by haiou327 at 2008-1-1 20:07:
Already solved, thanks for chishingchan's hint, but it's FOR, not IF

Posting the code
for /f %%a in ('dir /ad /b /s c:\haiou') do rd /s/q %%a

After using it, it reports an error. My system is windows2003.
Floor11 wl00560 Posted 2010-08-01 00:14
银牌会员 Posts 709 Credits 1,384
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023