中国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-10 16:54
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Ask about batch processing: How to confirm whether a specified file exists on the hard disk?
Printable Version  5,567 / 26
Floor1 lianjiang2004 Posted 2006-10-29 09:06
金牌会员 Posts 1,884 Credits 3,946
To ask about batch processing: How to confirm whether a specified file exists on the hard disk?
I want to implement the following task, how to write the batch processing? That is, starting from the C drive to O, check whether the aaa.txt file exists in the root directory. If it exists, output the drive letter to a variable and perform other tasks; otherwise, continue searching until the O drive ends.
Floor2 namejm Posted 2006-10-29 09:31
荣誉版主 Posts 1,737 Credits 5,226 From 成都
```
@echo off
for %%i in (c d e f g h i j k l m n o) do if exist %%i:\aaa.txt goto another
goto :eof

:another
command
```
Floor3 yangzhiyi Posted 2006-10-29 09:34
中级用户 Posts 123 Credits 261
IF EXIST c:\aaa.txt set 变量=c:
IF EXIST d:\aaa.txt set 变量=d:
...
IF EXIST o:\aaa.txt set 变量=o:
Other tasks
Direct enough, or
IF EXIST c:\aaa.txt set 变量=c: & goto end
IF EXIST o:\aaa.txt set 变量=o: & goto end
:end
Other tasks

Still the above floor is powerful, I have never known how to use for, can someone send a detailed explanation of for?

[ Last edited by yangzhiyi on 2006-10-29 at 09:36 AM ]
Floor4 lianjiang2004 Posted 2006-10-29 09:38
金牌会员 Posts 1,884 Credits 3,946
Thanks moderator, what does goto :eof mean? Also, I need to know which drive this file is on. I need to pass %%i to a variable to operate on this file. I borrowed a DOS book for this function and searched online for a long time, but still haven't figured it out.
Floor5 lianjiang2004 Posted 2006-10-29 09:40
金牌会员 Posts 1,884 Credits 3,946
Originally posted by yangzhiyi at 2006-10-29 09:34:
IF EXIST c:\aaa.txt set 变量=c:
IF EXIST d:\aaa.txt set 变量=d:
...
IF EXIST o:\aaa.txt set 变量=o:
------------------
This way I also thought of it, but it's too cumbersome. Hehe. Thanks.
Floor6 lianjiang2004 Posted 2006-10-29 09:46
金牌会员 Posts 1,884 Credits 3,946
This isn't this okay? There are too many commands in one sentence.
for %%q in (c d e f g h i j k l) do set abc=%%q if exist %abc%:\aaa.txt goto ok1
Floor7 namejm Posted 2006-10-29 09:48
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Generally speaking, the function of goto :eof is equivalent to exit. However, if you use call to call a certain label segment, the label segment must contain a goto :eof and cannot be replaced by exit.

If you want to know on which drive this file is, you can write a little more code for 2F:
Floor8 lianjiang2004 Posted 2006-10-29 09:58
金牌会员 Posts 1,884 Credits 3,946
Thanks, I understand. The usage in parentheses.
Floor9 lianjiang2004 Posted 2006-10-29 11:01
金牌会员 Posts 1,884 Credits 3,946
What's going on? There's an error when running. Did I make a mistake somewhere?
---------
1.bat

rem @echo off
REM Determine the location of _c.bat
for %%i in (c d e f) do (if exist %%i:\_c.bat set drv_c=%%i goto ok1)
_cn.bat
goto :eof

:ok1
rem Find the drive letter where the file is located.
--------------------------------------------------
After running, it shows
D:\>(if exist c:\_c.bat set drv_c=c goto ok1)
Bad command or file name
Floor10 lianjiang2004 Posted 2006-10-29 11:04
金牌会员 Posts 1,884 Credits 3,946
This also doesn't work.
rem @echo off
REM Determine the location of _c.bat
for %%i in (c d e f) do (
if exist %%i:\_c.bat set drv_c=%%i goto ok1
)
_cn.bat
goto :eof

:ok1
rem Find the drive letter where the file is located.
Floor11 namejm Posted 2006-10-29 11:10
荣誉版主 Posts 1,737 Credits 5,226 From 成都
set drv_c=%%i & goto ok1
Floor12 lianjiang2004 Posted 2006-10-29 20:57
金牌会员 Posts 1,884 Credits 3,946
Moderator, the two methods below still don't work. Could it be that the characters changed when copied out? I'm really sorry. Can you send it to me as an attachment?
-----------
rem @echo off
for %%i in (c d e) do (if exist %%i:\aaa.txt set drive=%%i&goto another)
goto :eof

:another
echo aaa.txt is located at %drive%
--------------
rem @echo off
for %%i in (c d e) do (
if exist %%i:\aaa.txt set drive=%%i&goto another
)
goto :eof

:another
echo aaa.txt is located at %drive%
Floor13 namejm Posted 2006-10-30 00:25
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Observing your modified code, there is no pause statement at the end, and I suspect it's because after finding it, it exits directly without pausing, causing you to mistakenly think the code is wrong.

I have enclosed the code with [code] and [/code]. You can click the [Copy to clipboard] of 7F to copy the code exactly. Just replace the last command with pause. It passed the test on my machine. I wonder if there is an aaa.txt in the root directory of your machine. Please check again.

[[i] Last edited by namejm on 2006-10-30 at 01:03 AM [/i]]
Floor14 lianjiang2004 Posted 2006-10-30 01:27
金牌会员 Posts 1,884 Credits 3,946
I'll take another look. When I was testing, I added rem at the beginning of the first sentence and added pause on the line above goto: eof.
-------------
Still not working. To narrow down the loop, I changed it to search for c-e. Here are the virtual machine screenshots:

[ Last edited by lianjiang2004 on 2006-10-30 at 02:16 AM ]

Attachments
snap.jpg (21.32 KiB)
snap1.jpg (22.8 KiB)
snap2.jpg (21.16 KiB)
Floor15 lxmxn Posted 2006-10-30 04:18
版主 Posts 4,938 Credits 11,386

Is the person upstairs testing using a virtual machine?

Is it testing using the DOS system? In DOS batch processing, it seems that the use of "(" and ")" is not supported. It is recommended to change it to the form of "goto "...
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023