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-13 11:40
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » What's different about the FOR here? View 672 Replies 7
Original Poster Posted 2004-02-03 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
If I change *.txt in for %%a in (c,d,e) do if exist %%a:\*.txt dir /s %%a:\*.txt to *.gho, it only searches for gho files in the root directory. But if it's TxT or com or exe, it will also find files in the subdirectories. Why is that? Thanks...
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 2 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

The main reason is that in the loop body of your loop statement you used if exist %%a:\*.txt, that is, it only searches the subdirectories when the corresponding file exists in the root of the current drive. I guess in your test, the root of the tested drive had .TXT, .COM, .EXE files, but no image file.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2004-02-03 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
That is indeed the case. After replacing if exist with dir /s, it searches the subdirectories too. So that's how it is. Thanks...!
But if I need it to search subdirectories too, and do it without dir or external commands, using if exist or something else instead, how should I write it as a workaround?
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 4 Posted 2004-02-03 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

Do you mean how to traverse DOS directories? That is quite difficult to do in a batch file, and in most cases you can flexibly work around it with other approaches. Can you tell us your specific goal?
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2004-02-03 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Yes, I just want to execute a file once I find it, without relying on external commands. I've just started learning batch files, and I don't know how to traverse the subdirectories under one drive letter... thanks
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 6 Posted 2004-02-05 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

Directory traversal without using external commands? Sounds a bit like a joke.

First I'll give you a directory traversal using a recursive algorithm, only as illustrative code:

::visitdir.bat
if == call %0 \.

:main
cd %1
if exist 指定文件 指定操作
dir /ad /b > dirlist.txt
:subloop
dir dirlist.txt | find " 0" > nul
if not errorlevel 1 goto end
type nul > null.txt
fc dirlist.txt null.txt /n | find "1:" > setdir.bat
echo e 100 "set dir=">setdir.asd
echo w>>setdir.asd
echo q>>setdir.asd
debug setdir.bat nul
call setdir
call %0 %1\%dir%
find "%dir%" dirlist2.txt
copy dirlist2.txt dirlist.txt > nul
goto subloop
:end

The above program uses external commands fc, find, debug, or other string-processing tools.


※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2004-02-05 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Thank you, I've collected the above to study ; no joke, I'm just learning and simply didn't understand ; since external commands have to be used, then I might as well choose Locate, which can save 10 or so lines of code and more... thanks again !


一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 8 Posted 2004-03-13 00:00 ·  中国 山东 烟台 芝罘区 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

There are differences even among external commands. Because external commands like fc and find are programs included with most systems, your program does not need to provide them specially, which means portability is very strong. But I don't have the Locate you mentioned, so it would have to be distributed together with the calling code you wrote; and in terms of size, I believe Locate is definitely larger than the program I wrote.

Also, that recursive algorithm program had quite a few problems, so I spent some time writing a non-recursive one, and it has already passed testing. You can take a look.
http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9588
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: