Page 1 of 1
Script changed by input
Posted: 22 Jul 2013 00:42
by mor.bas
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....
Re: Script changed by input
Posted: 22 Jul 2013 02:38
by penpen
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
Re: Script changed by input
Posted: 22 Jul 2013 03:14
by mor.bas
Hi,
I didnt understand what (%id:;= %) mean?
Re: Script changed by input
Posted: 22 Jul 2013 05:48
by Squashman
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.
Re: Script changed by input
Posted: 22 Jul 2013 06:03
by mor.bas
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...
Re: Script changed by input
Posted: 22 Jul 2013 06:05
by foxidrive
Code: Select all
@echo off
for /L %%a in (1,1,3) do (
echo %%a
)
pause
Re: Script changed by input
Posted: 22 Jul 2013 06:18
by mor.bas
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....
Re: Script changed by input
Posted: 22 Jul 2013 06:31
by penpen
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
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
Re: Script changed by input
Posted: 22 Jul 2013 09:11
by pditty8811
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.
Re: Script changed by input
Posted: 22 Jul 2013 10:08
by penpen
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