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
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?
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.
Summary: I need to learn how to create a batch file variable of a file or folder name to use several times later in the batch file. I've tried several ways and haven't figured out how to do it. One example tha...
Summary: I have the follwing patch file which calls two other batches: e: cd E:\SQLSERVER\SCRIPTS\INTELEX_LOADS rem get_date.bat studentdata.dat rem mail_bad.bat load_files\unmattched.txt rem and each of the s...
Summary: Is it possible to make a batch file take a key/string from an .ini file to use as a variable? And also how would i get a batch file write a variable to a .txt and a .ini file(not both types at once th...