|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
Variable in section name??I have a batch script with a bunch of groups of tasks to run or kill.
I want to set a variable, and reuse that variable within the respective section to jump the appropriate sub-section. The problem is that the variable doesn't seem to hold when using it in the section name. In the code below, you will see I declare the variable 'groupname' and try to use this in both the sub section declaration and the GOTO's. When I run this, it will try to GOTO the correct section, but the declared section is not found. If I 'hard code' the section names (IISStart, IISKill, IISSection) it works fine. Why can't I use the variable for the section names? Thanks, -Allen ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ :IISSection set groupname=IIS set /p user=%groupname%? [n]: if /i [%user%]==[] endlocal&goto %groupname%End if /i [%user%]==[y] endlocal&goto %groupname%Start if /i [%user%]==[n] endlocal&goto %groupname%End if /i [%user%]==[k] endlocal&goto %groupname%Kill if /i [%user%]==[q] endlocal&goto end set user= goto %groupname%Section :%groupname%Start net start "World Wide Web Publishing" net start "IIS Admin" set user= goto %groupname%End :%groupname%Kill net stop "World Wide Web Publishing" net stop "IIS Admin" set user= goto %groupname%End :%groupname%End |
|
|
Re: Variable in section name??On Mon, 16 Feb 2009 11:55:00 -0000, "umayxa3" <umayxa3@...> wrote:
>I have a batch script with a bunch of groups of tasks to run or kill. >I want to set a variable, and reuse that variable within the >respective section to jump the appropriate sub-section. > >In the code below, you will see I declare the variable 'groupname' and >try to use this in both the sub section declaration and the GOTO's. > >When I run this, it will try to GOTO the correct section, but the >declared section is not found. > >If I 'hard code' the section names (IISStart, IISKill, IISSection) it >works fine. > >Why can't I use the variable for the section names? >:IISSection >set groupname=IIS >set /p user=%groupname%? [n]: > >if /i [%user%]==[] endlocal&goto %groupname%End >if /i [%user%]==[y] endlocal&goto %groupname%Start >if /i [%user%]==[n] endlocal&goto %groupname%End >if /i [%user%]==[k] endlocal&goto %groupname%Kill I would think it is because you have declared the variable inside a setlocal/endlocal pair and not as a global variable. |
|
|
Re: Variable in section name??I had a 'setlocal' in there. I removed it and tried again.
It does the same thing - it can't find the section 'IISStart' when I have: sectionname=IIS goto %sectionname%Start :%sectionname%Start Thanks for any other suggestions. -Allen --- In batchworld@..., foxidrive <foxidrive@...> wrote: > > On Mon, 16 Feb 2009 11:55:00 -0000, "umayxa3" <umayxa3@...> wrote: > > >I have a batch script with a bunch of groups of tasks to run or kill. > >I want to set a variable, and reuse that variable within the > >respective section to jump the appropriate sub-section. > > > >In the code below, you will see I declare the variable 'groupname' and > >try to use this in both the sub section declaration and the GOTO's. > > > >When I run this, it will try to GOTO the correct section, but the > >declared section is not found. > > > >If I 'hard code' the section names (IISStart, IISKill, IISSection) it > >works fine. > > > >Why can't I use the variable for the section names? > > >:IISSection > >set groupname=IIS > >set /p user=%groupname%? [n]: > > > >if /i [%user%]==[] endlocal&goto %groupname%End > >if /i [%user%]==[y] endlocal&goto %groupname%Start > >if /i [%user%]==[n] endlocal&goto %groupname%End > >if /i [%user%]==[k] endlocal&goto %groupname%Kill > > > I would think it is because you have declared the variable inside a > setlocal/endlocal pair and not as a global variable. > |
|
|
Re: Re: Variable in section name??On Tue, 17 Feb 2009 10:20:06 -0000, "umayxa3" <umayxa3@...> wrote:
>I had a 'setlocal' in there. I removed it and tried again. >It does the same thing - it can't find the section 'IISStart' when I >have: > >sectionname=IIS >goto %sectionname%Start >:%sectionname%Start You forgot the set command and I don't really think you want to change the label dynamically, do you?? @echo off set sectionname=IIS goto :%sectionname%Start goto :EOF :abcStart echo abc pause goto :eof :defStart echo def pause goto :eof :IISStart echo IIS pause goto :eof |
|
|
Re: Variable in section name??Sorry. I did use 'set' but didn't type that into the e-mail :)
I do want to dynamically create the label, unless that won't work. Here is a more complete section of the code. Is what I want to do even possible?? Thanks, -Allen REM ------{ START OF SCRIPT }---------------------- @echo off cls c: REM PSTOOLS - www.sysinternals.com Echo === Options [y]es/[n]o/[k]ill [q]uit === Echo Do you want to start: REM ----{ ONE OF THE APPLICATION SECTIONS }-------- :IISSection set groupname=IIS <<== SET THE VARIABLE set /p user=%groupname%? [n]: <<== USE VARIABLE TO PROMPT USER REM ---{ USE VARIABLE TO JUMP TO THE APPROPRIATE SECTION }--- if /i [%user%]==[] endlocal&goto %groupname%End if /i [%user%]==[y] endlocal&goto %groupname%Start if /i [%user%]==[n] endlocal&goto %groupname%End if /i [%user%]==[k] endlocal&goto %groupname%Kill if /i [%user%]==[q] endlocal&goto end set user= REM ---{ USE VARIABLE TO GO TO THE TOP OF THIS SECTION REM IF A SELECTION IS NOT CHOSEN }--- goto %groupname%Section :IISStart <<== THIS WORKS BUT I WANT TO CHANGE TO %groupname%Start net start "World Wide Web Publishing" net start "IIS Admin" set user= goto IISEnd <<== THIS WORKS BUT I WANT TO CHANGE TO %groupname%End :IISKill <<== THIS WORKS BUT I WANT TO CHANGE TO %groupname%Kill net stop "World Wide Web Publishing" net stop "IIS Admin" set user= goto IISEnd <<== THIS WORKS BUT I WANT TO CHANGE TO %groupname%End :IISEnd <<== THIS WORKS BUT I WANT TO CHANGE TO %groupname%End REM ----------------------------------------------- --- In batchworld@..., foxidrive <foxidrive@...> wrote: > > On Tue, 17 Feb 2009 10:20:06 -0000, "umayxa3" <umayxa3@...> wrote: > > >I had a 'setlocal' in there. I removed it and tried again. > >It does the same thing - it can't find the section 'IISStart' when I > >have: > > > >sectionname=IIS > >goto %sectionname%Start > >:%sectionname%Start > > You forgot the set command and I don't really think you want to change the > label dynamically, do you?? > > > @echo off > set sectionname=IIS > goto :%sectionname%Start > goto :EOF > :abcStart > echo abc > pause > goto :eof > :defStart > echo def > pause > goto :eof > :IISStart > echo IIS > pause > goto :eof > |
|
|
Re: Re: Variable in section name??On Tue, 17 Feb 2009 11:13:05 -0000, "umayxa3" <umayxa3@...> wrote:
>I do want to dynamically create the label, unless that won't work. No, you can't. Is there a reason why you want to do that - I couldn't see a reason in your code. |
|
|
Re: Re: Variable in section name??I have a block of this code for each application I want to start/stop/kill. I have about 6 applications now and the list is growing.
It's about 90% the same for each section. I just need to run/kill an application or start/stop a service. So, if I use the declared variable in the section name, it makes it easier to COPY and PASTE, then change the variable (and the specifics for that application) and it runs. Sorry if this is confusing. Is there a better way? Thanks, -Allen --- On Tue, 2/17/09, foxidrive <foxidrive@...> wrote: From: foxidrive <foxidrive@...> Subject: Re: [BATCH WORLD] Re: Variable in section name?? To: batchworld@... Date: Tuesday, February 17, 2009, 6:20 AM On Tue, 17 Feb 2009 11:13:05 -0000, "umayxa3" <umayxa3@yahoo. com> wrote: >I do want to dynamically create the label, unless that won't work. No, you can't. Is there a reason why you want to do that - I couldn't see a reason in your code. [Non-text portions of this message have been removed] |
|
|
Re: Re: Variable in section name??On Tue, 17 Feb 2009 08:38:42 -0800 (PST), Allen May <umayxa3@...>
wrote: >I have a block of this code for each application I want to start/stop/kill. I have about 6 applications now and the list is growing. > >It's about 90% the same for each section. I just need to run/kill an application or start/stop a service. So, if I use the declared variable in the section name, it makes it easier to COPY and PASTE, then change the variable (and the specifics for that application) and it runs. > >Sorry if this is confusing. Is there a better way? Maybe you could populate a set of variables with text depending on each choice, and use the variables in a single block to do what you require. A simple example: if /i "%choice%"=="widget" ( set "a1=this program.exe" set "a2=About to launch the widget application" set "a3=program2.exe" ) if /i "%choice%"=="wagme" ( set "a1=this other executable.exe" set "a2=About to launch the wagme application" set "a3=program4.exe" ) start "" "%a1%" echo %a2% taskkill "%a3%" |
| Free embeddable forum powered by Nabble | Forum Help |