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-06-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask about batch processing: How to confirm whether a specified file exists on the hard disk? View 5,392 Replies 26
Original Poster Posted 2006-10-29 09:06 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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.
Floor 2 Posted 2006-10-29 09:31 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
```
@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
```
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-10-29 09:34 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
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 ]
Floor 4 Posted 2006-10-29 09:38 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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.
Floor 5 Posted 2006-10-29 09:40 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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.
Floor 6 Posted 2006-10-29 09:46 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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
Floor 7 Posted 2006-10-29 09:48 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
for %%i in (c d e f g h i j k l m n o) do (
if exist %%i:\aaa.txt set drive=%%i&goto another
)
goto :eof

:another
echo aaa.txt is located in the %drive% partition
command
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-10-29 09:58 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
Thanks, I understand. The usage in parentheses.
Floor 9 Posted 2006-10-29 11:01 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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
Floor 10 Posted 2006-10-29 11:04 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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.
Floor 11 Posted 2006-10-29 11:10 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
set drv_c=%%i & goto ok1
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-10-29 20:57 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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%
Floor 13 Posted 2006-10-30 00:25 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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]]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-10-30 01:27 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
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
snap1.jpg
snap2.jpg
Floor 15 Posted 2006-10-30 04:18 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

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 "...
Forum Jump: