|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『楼 主』:
请教批处理:如何确认指定文件在硬盘上是否存在?
使用 LLM 解释/回答一下
请教批处理:如何确认指定文件在硬盘上是否存在?
我想实现下面的任务,如何写批处理? 就是从C盘开始到O为止,检查根目录下是否存在aaa.txt这个文件,存在的话,就输出盘符给以变量,并执行其他任务,否则继续查找直到O盘结束。
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.
|
|
2006-10-29 09:06 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
@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
```
@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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-29 09:31 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
IF EXIST c:\aaa.txt set 变量=c:
IF EXIST d:\aaa.txt set 变量=d:
...
IF EXIST o:\aaa.txt set 变量=o:
其它任务
够直接吧,或
IF EXIST c:\aaa.txt set 变量=c: & goto end
IF EXIST o:\aaa.txt set 变量=o: & goto end
:end
其它任务
还是楼上的利害,我一直都不会用 for ,有人能发一个for 的详细说明吗
Last edited by yangzhiyi on 2006-10-29 at 09:36 AM ]
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 ]
|
|
2006-10-29 09:34 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
谢谢版主,请问goto :eof 什么意思?另外我还需要知道这个文件在哪个盘,需要把%%i传给一个变量,好对这个文件进行操作。
为这个功能借了DOS书,上网也查了半天,还没会。
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.
|
|
2006-10-29 09:38 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
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:
------------------
这样我也想到了,就是太麻烦。呵呵。谢谢了。
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.
|
|
2006-10-29 09:40 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
这样不行吧?一句里命令太多。
for %%q in (c d e f g h i j k l) do set abc=%%q if exit %abc%:\aaa.txt goto ok1
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
|
|
2006-10-29 09:46 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
一般而言,goto :eof 的功能相当于exit,但是,如果用 call 来调用某个标签段的话,这个标签段中必须包含一个 goto :eof 而不能用 exit 来替换。
如果想知道这个文件在哪个盘上的话,把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位于 %drive% 分区
command
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-29 09:48 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
多谢了,明白了,用括号括起来的用法。
Thanks, I understand. The usage in parentheses.
|
|
2006-10-29 09:58 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
怎么回事?运行出错,哪里我哪里写错了吗?
---------
1.bat
rem @echo off
REM 判断_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 找到文件所在盘符。
--------------------------------------------------
运行后显示
D:\>(if exist c:\_c.bat set drv_c=c goto ok1)
Bad command or file name
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
|
|
2006-10-29 11:01 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
这样也不行。
rem @echo off
REM 判断_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 找到文件所在盘符。
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.
|
|
2006-10-29 11:04 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
set drv_c=%%i 和 goto ok1 之间还有一个连接符 &
set drv_c=%%i & goto ok1
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-29 11:10 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
版主,下面两种都还是不行呀。是不是拷贝出来字符变掉了?实在不好意思,能不能以附件形式给我?
-----------
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位于 %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位于 %drive%
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%
|
|
2006-10-29 20:57 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
观察你修改后的代码,最后没有暂停语句pause,怀疑是因为找到了之后却不暂停就直接退出了,导致你误认为代码出错。
我已经把代码用[code]和[/code]括起来了,你点7F的 [Copy to clipboard] 就可以把代码分毫不差地复制下来,最后一个command你用pause来替换就可以了。在我的机子上测试通过,不知道你机子上是否存在一个根目录下的aaa.txt,请你再检查一次。
[[i] Last edited by namejm on 2006-10-30 at 01:03 AM [/i]]
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 and
. You can click the 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.
Last edited by namejm on 2006-10-30 at 01:03 AM ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-30 00:25 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我再看看。我在测试时都把第一句前加了rem,在goto: eof上句加了pause。
-------------
还是不行。为缩小循环,改成查找c-e。虚拟机截图如下:
Last edited by lianjiang2004 on 2006-10-30 at 02:16 AM ]
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 ]
附件
1: snap.jpg (2006-10-30 02:16, 21.32 KiB, 下载附件所需积分 1 点
,下载次数: 3)
附件
2: snap1.jpg (2006-10-30 02:16, 22.8 KiB, 下载附件所需积分 1 点
,下载次数: 3)
附件
3: snap2.jpg (2006-10-30 02:16, 21.16 KiB, 下载附件所需积分 1 点
,下载次数: 3)
|
|
2006-10-30 01:27 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
楼上的是用虚拟机测试的?
是不是用的DOS系统测试的啊?DOS里面的批处理好象不支持"("和")"的使用,建议改成"goto "的形式...
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 "...
|
|
2006-10-30 04:18 |
|