Issue with "findstr" using Batch - windows

Below, I wrote some code. It detects if a Micro-SD card is inserted into the computer and if so, it will ask to enter your pin. After you enter the pin, it will look through the card for a text file that contains a list of pins.
:start
cls
echo.
echo.
if exist E:\ (
goto enterPin
) else (
echo INSERT YOUR CARD
)
timeout 1 >nul
goto start
:enterPin
echo Enter Account Pin: %chip%
set /p pin=": "
:: Finding the specified pin
findstr /m "%pin%" E:\Chip\CardInfo.txt >Nul
if %errorlevel%==0 (
echo Pin "%pin%" is valid
timeout 1 >nul
goto account
)
if %errorlevel%==1 (
echo Pin "%pin%" is invalid
pause
goto start
)
:account
cls
:: Finds the name of the account owner and continues
setlocal enableextensions disabledelayedexpansion
for /F "tokens=2 delims=/" %%a in ('findstr /I "%pin%/" E:\Chip\CardInfo.txt') do set "user=%%a"
for /F "tokens=3 delims=/" %%b in ('findstr /I "%pin%/%user%/" E:\Chip\CardInfo.txt') do set "balance=%%b"
echo.
echo.
echo Welcome, %user%.
echo.
echo ACCOUNT BALANCE: $%balance%
echo.
echo 1=Deposit / 2=Withdraw / 3=Exit / 4=Refresh
choice /c 1234 >nul
if %ERRORLEVEL% EQU 1 goto deposit
if %ERRORLEVEL% EQU 2 goto withdraw
if %ERRORLEVEL% EQU 3 exit
if %ERRORLEVEL% EQU 4 goto account
:deposit
echo.
echo.
set /p add="Money to Deposit: "
set /a moneytoadd=%balance%+%add%
call jrepl "%pin%/%user%/%balance%" "%pin%/%user%/%moneytoadd%" /f E:\Chip\CardInfo.txt /o -
goto account
:withdraw
echo.
echo.
set /p sub="Money to Withdraw: "
set /a moneytosub=%balance%-%sub%
call jrepl "%pin%/%user%/%balance%" "%pin%/%user%/%moneytosub%" /f
E:\Chip\CardInfo.txt /o -
goto account
endlocal
Here's when the issue comes in. A pin consists of 4 numeric characters (ex. 1234), but if there's two pins with the same characters (ex. 1234, 6543), it will say the pin is valid. So for example, if I type 4, it will just look for just the number 4 in the file. And will say the pin is valid. Even though, just the number 4 is not an existing pin. My guess is that it's a flaw with "findstr". But I'm not sure.
Contents of "CardInfo.txt":
1234/Test User/1000
6543/Another Test User/2000

use REGEX (using <StartOfLine><PIN></>):
findstr /m "^%pin%/" E:\Chip\CardInfo.txt >Nul
where ^ is "Start of Line".

Here is what exactly does what you want:
#echo off
::add your path below
for /f %%a in (file.txt) do (
call :funch %%a
)
:funch
set input=%1
set "modifiedinput=%input:~0,4%"
set /p pin=Enter Account Pin:
if %modifiedinput% equ %pin% ( goto authentication_passed) else ( goto authentication_failed)
:authentication_passed
echo auth passed
rem your code
pause >nul
exit
:authentication_failed
echo auth failed
goto funch
it will read the input from file and then extract first four characters which in your case is the pin.

I rewrote the entire batch file to be more fail safe on execution:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem endlocal is executed implicitly by cmd.exe on exiting batch file processing.
:Begin
cls
echo\
echo\
if exist E:\ goto enterPin
echo INSERT YOUR CARD
%SystemRoot%\System32\timeout.exe /T 1 >nul
goto Begin
:enterPin
set "pin="
set /p pin="Enter account pin for %chip%: "
if not defined pin goto enterPin
set "pin=%pin:"=%"
if not defined pin goto enterPin
for /F delims^=01234567890^ eol^= %%I in ("%pin%") do goto enterPin
rem Finding the specified pin.
%SystemRoot%\System32\findstr.exe /B /L /M /C:"%pin%/" E:\Chip\CardInfo.txt >Nul
if errorlevel 1 (
echo Pin "%pin%" is invalid.
pause
goto Begin
)
echo Pin "%pin%" is valid.
%SystemRoot%\System32\timeout.exe /T 1 >nul
:account
cls
rem Finds the name of the account owner and continues
for /F "tokens=2,3 delims=/" %%I in ('%SystemRoot%\System32\findstr.exe /B /L /C:"%pin%/" E:\Chip\CardInfo.txt') do set "user=%%I" & set "balance=%%J"
echo/
echo/
echo Welcome, %user%.
echo/
echo ACCOUNT BALANCE: $%balance%
echo/
echo 1=Deposit / 2=Withdraw / 3=Exit / 4=Refresh
%SystemRoot%\System32\choice.exe /c 1234 >nul
if not errorlevel 1 goto account
if errorlevel 4 goto account
if errorlevel 3 exit /B
if errorlevel 2 goto withdraw
:deposit
echo/
echo/
set "add="
set /P "add=Money to deposit: "
set /A moneytoadd=balance + add
call "%~dp0jrepl.bat" "%pin%/%user%/%balance%" "%pin%/%user%/%moneytoadd%" /L /f E:\Chip\CardInfo.txt /o -
goto account
:withdraw
echo/
echo/
set "sub="
set /P "sub=Money to withdraw: "
set /A moneytosub=balance - sub
call "%dp0jrepl.bat" "%pin%/%user%/%balance%" "%pin%/%user%/%moneytosub%" /L /f E:\Chip\CardInfo.txt /o -
goto account
Issues fixed with this code:
There is the command START. For that reason it is not advisable to use the string start as label although it is possible.
It is advisable to avoid an IF (...) ELSE (...) condition if a simple IF condition with GOTO can be used too.
The usage of full qualified file names wherever possible avoids a batch file not running as expected on environment variables PATH (too often) or PATHEXT (rarely) are corrupted on the userĀ“s machine.
A user has the freedom to enter on a prompt done with set /P really anything from nothing to something resulting in further processing in a syntax error with an immediate exit of batch file execution on code not being prepared for any user input. For that reason the first set /P prompt is improved and validates if the user entered a string consisting only of digits.
The usage of FINDSTR is done with additional options to find the pin only at beginning of a line case-sensitive with a literal search and the next character must be a slash character.
The recommended syntax for ERRORLEVEL evaluation is used in the code.
The arithmetic expressions are written using the recommended syntax to work even if the user enters nothing or a string which cannot be converted to an integer number at all.
The command EXIT is used with option /B to just exit the processing of the batch file and not the entire command process.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
choice /?
cls /?
echo /?
findstr /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
timeout /?
Useful pages regarding to the improvements on code:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Syntax error in one of two almost-identical batch scripts: ")" cannot be processed syntactically here
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
Single line with multiple commands using Windows batch file
Safe number comparison in Windows batch file

Related

Batch: How to scroll through files to read to a variable?

I'm trying to have it scroll through a directory and present a new variable when the user replies "N". I have it all figured out except how to go to the next variable.
cd "C:\Test"
for /r %%F in (*) do SET Show=%%~NF
echo %Show%
echo.
SET /P Continue=Continue?(Y/N)
if /I "%Continue%" EQU "y" goto :Run
if /I "%Continue%" EQU "n" goto :Start
If you're looking to scroll the directory and prompt the user the file name and have them choose to choose it or continue, then bellow should help you.
Firstly, we can use dir /b /a:d to display only directories (folders) in the the current directory. By using a code block ( & ) we can put batch script inside the for loop. For your sake, we can use the CHOICE command to prompt to continue the loop or to save current folder to string and do something with it.
ScrollTreeWithPrompt.bat:
#echo off
setlocal EnableDelayedExpansion
Rem | Configuration
Set "MainDir=C:\Test"
Rem | Get Each Project Folder
for /f "tokens=*" %%A in ('dir "!MainDir!\" /b /a:d') do (
Cls
Echo Current Folder: %%A
echo(
CHOICE /M "Continue?"
Rem | Check for "N" - If so Set String & goto
IF "!ERRORLEVEL!"=="2" (
Set "Choice=%%A"
GOTO Run
)
)
Rem | No Further Results
Cls
Echo Warning: No further folders found.
pause>NUL
goto :EOF
:Run
Cls
echo Currently selected: !MainDir!\!Choice!
pause>NUL
goto :EOF
I have left a few Rem comments in the script to help you along. For any more help on the commands, type the following into a command prompt:
choice /?
set /?
for /?
goto /?
Is this what you need:
For /R "C:\Test" %%A In (*) Do (Choice /M "%%~nA"
If Not ErrorLevel 2 (Set "Show=%%~nA" & GoTo Run))
Exit /B
:Run
Echo Running against %Show%
Pause
Alternatively, should you wish to return to the loop after running against the file name, then use Call instead of GoTo:
For /R "C:\Test" %%A In (*) Do (Choice /M "%%~nA"
If Not ErrorLevel 2 Call :Run "%%~nA")
Exit /B
:Run
Set "Show=%~1"
Echo Running against %Show%
Pause

Batch file how to use findstr command to find a text and make it a variable?

I want to know how to use Findstr command to find a text in a file and make it a variable here what i tried :
#echo off
for /F "delims=" %%a in ('findstr /I /m "100" Config.dat') do set "Variable=%%a"
cls
if %errorlevel% == 0 (
goto found
) else (
goto nope
)
:found
cls
echo founded ! %Variable%
pause
exit
:nope
cls
echo not found!
pause
exit
Ok i explain : In the 2nd line the number "100" is what i want to find and the "Config.dat" is the file that have in it the number 100 and some other numbers and the "Variable" in there is the name of the variable that i want to store in it 100.
The problem is when it founded number 100 it goes to the function "found" and displays "Founded! 100" but when it not founded it also goes to "found" function and only display founded! without 100. So why when it didn't founded it it goes to "found" i need it to go to "nope".
So i hope you guys explain to me if i did something wrong and thanks!
This is because for /F calls command in a standalone cmd.exe process and does not return the error level into context of the caller:
#echo off
rem drop last error level
type nul>nul
for /F "usebackq delims=" %%a in (`cmd.exe /C #exit /b 123`) do rem
echo ERRORLEVEL=%ERRORLEVEL%
-
ERRORLEVEL=0
If you want just load config values into environment variables, then there is no need to search anything. Just create standalone configuration file for that.
config.vars
# loads by cmd.exe script
aaa=111
"bbb=111 222"
/A ccc=1+1
"ddd=%bbb% & 333"
load_config.bat
#echo off
for /F "usebackq eol=# tokens=* delims=" %%i in ("config.vars") do (
call set %%i
)

Tokens created in for loop are not being passed to secondary script

I will start off by saying that I am horribly new at this. Upon researching, I have run into a lot of examples that include far more complex scripts than what I am currently attempting to do.
This script prompts the user to create/delete/cancel a user/group from a file list while calling child scripts. (I am to use at least 3 total scripts with a recommended amount of 5)
The file being read looks similar to this: (Departments occasionally repeating, 3 columns)
--
EMPLOYEES
--
Name employeeID Department
aaaaa AAAAA Alpha
bbbbb BBBBB Beta
ccccc CCCCC Omega
ddddd DDDDD Beta
ScriptA is as follows (Runs Smoothly, the child scripts are my issue - I think)
::This script reads a provided file and creates or deletes users, groups, and folders.
:MENU
CHOICE /C CRX /M "Create or Remove? To cancel, hit X."
REM #ECHO OFF
IF ERRORLEVEL 1 GOTO CREATE
IF ERRORLEVEL 2 GOTO DELETE
IF ERRORLEVEL 3 GOTO END
:CREATE
CALL MakeUsersandGroups.cmd
GOTO END
:DELETE
CALL RemoveUsersandGroups.cmd
:END
ECHO Exiting
PAUSE
Script B MakeUsersandGroups.cmd is as follows
#ECHO OFF
SET /P usrfile="Extract names from which file? "
::Make groups
FOR /F "tokens=1-3 skip=4" %%A IN (%usrfile%) DO GOTO GROUP
:GROUP
CALL mkgroup.cmd %%A %%B %%C
Script B appears to run smoothly, however the %%A %%B %%C does not properly transfer over to the called .bat file. I was taught in class that the tokens convert to numbers %1 %2 %3.
Script C (mkgroup.cmd) is as follows so far
::Create users and groups using columns stated by variables in caller script.
:CHECKGROUP
NET LOCALGROUP %3 | FIND /i "%3" > NUL && GOTO GROUPEXISTS
NET LOCALGROUP %3 /ADD
:GROUPEXISTS
GOTO USER
:USER
NET USER | FIND /i "%2" > NUL && GOTO USEREXISTS
NET USER %1 %2 /ADD
NET LOCALGROUP %3 %2 /ADD
GOTO FOLDER
:USEREXISTS
NET LOCALGROUP %3 | FIND /i "%2" > NUL && GOTO FOLDER
NET LOCALGROUP %3 %2 /ADD
:FOLDER
FIND /i "C:/home/%2" > NUL && GOTO CONTINUE
MKDIR C:/home/%2
:CONTINUE
THIS is where I have encountered errors.
When it runs without ECHO OFF active I get the following in cmd
FOR /F "tokens=1-3 skip=4" %A IN (newhirestest.txt) DO GOTO GROUP
GOTO GROUP
CALL mkgroup.cmd %A %B %C
NET LOCALGROUP | FIND /i "" 1>NUL && GOTO GROUPEXISTS
NET LOCALGROUP /ADD
The syntax of this command is:
NET LOCALGROUP
[groupname [/COMMENT:"text"]] [/DOMAIN]
groupname {/ADD [/COMMENT:"text"] | /DELETE} [/DOMAIN]
groupname name [...] {/ADD | /DELETE} [/DOMAIN]
GOTO USER
NET USER | FIND /i C 1>NUL && GOTO USEREXISTS
FIND: Parameter format not correct
NET USER B C /ADD
the command completed successfully ::no actually, it made users of my variable names
NET LOCALGROUP C /ADD
The specified local group already exists.
GOTO FOLDER
FIND /i "C:home/C" 1>NUL && GOTO CONTINUE
::infinite type lock, had to ^C, Y to exit
It is very obvious to me that my tokens/variables are not transferring correctly, however I can not figure why or how to fix it: based on my notes from class, this has been done correctly.
There are two similar other scripts for removing and they seems to be running without errors.
Any advisory would be greatly appreciated.
Thank you in advance.
The problem is in MakeUsersandGroups.cmd (script B).
For loop variables only exist within the scope of the loop, so when you use a goto to leave the loop, %%A, %%B, and %%C are destroyed and you end up passing nothing to mkgroup.cmd.
You can get around this by sticking the call to mkgroup.cmd in the for loop itself:
#ECHO OFF
SET /P usrfile="Extract names from which file? "
::Make groups
FOR /F "tokens=1-3 skip=4" %%A IN (%usrfile%) DO CALL mkgroup.cmd %%A %%B %%C
Also, in ScriptA
IF ERRORLEVEL 1 GOTO CREATE
IF ERRORLEVEL 2 GOTO DELETE
IF ERRORLEVEL 3 GOTO END
must be reversed.
The if errorlevel syntax is interpreted as "if errorlevel is n or greater than n dothis" so since 3 is normlly greater than 1, the first branch will be taken.
IF ERRORLEVEL 3 GOTO END
IF ERRORLEVEL 2 GOTO DELETE
IF ERRORLEVEL 1 GOTO CREATE
Should work as intended.
I see two problems here.
The way to check the ERRORLEVEL value returned from CHOICE command is wrong. You may entirely avoid the series of IF commands and directly use such an ERRORLEVEL in a GOTO command, that is a simpler solution:
:MENU
:OPTION-255
CHOICE /C CRX /M "Create or Remove? To cancel, hit X."
GOTO OPTION-%ERRORLEVEL%
:OPTION-1 CREATE
CALL MakeUsersandGroups.cmd
GOTO END
:OPTION-2 DELETE
CALL RemoveUsersandGroups.cmd
:OPTION-3 END
:OPTION-0
ECHO Exiting
PAUSE
The other problem is that you are not exporting the %%A %%B %%C FOR parameters in the same FOR context, because the GOTO GROUP command cancel the FOR. You must change it by a CALL GROUP command instead. After that, you may use a simple trick in order to get the tokens of the active FOR command: just active another FOR with any (unused) token:
FOR /F "tokens=1-3 skip=4" %%A IN (%usrfile%) DO CALL GROUP
rem Other code here
GOTO :EOF
:GROUP
FOR %%a IN (x) DO CALL mkgroup.cmd %%A %%B %%C

How to make bat file listing with options to choose

I want to make a bat file that list all of the files in a specific directory, and add numbers at the beginning of the every one of the listed items. This numbers need to be a selectable options.
Example:
I have a folder with 5 files in it, aaa.exe, bbb.exe, ccc.exe, ddd.exe, eee.exe. When i run bat file i need to see
aaa.exe
bbb.exe
ccc.exe
ddd.exe
eee.exe
So now if i wana run 5-th exe i need to press 5, than press enter and that 5th exe will now start.
I allredy find how to list all of the items in folder with this code
REM -start "c:\windows\system32" notepad.exe
for /r %%i in (*) do echo %%i
pause
exit
but i can't figure out how to add numbers in front of the text and make that numbers to be a selectable options.
Edit---
Now im getting
ERROR: Duplicate choices are not allowed. running '""' is not
recognized as an internal or external command, operable program or
batch file.
when i'm trying to run this loop for a second time.
This is code that i wrote:
#ECHO OFF
setlocal enabledelayedexpansion
REM ---Prompt part
:choise
SET /P AREYOUSURE=Install programs (Y/[N])?
IF /I "%AREYOUSURE%" EQU "Y" GOTO :chooseInstall
IF /I "%AREYOUSURE%" EQU "N" GOTO :nope
REM --Cheking for Y or N
GOTO :choise
:nope
echo "Ok. Have a nice daty / night"
pause
exit
:chooseInstall
echo Wich program do you wana install ?
echo.
echo 1. 7Zip
echo 2. CPU Z
echo.
SET /P AREYOUSURE=Choosing:
IF /I "%AREYOUSURE%" EQU "1" set "pathToSoft=C:\Users\usr\Desktop\hello"
IF /I "%AREYOUSURE%" EQU "2" set "pathToSoft=C:\Users\usr\Desktop\bye"
echo.
echo.
echo %pathToSoft%
echo.
echo.
REM ---Installs
echo "Wich file to install"
cd %pathToSoft%
echo.
echo.
REM --Loops that scan files
set /A counter=0
for /R %%i in (*) do (
if not "%%~nxi" == "%~nx0" (
set /A counter+=1
echo !counter!: %%~nxi
set exe[!counter!]=%%i
set choice=!choice!!counter!
)
)
if %counter% LSS 10 (
choice /C %choice% /M "Choose: "
set EXENUM=!ERRORLEVEL!
) else set /P EXENUM="enter exe number: "
set EXECUTABLE=!exe[%EXENUM%]!
echo running %EXECUTABLE%
call "%EXECUTABLE%"
echo.
echo.
echo.
:installmore
SET /P INSTALLMORE=Do you wana install somthing else (Y/[N])?
IF /I "%INSTALLMORE%" EQU "Y" GOTO :chooseInstall
IF /I "%INSTALLMORE%" EQU "N" GOTO :nope
count the executables and associate them with the counter, creating kind of "array" variables (filter out the current batch script)
build the choice list at the same time
after the loop, use choice if no more than 9 choices, else use a classical interactive set
retrieve the user selection and call the executable/batch file
(you have to enable delayedexpansion to be able to use % and ! env. var separators & instant evaluation within the loop)
can be done like this:
#echo off
setlocal enabledelayedexpansion
set /A counter=0
set choice=
for /R %%i in (*) do (
if not "%%~nxi" == "%~nx0" (
set /A counter+=1
echo !counter!: %%~nxi
set exe[!counter!]=%%i
set choice=!choice!!counter!
)
)
if %counter% LSS 10 (
choice /C %choice% /M "type exe number"
set EXENUM=!ERRORLEVEL!
) else set /P EXENUM="enter exe number: "
set EXECUTABLE=!exe[%EXENUM%]!
echo running %EXECUTABLE%
call "%EXECUTABLE%"

Running two command prompt terminals in native windows, how can I redirect the output of one to the input of the other?

I have one Windows 10 command prompt running and awaiting input, and I wish to automate continuous and live input with a second command prompt. I have gotten the second command prompt to extract the desired variable, and I wish to send it to the other command prompt that is waiting for input.
The "awaiting input" command prompt must run in real time because it is connected to Plink (not an SSH session so no use of the -m command here) which is connecting to a microcontroller. So it cannot be accomplished (at least I don't think) with function calls.
I see that it can be done in UNIX environments: https://askubuntu.com/questions/496914/write-command-in-one-terminal-see-result-on-other-one
Thanks in advance and please advise,
--A hopeful beginner
Batch code starts 2 piped processes, one for getting keyboard input and writing to a file, and the other reading the data written. Note there isn't a cmd window for each process, but there are two new processes running. You may use cmd /c or start if you need two consoles.
Does it help?
#echo off
set "pipefile=pipefile.txt"
if "%~1" neq "" goto %1
copy nul "%pipefile%" >nul
"%~F0" getInput >>"%pipefile%" | "%~F0" readInput <"%pipefile%"
echo(
echo(Batch end...
ping localhost -n 1 >nul
del /F /Q "%pipefile%" 2>nul
exit/B
:getInput
set "input="
set/P "input="<CON
echo(%input%
if /I "%input%" equ "EXIT" exit
goto:getInput
:readInput
setlocal enableDelayedExpansion
set/P="enter some data [EXIT to exit] "
for /l %%. in () do (
set "input="
set/P "input="
if defined input (
if /I "!input!" equ "EXIT" exit
set/P="enter some data [EXIT to exit] "
)
ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)
Running two console windows
#echo off
set "pipefile=pipefile.txt"
if "%~1" neq "" goto %1
del /F /Q "%pipefile%" 2>nul
copy nul "%pipefile%" >nul
start "writer" cmd /c ^""%~f0" readInput ^<"%pipefile%" ^"
start "reader" cmd /c ^""%~f0" getInput ^"
echo(
Echo batch end...
ping localhost -n 1 >nul
goto :EOF
:getInput
set "input="
set/P "input=enter some data [EXIT to exit] "
echo(%input%>>"%pipefile%"
if /I "%input%" equ "EXIT" exit
goto:getInput
:readInput
setlocal enableDelayedExpansion
for /l %%. in () do (
set "input="
set /p "input="
if defined input (
if /I "!input!" equ "EXIT" exit
echo(!input!
)
ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)

Resources