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-08-10 12:08
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [fixed] Code for traversing the whole drive to delete folders won't work View 688 Replies 4
Original Poster Posted 2008-11-17 09:31 ·  中国 山东 济南 联通
高级用户
★★★
Credits 959
Posts 474
Joined 2007-10-25 10:40
18-year member
UID 100716
Gender Male
Status Offline
It can only delete the 123456 folders inside subdirectories. If I create a 123456 folder in the root directory, it can't delete it. Please take a look at where my code is wrong.

for %%a in (c d e f) do (if exist %%a: (for /f "delims=" %%b in ('dir /s /b /d /a "%%a:\123456"') do (rd /s/q "%%b")))


[ Last edited by HAT on 2008-11-17 at 11:14 ]
Floor 2 Posted 2008-11-17 10:16 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
It really is like that. For example, if the folder C:\123456 exists, then dir /s /a:d /b C:\123456 starts looking for the 123456 folder under C:\123456\.

What I usually do is first check (if exist) whether there is such a file (folder) in the root directory...
@echo off
for %%A in (c d e f) do (
if exist %%A: (
if exist "%%A:\123456\" rd /s /q "%%A:\123456\"
for /f "delims=" %%a in ('dir /s /a:d /b "%%A:\123456" 2^>nul') do (
rd /s /q "%%a"
)
)
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +2 2008-11-17 11:13
Floor 3 Posted 2008-11-17 10:36 ·  中国 山东 济南 联通
高级用户
★★★
Credits 959
Posts 474
Joined 2007-10-25 10:40
18-year member
UID 100716
Gender Male
Status Offline
Thanks to the friend in the post above.
Floor 4 Posted 2008-11-17 10:39 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Try this?

@echo off
for %%a in (c d e f) do (
if exist %%a: (
for /f "delims=" %%i in ('dir /a /s /ad /b "%%a:\*"^|findstr /i /e "\123456"') do (
echo rd /s/q "%%i"
)
)
)
echo.&pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +2 2008-11-17 11:13
Floor 5 Posted 2008-11-17 10:48 ·  中国 山东 济南 联通
高级用户
★★★
Credits 959
Posts 474
Joined 2007-10-25 10:40
18-year member
UID 100716
Gender Male
Status Offline
moniuming, thank you very much, the test succeeded!!
Forum Jump: