Page 1 of 1

Help with if / loop and variables

Posted: 30 Dec 2022 19:53
by ValComputers
So I am trying to figure out how to take a total set of variables being read from a file say for example

1
2
3
4
5

set to variables and done with a count so each one would be a(1) a(2) a(3) etc etc and the count variable would be however many lines in the file in my example count=5 since there are 5 lines

what I am trying to do is ask a user for an input after the file is read and variables are created and count is set to ask for their input

so say they input 4 I want the script to then go thru each variable a(1-5) using count as the largest variable to determine in what they entered matches any of the variables from the file

in this case a(4) would = 4

Can anyone help or give me an idea or the steps I would need to take to compare the variable loop to the user input

Thank you so much

Re: Help with if / loop and variables

Posted: 31 Dec 2022 10:09
by aGerman
Try to use a FOR /F loop along with FINDSTR /N.

Steffen

Code: Select all

@echo off &setlocal

:: read the file using FINDSTR /N that outputs the lines prefixed with line number and colon
:: preconditions: the file doesn't contain any blank lines, lines don't begin with a colon
:: the output is tokenized using the colon as separator
:: %%i contains the current line number, %%j the current content of the line
:: finally you get variable cnt containing the last assigned line number,
:: and the associative array a(1)..a(cnt) containing the file content
for /f "tokens=1* delims=:" %%i in ('findstr /n . "test.txt"') do set /a "cnt=%%i" &set "a(%%i)=%%j"

:: run SET /P in a loop until the user entered valid input
:input_loop
set "num="
set /p "num=Enter a number (1..%cnt%): "
call :check num 1 %cnt%
if errorlevel 1 goto input_loop

:: output the chosen line
setlocal EnableDelayedExpansion
echo !a(%num%)!
endlocal

pause
goto :eof

:check VarName Min Max
setlocal EnableDelayedExpansion
:: Only digits allowed.
for /f "delims=1234567890 eol=" %%i in ("!%~1!") do (endlocal &exit /b 1)
:: Valid number (everything where SET /A doesn't fail).
2>nul set /a "dummy=!%~1!" || (endlocal &exit /b 1)
:: Decimal integer (no octal number with a preceding 0).
if "!%~1!" neq "%dummy%" (endlocal &exit /b 1)
:: Minimum check
if %dummy% lss %~2 (endlocal &exit /b 1)
:: Maximum check
if %dummy% gtr %~3 (endlocal &exit /b 1)
endlocal &exit /b 0

Re: Help with if / loop and variables

Posted: 31 Dec 2022 21:36
by ValComputers
I appreciate the help but I think I wasn't clear what I was trying to accomplish is to add the lines from the file ( which is in the script you added )
Total the lines ( counted - in your script )
but then I need to ask the user for an input and once they press enter

I need to loop thru the results from the first step to test for the users input and if it matches any line of the file ( variables ) then tell it to continue if not then end or ask for a correct matching input

Basically asking the user for an input that is then verified against the lines in the text file ... searching thru ( X ) total lines of variables for that answer

Thank you for your code and everyone's assistance

Re: Help with if / loop and variables

Posted: 31 Dec 2022 23:19
by ValComputers
I need to compare the text variable to the user input and if it matches continue the script if not end the bat or ask again for user input

Thanks for helping with your scripting ... I truly appreciate it

Re: Help with if / loop and variables

Posted: 01 Jan 2023 06:14
by aGerman
Why is it necessary to read the whole file into an associative array then? Directly use the user input along with FIND or FINDSTR in order to check whether it occurs in the file.

Steffen

Re: Help with if / loop and variables

Posted: 02 Jan 2023 08:49
by miskox
I agree with aGerman: user enters the data and all you have to do is use FIND or FINDSTR and check for ERRORLEVEL (or && / ||).

This is what you get for not providing all the information in the first place - solution to your problem might be completely different from your first approach.

Saso

Re: Help with if / loop and variables

Posted: 03 Jan 2023 18:51
by ValComputers
you both are probably right sorry I am a total Noobie at this and just trying to work things out ... I was hoping I guess to have all the options in a different variable I think in case I needed them for another part of script

I am trying to create this as sort of a password script for users so that they need to have a password in the TXT file provided in order to work

Was thinking I could download a TXT file read it into variables then delete it so it does not remain on the system

Maybe there is an easier way ? Just want to keep track of who is using the script ETC ETC

Thanks again All :)

Re: Help with if / loop and variables

Posted: 03 Jan 2023 22:20
by Aacini
If you can't explain with words what you want, then show it with data...

For example, put: "Giving this data file" and post an example file, "And this user input" and show us the user input, "I want this as output", accordingly... If the process is somewhat complex, show us several input and desired output examples.

This have the advantage that we have some example data to work with...

Antonio

Re: Help with if / loop and variables

Posted: 29 Jan 2023 18:43
by ValComputers
So to revisit this

I want to be able to download a txt file ( that will be downloaded and hidden in a hidden local folder on the PC )

Then ask the user to input a variable say password = ???

Then count the lines of possibles in the txt file and then use that total to test the input against each answer in the txt file

Example

Txt file has contents such as

123456
321123
654321
987456
987654
456789
789456
etc
etc
etc
( and can have more or less lines in the file )

The TXT file is downloaded from somewhere into a local hidden file

then the file contents are put into variables

A (1)
A (2)
A (3)
ETC ETC ETC

the last one say
A (10) is used to determine the number of entries which would be 10

then the user is asked for an entry ( password ? )

at that point the input is compared against the variables ( one at a time ) to test if there is a match

If not the script prompts again say 2-3 times max before it quits

and if it matches then the script continues to allow access and do something

Any help ? or guidance would be appreciated

Thank you so much

Re: Help with if / loop and variables

Posted: 30 Jan 2023 14:58
by Aacini
Ok. Let's revisit what I understand from your question:

- You have a .txt file. I don't matters where and how you have such a file (if it was downloaded from the web or if it is in a hidden folder, etc.) because such details have not any influence on the solution.

- The example data file is this:

Code: Select all

123456
321123
654321
987456
987654
456789
789456
- "Then the file contents are put into variables". For example:

Code: Select all

A[1]=123456
A[2]=321123
A[3]=654321
A[4]=987456
A[5]=987654
A[6]=456789
A[7]=789456
I changed the (round) parentheses by (square) braquets because this is the standard way to manage array elements.

"the last one is used to determine the number of entries, for example: entries=7.

Ok. For the rest of requirements, I guess this code fulfills them:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem the file contents are put into variables
rem the last one is used to determine the number of entries
set "entries=0"
for /F %%a in (theFile.txt) do (
   set /A entries+=1
   set "A[!entries!]=%%a"
)

set "match="
set "times=0"
:nextTime

rem then the user is asked for an entry ( password ? )
set /P "password=Enter password: "

rem at that point the input is compared against the variables ( one at a time ) to test if there is a match
for /L %%i in (1,1,%entries%) do (
   if "%password%" equ "!A[%%i]!" set "match=%%i"
)

rem if it matches then the script continues to allow access and do something
if defined match goto doSomething

rem If not the script prompts again say 2-3 times max before it quits
set /A times+=1
if %times% lss 4 goto nextTime

echo Invalid password
goto :EOF


:doSomething
echo Access granted...
Hope it helps...

Antonio