Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
duke
- Posts: 4
- Joined: 17 Jul 2008 04:16
#1
Post
by duke » 17 Jul 2008 04:33
Hi,
I hope someone can help
I am trying to use the Environment variable substitution method
%PATH:str1=%
this will take the string in the path variable search for "str1" and remove it.
what i need to do is remove a specific string stored in the variable in my example as nextcommand.
i have tried several combinations but using the above example i dont think you can substitute a string variable in place of str1
can someone help. below is the code i am trying to fix
Code: Select all
@echo off
set output=%*
set nextcommand=%1 %2 %3 %4 %5 %6 %7
echo %nextcommand%
set refoutput=%output:%nextcommand=%
echo %refoutput%
What i am trying to do is take a list args from a batch script and take pieces of the batch args in different combination to run other programs. If someone can think of a better way, please advise
May thanks
-
greenfinch
- Posts: 36
- Joined: 17 Jul 2008 07:37
#2
Post
by greenfinch » 17 Jul 2008 07:47
Hi duke,
Don't know if this helps - it runs through all parameters passed to a batch and if any match certain values, does something else. Here it calls another subroutine, but it might just be setting a new variable, then you use that later on.
Code: Select all
FOR %%a in (%*) do (call :procparams %%a)
GOTO next
:procparams
if %1==something (call :something)
if %1==something2 (call :something2)
if %1==something2 (call :something2)
GOTO:EOF
:next
-
duke
- Posts: 4
- Joined: 17 Jul 2008 04:16
#3
Post
by duke » 17 Jul 2008 08:56
Hi Greenfinch,
I have tried something similar Rather than take a substring of %* i tried to build my string using a shift and label to make a loop, but unfortunatly its doesnt work correctly. I seem to face a different problem
I loop through the args adding the argument i require to a string but i am so of my arguments have quotes ie
example.bat arg1 (%0) arg2(%1) agr3(%2) "arg4" (%3)
now if i add %2 and %3
set "command=%2% %3%
that does not work and i get unexpected error.
-
greenfinch
- Posts: 36
- Joined: 17 Jul 2008 07:37
#4
Post
by greenfinch » 17 Jul 2008 09:45
Sorry I'm a bit confused.
If I name that script in your first post 'refoutput.cmd', what do you want to get from:
-
duke
- Posts: 4
- Joined: 17 Jul 2008 04:16
#5
Post
by duke » 17 Jul 2008 10:09
it should take list of args as many as required for example 15
make a string talking all the args (set output=%*)
make a string taking the first 7 args (set nextcommand=%1 %2 %3 %4 %5 %6 %7)
remove the first 7 arguments from the full set of arguments (set refoutput=%output:%nextcommand=%)
output the reminder of the arguments echo %refoutput%
-
greenfinch
- Posts: 36
- Joined: 17 Jul 2008 07:37
#6
Post
by greenfinch » 17 Jul 2008 10:15
Got it. I had wanted to something like this a few weeks ago, and only just worked it out now - so thank you!
Code: Select all
setlocal ENABLEDELAYEDEXPANSION
set output=%*
set nextcommand=%1 %2
set refoutput=!output:%nextcommand% =!
echo %refoutput%
-
duke
- Posts: 4
- Joined: 17 Jul 2008 04:16
#7
Post
by duke » 17 Jul 2008 10:36
Mr greenfinch you are a star!!
I am greatly in your debt!!
That works perfectly!
could you please explain what the
setlocal ENABLEDELAYEDEXPANSION
and the ! ! marks do.i have not come across this before
once again your help is greatly appreciated
-
greenfinch
- Posts: 36
- Joined: 17 Jul 2008 07:37
#8
Post
by greenfinch » 17 Jul 2008 10:55
This explains it quite well:
http://batcheero.blogspot.com/2007/06/h ... nsion.html
Instead of % you use ! to indicate a variable but these are only recognised if you use setlocal ENABLEDELAYEDEXPANSION
In the case of
Code: Select all
set refoutput=!output:%nextcommand% =!
it delays the expansion of !output! until it's expanded %nextcommand%
The space before the =! is just cos there's a space left over after you remove arg1 arg2 etc.
Glad to help,
GF