If Statement Action Based On Volume Label

October 30, 2009 at 11:14:38
Specs: Windows 7
Hello,

I am trying to get a BAT file to perform specific cleanup based on the volume label. I think I am getting close, but can't quite seem to get it 100%. Here is what I am trying so far:

:YesClean
	echo This is where the prior boot cleanup will occur based on volume label
	for /f "tokens=1-5*" %%1 in ('vol') do (set volume=%%6 & goto cleanup)

:cleanup
	if /i %volume% EQU "TEST" goto TEST

:FAILURE
	echo This catches if the volume check has failed
	Goto END

:TEST
	echo This is where actions needed based on volume label would occur

:END
	echo That's All

I keep ending up in the FAILURE section despite the volume name being TEST and showing such if I echo it. Any help would be greatly appreciated.

Chris


See More: If Statement Action Based On Volume Label

Report •


#1
October 30, 2009 at 12:16:47
if /i "%volume%" EQU "TEST" goto TEST

If that doesn't work, keep echo on, and show us the output for the line.

Report •

#2
October 31, 2009 at 03:11:19
You better look at your VOL output. Mine is:
========================
Volume in drive C is 2G_40
Volume Serial Number is 3318-1605
==============================
If yours is similar, you'll probably need:
=====================================
@echo off & setLocal enableDELAYedexpansion

for /f "tokens=1-5* delims= " %%a in ('vol') do (
set v=%%f
goto :done here
)
:done here
echo label is !v!


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

M2


Report •

#3
November 2, 2009 at 14:09:59
Hello,

I can't believe I didn't think to ECHO the info as Razor2.3 noted. What I discovered is that I am getting a space after the volume name that is being captured as a variable. Now that I know that I am just adding a space to the name being looked for in the IF statement and all is working well.

Thanks for the help,

Chris


Report •

Related Solutions

#4
November 3, 2009 at 01:53:21
set volume=%%6 & goto cleanup)


That's where you're getting the space.


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

M2


Report •

#5
November 3, 2009 at 05:01:22
Hi Mechanix2Go,

Thanks for the info, but I am not sure how to remove the extra space it based on your reply. Sorry if I am missing something and adding the space to the volume name being compared works, but I wouldn't argue with learning why it is happening if you are willing to share.

Thanks,

Chris


Report •

#6
November 4, 2009 at 02:17:05
set volume=%%6& goto cleanup)


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

M2


Report •

#7
November 4, 2009 at 08:02:41
Hi M2,

Thanks for the clarity - that makes much more sense.

Chris


Report •

#8
November 4, 2009 at 14:58:27
Mechanix2Go, someone's after you, it seems.

Report •

#9
November 5, 2009 at 01:17:26
the reason might possibly be:
there is one space output by "vol" after the volume label, as you observed. using delims= "and tokens=5* causes the space to be appended to last token since token 5 mops up the balance of the inputline from "vol". removal of asterisk from "tokens=" clause might fix, or set tokens to: 1-6 and put asterisk on number 6.


it's all academic for this issue however.


Report •


Ask Question