Drive serial Number

April 16, 2010 at 13:07:48
Specs: Windows 2008 or XP, i7 16GB
Hello,
I have the following to extract a drive letter of a drive with the name backup.

@echo off & setLocal EnableDELAYedeXpansion & set S_D_Drive2=
for %%a in (c d e f g h) do (
vol %%a: 2> nul | find /I "BACKUP" >nul
if not errorlevel 1 set S_D_Drive2=%%a)
if defined S_D_Drive2 (echo drive letter is !S_D_Drive2!) else (goto NO_BACKUP_FOUND)

which works great, but I would also like to extract the Volume Serial Number which is in line 2
ie:
Volume in drive E is BACKUP
Volume Serial Number is 9853-87D9

So the output will look like this
echo %S_D_Drive2%
E

echo %SERIAL_NUMBER%
9853-87D9

Any ideas?
running winXP sp3


See More: Drive serial Number

Report •

#1
April 16, 2010 at 16:17:00
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=5 delims= " %%a in ('vol') do (
set S=%%a
)
echo !S!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
April 16, 2010 at 16:33:44
Thanks, it worked like a charm!
I forgot all about taking out the 5th token.
Thanks again!

Report •

#3
April 17, 2010 at 13:27:20
Well not so fast... I have a question; why does this line:
for /f "tokens=5 delims= " %%a in ('vol') do (
set S=%%a
)
echo !S!
take the second line from the dos command vol, and not the first?
Also How would I be able to extract a word from the first line, second, third?
Thank you.

Report •

Related Solutions

#4
April 17, 2010 at 14:14:00
It sets it to the 5th token in line 1, then line 2.

If you want to use the first line of a multi-line command output:


=========================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=5 delims= " %%a in ('vol') do (
set S=%%a
goto :done
)
:done
echo !S!

============================
To use line 8:
============================
@echo off & setLocal EnableDELAYedeXpansion

for /f "skip=7 tokens=* delims= " %%a in ('dir/b/a-d') do (
set S=%%a
goto :done
)
:done
echo !S!


=====================================
Helping others achieve escape felicity

M2


Report •

#5
April 17, 2010 at 16:53:49
Thank you for the answer, but at the expense of sounding really dense, I find that when I do:
for /f "tokens=2 delims= " %%a in ('vol') do (
set S=%%a
)
echo !S!
I would expect to see the following;
In
Serial
But instead I only get :
Serial
This is what has me so confused, without the skip parsing option, I would expect to see the second token of both lines, but do not. I don’t know why that is so.
Thanks in advance, I hope you can enlighten me.

Report •

#6
April 17, 2010 at 17:09:20
I think I figured it out, correct me if I’m wrong, but the reason I only see the second line is the variable is set to the token in the first line, then the program goes on and that same variable is set to the token in the second line, which is displayed. Am I right?
Thanks!

Report •

#7
April 17, 2010 at 17:36:07
yep, you got it


=====================================
Helping others achieve escape felicity

M2


Report •

#8
April 17, 2010 at 18:26:54
Thanks again for your help

Report •

Ask Question