Batch scripting exit from For loop.

Status
Not open for further replies.

ValerieMay

Beta member
Messages
1
Win XP Home SP.3+

The purpose of the batch script below is to detect the drive letter of an installed optical device and if the disk in the device contains a specific file (in this case Nero.ico) copy it to a folder. I would like to exit the For loop when copying is complete, in other words when the copying is finished the loop is redundant so does not have to continue. Exit and Exit /B won't do it as they will cause the script to be exited, Goto and Call :sub will leave the loop "live". Is there a command just to exit a For loop when a condition is met?

Code:
@echo off>%temp%\nero.ico
cls
setlocal enabledelayedexpansion

for %%1 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 %%1: (
    fsutil fsinfo drivetype %%1:| find /i "cd-rom">nul && set cdrom=%%1
    if exist !cdrom!:\nero.ico copy !cdrom!:\nero.ico f:\temp\ > nul
    set cdrom=
    
          )
  )

:: more coding here.....
 
I don't understand why the goto sub doesn't work , but i'll trust that you tested it. i think you may need create new logic. the following concept may help.

Working With Batch Files

Let's consider one more example of the FOR loop. See if you can determine what the following batch file does.

MYVOLS.BAT
----------
@ECHO OFF
FOR %%v IN (A: C: D:) DO VOL %%v >> LABELS.ARE
FIND "Volume in drive " LABELS.ARE
DEL LABELS.ARE


also, this may be much easier to do in vbscript

VBScript Objects Drives Collection - DevGuru Quick Reference
 
Status
Not open for further replies.
Back
Top Bottom