Iterate volume letters to find ID

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
wellard
Posts: 3
Joined: 27 Nov 2014 04:49

Iterate volume letters to find ID

#1 Post by wellard » 27 Nov 2014 05:05

G'Day,

I have ugly code which does what i need: takes letter by letter (Z --> C) in system and check the volume ID ("XXXX-XXXX"). Once correct volume is found, letter is assigned a variable (tdrive) and script proceeds.

Here is a code:

Code: Select all

vol z: | find "XXXX-XXXX" >nul
if not errorlevel 1 (
set "tdrive=z:"
goto start
)

vol y: | find "XXXX-XXXX" >nul
if not errorlevel 1 (
set "tdrive=y:"
goto start
)

....

vol c: | find "XXXX-XXXX" >nul
if not errorlevel 1 (
set "tdrive=c:"
goto start
)

cls
echo.
echo.
echo target drive NOT found,
echo.
echo operation aborted.
pause >nul
goto :eof

:start
...


In what way this code can be changed in order to preserve functionality, but occupy less place and become more professional?
(some kind of iteration...?)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Iterate volume letters to find ID

#2 Post by foxidrive » 27 Nov 2014 08:06

Code: Select all

@echo off
set "tdrive="
for %%a in (z y x) do vol %%a: |find "blah" >nul && set tdrive=%%a:
echo tdrive is either "%tdrive%" or it's nul
pause

wellard
Posts: 3
Joined: 27 Nov 2014 04:49

Re: Iterate volume letters to find ID

#3 Post by wellard » 27 Nov 2014 09:17

Ok, thanks a lot!

Currently code is following:

Code: Select all

@echo off
set "tdrive="
for %%a 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 b a) do vol %%a: |find "blah" >nul && set tdrive=%%a:
if not defined tdrive echo drive not found & pause >nul & goto :eof
pause


Is it possible to replace (z y x w v u t s r q p o n m l k j i h g f e d c b a) part with regular expression (to go through every letter in alphabet)?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Iterate volume letters to find ID

#4 Post by Yury » 27 Nov 2014 10:19

wellard wrote:Is it possible to replace (z y x w v u t s r q p o n m l k j i h g f e d c b a) part with regular expression (to go through every letter in alphabet)?



Code: Select all

for /l %%# in (90 -1 65) do cmd /c exit /b %%#& 2>nul call vol %%=ExitCodeAscii%%:|>nul find "blah"&& call set tdrive=%%=ExitCodeAscii%%:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Iterate volume letters to find ID

#5 Post by foxidrive » 27 Nov 2014 13:08

You win, Yury. :D

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Iterate volume letters to find ID

#6 Post by Squashman » 27 Nov 2014 13:25

I still vote for your code Foxidrive because it is more readable. No offense to you Yury. It is great code but a newbie to batch isn't going to understand what your code is doing.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Iterate volume letters to find ID

#7 Post by foxidrive » 27 Nov 2014 16:27

Squashman wrote:I still vote for your code Foxidrive because it is more readable. No offense to you Yury. It is great code but a newbie to batch isn't going to understand what your code is doing.


I know what you mean Squashman, Yury provided what the OP asked for though, which is convoluted and obfuscated. :)

I get the impression that the OP has some other motive for asking the questions...

wellard
Posts: 3
Joined: 27 Nov 2014 04:49

Re: Iterate volume letters to find ID

#8 Post by wellard » 28 Nov 2014 01:38

Yury wrote:

Code: Select all

for /l %%# in (90 -1 65) do cmd /c exit /b %%#& 2>nul call vol %%=ExitCodeAscii%%:|>nul find "blah"&& call set tdrive=%%=ExitCodeAscii%%:

Thanks a lot!

Post Reply