中国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-13 10:34
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS学习入门 & 精彩文章 (教学室) » What's different about the FOR here?
Printable Version  671 / 7
Floor1 xiaojun Posted 2004-02-03 00:00
银牌会员 Posts 499 Credits 2,202
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...
Floor2 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
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.
Floor3 xiaojun Posted 2004-02-03 00:00
银牌会员 Posts 499 Credits 2,202
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?
Floor4 willsort Posted 2004-02-03 00:00
元老会员 Posts 1,512 Credits 4,432
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?
Floor5 xiaojun Posted 2004-02-03 00:00
银牌会员 Posts 499 Credits 2,202
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
Floor6 willsort Posted 2004-02-05 00:00
元老会员 Posts 1,512 Credits 4,432
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.


Floor7 xiaojun Posted 2004-02-05 00:00
银牌会员 Posts 499 Credits 2,202
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 !


Floor8 willsort Posted 2004-03-13 00:00
元老会员 Posts 1,512 Credits 4,432
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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023