Computing.Net > Forums > Programming > If Statement Action Based On Volume Label

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

If Statement Action Based On Volume Label

Reply to Message Icon

Name: The Cars Forever (by Chris Leonti)
Date: October 30, 2009 at 11:14:38 Pacific
OS: Windows 7
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 30, 2009 at 12:16:47 Pacific
Reply:

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

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

1

Response Number 2
Name: Mechanix2Go
Date: October 31, 2009 at 03:11:19 Pacific
Reply:

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


-1

Response Number 3
Name: The Cars Forever (by Chris Leonti)
Date: November 2, 2009 at 14:09:59 Pacific
Reply:

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


1

Response Number 4
Name: Mechanix2Go
Date: November 3, 2009 at 01:53:21 Pacific
Reply:

set volume=%%6 & goto cleanup)


That's where you're getting the space.


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

M2


-1

Response Number 5
Name: The Cars Forever (by Chris Leonti)
Date: November 3, 2009 at 05:01:22 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: November 4, 2009 at 02:17:05 Pacific
Reply:

set volume=%%6& goto cleanup)


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

M2


1

Response Number 7
Name: The Cars Forever (by Chris Leonti)
Date: November 4, 2009 at 08:02:41 Pacific
Reply:

Hi M2,

Thanks for the clarity - that makes much more sense.

Chris


0

Response Number 8
Name: Razor2.3
Date: November 4, 2009 at 14:58:27 Pacific
Reply:

Mechanix2Go, someone's after you, it seems.


0

Response Number 9
Name: nbrane
Date: November 5, 2009 at 01:17:26 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: If Statement Action Based On Volume Label

Find Volume number in bat file www.computing.net/answers/programming/find-volume-number-in-bat-file/17713.html

awk syntax error www.computing.net/answers/programming/awk-syntax-error/19611.html

Deleting files based on Date www.computing.net/answers/programming/deleting-files-based-on-date/15215.html