Script changed by input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Script changed by input

#1 Post by mor.bas » 22 Jul 2013 00:42

hi,


I need to changed my script like this:


i have a function that doing things and i want it to react like this:


I'm setting in the beginning the variable.\


If it set to null


Set id=null


It's good like it's working now.


If it set to number :


Set id=0,1


The script will run my function for two time each time with other parameters (first time with 0 and second with 1 )


The script could be take input like 0,1,2,3(4 times it will execute )


Thanks in advance....

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script changed by input

#2 Post by penpen » 22 Jul 2013 02:38

mor.bas wrote:hi,
(...) i have a function that doing things (...) The script will run my function (...)
The name of the function were good to know, as it should be called. I assume its name is myFunc.

mor.bas wrote:(...) If it set to null (...) I'm setting in the beginning the variable (....) It's good like it's working now. (...) If it set to number : (...)
I assume the setting of the variable works completely although the "it's working now" is within the full description.

mor.bas wrote:The script will run my function for two time each time with other parameters(first time with 0 and second with 1 )
The script could be take input like 0,1,2,3(4 times it will execute )
I assume this is, what you want to do.

If it is so, this may work:

Code: Select all

for %%a in (%id:;= %) do call :myFunc %%a

penpen

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Script changed by input

#3 Post by mor.bas » 22 Jul 2013 03:14

Hi,

I didnt understand what (%id:;= %) mean?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script changed by input

#4 Post by Squashman » 22 Jul 2013 05:48

Penpen, you don't need to change the delimiter to a space to have the for loop work correctly. It will parse the ID variable just fine with a semicolon or a comma as the delimiter.

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Script changed by input

#5 Post by mor.bas » 22 Jul 2013 06:03

Hi,
It's not working for me
Let's me explain more...

In the first of the script i set it like



SET ID=0,1,2



Then my func should run three time



first time setting a varible that it's used inside to 0



second time setting a varible that it's used inside to 1



third time setting a varible that it's used inside to 2


Thanks in advance...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script changed by input

#6 Post by foxidrive » 22 Jul 2013 06:05

Code: Select all

@echo off
for /L %%a in (1,1,3) do (
echo %%a
)
pause

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Script changed by input

#7 Post by mor.bas » 22 Jul 2013 06:18

Hi,



Two things :



1.I want it to be dynamical according to what i'm writing in the id varible.



It can be :



SET ID=0,1 (to run twice) or SET ID=0 (to run once).



2. In the loop inside I want to set my varible to the id input



It is right to write:



SET FACILITY=%%a ?


Thanks....

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script changed by input

#8 Post by penpen » 22 Jul 2013 06:31

Squashman wrote:Penpen, you don't need to change the delimiter to a space to have the for loop work correctly. It will parse the ID variable just fine with a semicolon or a comma as the delimiter.
Cool 8)

mor.bas wrote:Hi,
It's not working for me
Let's me explain more
...


This example works, but i assumed you have coded all other, a full example is:

Code: Select all

@echo off
setlocal
SET ID=0,1,2
if not "%ID%" == "null" for %%a in (%ID%) do call :myFunc %%a
endlocal
goto :eof

:myFunc
   SET FACILITY=%1
   echo FACILITY=%FACILITY%
   goto :eof

penpen

Edit: added if not "%ID%" == null
Edit2: added edit notes
Edit3 corrected set facility line
Last edited by penpen on 22 Jul 2013 06:38, edited 3 times in total.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Script changed by input

#9 Post by pditty8811 » 22 Jul 2013 09:11

The way to do this would be to ask for the input of sequence of numbers. Put that in a variable.

Then have a for loop (not for /f loop) iterate over each character of that variable because it uses commas as delims.

The last assigned placeholder of the loop would be the highest number of the variable you want to use in yoru function. Then just iterate from 0 up to the highest number, changing the variable by adding one every time, and using the variable within your function until the value is greater than the highest number.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script changed by input

#10 Post by penpen » 22 Jul 2013 10:08

Ah, i didn't understand to get the input from user, so here is a example doing that, too:

Code: Select all

@echo off
setlocal enableDelayedExpansion

set "input="
set /p "input=Please type in a non negative int32 number: "

if defined input (
   set /a "parsedInput=input"
   if not "!input!" == "!parsedInput!" set "parsedInput=null"
   if !parsedInput! LSS 0 set "parsedInput=null"
) else (
   set "parsedInput=null"
)

if "%parsedInput%" == "null" (
   set "ID=null"
) else (
   set "ID="
   for /l %%a in (0,1,%parsedInput%) do set "ID=!ID!,%%a"
   set "ID=!ID:~1!
)

if not "%ID%" == "null" for %%a in (%ID%) do call :myFunc %%a
endlocal
goto :eof

:myFunc
   SET FACILITY=%1
   echo FACILITY=%FACILITY%
   goto :eof

Post Reply