0

I am trying to set a global variable within the for loop sing the index. Then i want to break the loop and print out that value. The code looks as follows:

SetLocal EnableDelayedExpansion
for %%d in (z y x w v u t s r q p o n m l k j i h g f e d c) do (
set letter = %%d
if exist %%d:\ (echo nothing) else (goto break)
)

:break
echo %letter%
timeout 30

For some reason i cannot echo the value of %letter% what am I doing wrong?

Gerhard
  • 22,678
  • 7
  • 27
  • 43

2 Answers2

2

Why the spaces before and after = in set? that would create a variable called %letter % (trailing space) and a value of d (leading space)

SetLocal EnableDelayedExpansion
for %%d in (z y x w v u t s r q p o n m l k j i h g f e d c) do (
  set "letter=%%d"
  if exist %%d:\ (
       echo nothing
      ) else (
       goto break
     )
 )

:break
echo %letter%
timeout 30

Just one thing you need to understand from your script, if any of the drives does not exist, it will exit the loop, echo it and will not return to the loop.. If that is what your plan is, then fine, else, call the label, but rather get rid of the label and run the code in the else portion of the loop.

Or a better option maybe:

@echo off
for %%d in (z y x w v u t s r q p o n m l k j i h g f e d c) do (
 if not exist %%d:\ echo %%d & timeout 30
)

If you do not want to wait 30 seconds for each non existant result, you can remove & and mobe the timeout 30 statement to below the last parenthesis, for instance:

@echo off
for %%d in (z y x w v u t s r q p o n m l k j i h g f e d c) do (
 if not exist %%d:\ echo %%d
)
timeout 30
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

As your minimal code example currently makes little sense to me, also mentioned by Gerhard Barnard, I have decided to post a few code snippets for your information.

I prefer to use Mountvol for this type of thing. It would certainly help in cases such as an empty optical drive at e.g. D:\, where If Exist D:\ will fail, but that wouldn't necessarily mean the drive letter D: is okay to assign to a new drive.

If you're just trying to identify the first mounted drive letter, alphabetically:

@Set "_l="&For /F Tokens^=* %%A In ('MountVol^|Find ":\"^|Sort/R')Do @Set "_l=%%~dA"
@Echo(The first letter assigned to a drive is %_l%&Pause

To identify the last mounted drive letter, alphabetically:

@Set "_l="&For /F Tokens^=* %%A In ('MountVol^|Find ":\"^|Sort')Do @Set "_l=%%~dA"
@Echo(The last letter assigned to a drive is %_l%&Pause

To determine the first available, unmounted, drive letter, alphabetically:

@Set "_l="&For /L %%A In (67 1 90)Do @Cmd/C Exit/B %%A&Call:Sub %%=EXITCODEASCII%%
@Echo(The first available drive letter is %_l%&Pause&Exit/B
:Sub
@If Not Defined _l MountVol|Find "%1:\">Nul||Set "_l=%1:"

And to determine the last available, unmounted, drive letter, alphabetically.

@Set "_l="&For /L %%A In (90 -1 67)Do @Cmd/C Exit/B %%A&Call:Sub %%=EXITCODEASCII%%
@Echo(The last available drive letter is %_l%&Pause&Exit/B
:Sub
@If Not Defined _l MountVol|Find "%1:\">Nul||Set "_l=%1:"

In the latter two examples you can replace 67 with 66 if you wish to include a possible B: drive, or with 65 if you wish to also include a possible A: drive, you can also replace it with 68 to exclude the C: drive.

Compo
  • 36,585
  • 5
  • 27
  • 39