Computing.Net > Forums > Programming > Batch File - variable expansion

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.

Batch File - variable expansion

Reply to Message Icon

Name: markf
Date: March 29, 2006 at 10:14:08 Pacific
OS: 2K/XP
CPU/Ram: Intel PIII 1000 / 768MB
Comment:

Hi all. I know just enough to be dangerous. Having said that, I want to extract just the filename from %0

Set jobname=%0
Set jobname=%jobname:.bat=%
Set jobname=%jobname:%CD%=%

:.bat= works fine in stripping off the extension, but :%CD%= does not work to strip off the path.
My understanding is : expects a value, not a variable, so %CD% does not get expanded. I'd rather not have to hard code the path in place of %CD%. Wondering if anyone knows of a way around this?



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: March 29, 2006 at 11:25:25 Pacific
Reply:

To retain the file name only just code %~n0, any way to put your script to work you need to enable the delayed expansion of variables that allow to code the "variable of variable", so

SetLocal EnableDelayedExpansion

Set jobname=%0
Set jobname=%jobname:.bat=%
Set jobname=!jobname:%CD%=!

EndLocal

where the outer variable has to be marked with ! and the inner one with the conventional %.



0

Response Number 2
Name: markf
Date: March 29, 2006 at 13:05:52 Pacific
Reply:

That's awesome. Works like a charm. I had an intuitive sense that delayedexpansion might have something to do with it, but couldn't quite put it together. I think I understand it now. Thanks very much. Most appreciated!


0

Response Number 3
Name: markf
Date: March 29, 2006 at 13:22:09 Pacific
Reply:

P.S. I will just use %~n0, but it's nice to know how the variable in variable works, anyway.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


VB.NET web browser+filter JAVA collision detection ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File - variable expansion

Batch file variable creation www.computing.net/answers/programming/batch-file-variable-creation/14564.html

batch file variable reset www.computing.net/answers/programming/batch-file-variable-reset/16220.html

Batch file variables www.computing.net/answers/programming/batch-file-variables/16799.html