Page 1 of 1

Iterate volume letters to find ID

Posted: 27 Nov 2014 05:05
by wellard
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...?)

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 08:06
by foxidrive

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

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 09:17
by wellard
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)?

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 10:19
by Yury
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%%:

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 13:08
by foxidrive
You win, Yury. :D

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 13:25
by Squashman
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.

Re: Iterate volume letters to find ID

Posted: 27 Nov 2014 16:27
by foxidrive
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...

Re: Iterate volume letters to find ID

Posted: 28 Nov 2014 01:38
by wellard
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!