Board logo

标题: 请问用什么命令跳出FOR循环? [打印本页]

作者: hjting     时间: 2007-2-9 23:10    标题: 请问用什么命令跳出FOR循环?


@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if not exist %%a:\nul ‘这行后加跳出命令
if exist %%a:\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "可用字节"') do set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul
echo %%a 盘剩余空间:!freesize! MB
)
)
pause
作者: ccwan     时间: 2007-2-9 23:35

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if not exist %%a:\nul goto :eof
for /f "tokens=3" %%b in ('dir /-c %%a:\^|findstr "可用字节"') do set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul
echo %%a 盘剩余空间:!freesize! MB
)2>nul
pause
小小修改一下,看看有什么不同。

[ Last edited by ccwan on 2007-2-9 at 11:41 PM ]
作者: fastslz     时间: 2007-2-9 23:50
访问到光驱盘符即跳出for
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (C D E F G H I J K L M N O P Q R S T U V W) DO (
    VOL %%A: >NUL 2>NUL
    IF NOT ERRORLEVEL 1 (
       FOR /F "TOKENS=3" %%B IN ('DIR /-C %%A:\^|FIND "可用字节"') DO SET FREESIZE=%%B
       SET /A FREESIZE=!FREESIZE:~0,-3!/1049>NUL
       ECHO %%A 盘剩余空间:!FREESIZE! MB
       ) ELSE (
         GOTO END
       )
)
:END
PAUSE

作者: hjting     时间: 2007-2-10 00:21    标题: 请问goto :eof是不是跳出FOR的?

还有2>nul
作者: ccwan     时间: 2007-2-10 01:01
goto :eof 是跳转到批处理文件的结尾的意思,论坛里有很多帖子谈到。

if not exist %%a:\nul goto :eof这一句是说如果没有某个分区就跳转到批处理文件的结尾,当你的盘符不连续时也会直接跳出,要注意!

2>nul是屏蔽(不显示)所有的错误信息。

另外,不知你试过没有,3楼的代码我试过有问题,提示find参数格式不正确。我的测试环境,windows2000
作者: hjting     时间: 2007-2-10 01:10    标题: 帮忙看看纯DOS下的

@echo on
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%a:\nul (
.....
)else (
goto mg
))
:mg
dir

这样的for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) 运行后
%%a没有变成c d e f g h i j k l m n o p q r s t u v w x y z,何解?
作者: logictianjin     时间: 2007-4-10 23:27


  Quote:
Originally posted by ccwan at 2007-2-9 10:35 AM:
[code]@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if not exist %%a:\nul goto :eof
for /f "tokens=3" %%b in ('dir /-c %% ...

if not exist %%a:\nul

问题1 用这个命令可以判断空盘符?能否给讲下原理?
      2 \nul是什么意思?

问题已经搞清楚了,不用回答了,感谢ccwan的范例,让我学到很多!

[ Last edited by logictianjin on 2007-4-10 at 11:28 AM ]