Read a variable from a text file - windows

I'm trying to create a password prompt which compares the user input to information in a text file (the password is saved in the .txt file).
I've tried to work with information provided to me through the command prompt and and this website but I just can't get it to work, probably because i don't have sufficient experience as I'm rather new to advanced batch coding.
This is what I've come up with so far, the name of the text file is Q47.txt and in it is just the word "hello" until I can get this to work:
#echo off
:a
cls
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (Q47.txt) do set content=!content! %%i
echo %content%
EndLocal
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" (
goto begin
)
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul
Please can you tell me where I've gone wrong.
I'd also like to know how to eliminate the space before the variable.

You might try this:
#ECHO OFF &SETLOCAL
:a
set "content="
for /F "tokens=*" %%i in (Q47.txt) do set "content=%%i"
echo "%content%"
echo Enter password to continue:
set /p "VAR=>"
if "%VAR%" == "%content%" goto begin
echo Incorrect, please try again.
pause >nul
goto a
:begin
cls
echo Welcome
pause >nul

Read the file into a variable like this:
set /p content=<"C:\path\to\Q47.txt"

Related

How to make batch read certain parts of text and convert it into a variable

so I'm just wondering if it's possible to to make a batch file read a line of text but split every letter up into it's own variable, ex:
#echo off
:start
set /p text=input:
set out=
set out2=
set /p text=input:
:loop
set out=%out% %text:~0,1%
set out2=%out2:~1% %text:~1%
set text=%text:~1%
if defined text goto loop
echo %out% -%out2%
pause
goto start
What I've written here doesn't work (I was just fiddling around trying to find the answer)
But what I was trying to do was to make "out" and "out2" into 2 separate values. Where "out" would be the first letter typed, and "out2" would be the second letter and so on. (planning to have about 16 out's that can read the first 16 letters of whatever the user inputs and make it into separate varibles)
ex: typing "ab" in the same line would result in "out" being "a" and "out2" being b
Another thing I couldn't figure out either was how to stop "out" from reading everything after the first letter. If anyone could help me with this issue, please explain what you've done to fix it. Thanks in advance
Here is a little trick you can use with CMD.exe and the /U option. The FOR /F command is necessary to capture the output to assign to a variable. I then build a pseudo array with the cnt variable. The SET out command is just used to display all the variables in the pseudo array.
#echo off
setlocal
SET /P "text=INPUT:"
set "cnt=0"
for /F "delims=" %%G IN ('cmd /u /c "echo %text%"^|find /V ""') do (
set /A cnt+=1
CALL SET "out%%cnt%%=%%G"
)
FOR /L %%G IN (1,1,%cnt%) DO CALL echo %%out%%G%%
endlocal
pause
And here is just a quick run of the code.
C:\Users\Squashman\Desktop>so.bat
INPUT:foobar
out1=f
out2=o
out3=o
out4=b
out5=a
out6=r
Press any key to continue . . .
Here is the answer you provided but I fixed your logic errors.
#echo off
setlocal ENABLEDELAYEDEXPANSION
title Test
color a
mode 150
:start
cls
echo test
echo.
set /p text=Input:
set "texttmp=%text%"
set cnt=0
:Reader1
set /a cnt+=1
echo Val-%cnt% = !texttmp:~0,1!
set c[%cnt%]=!texttmp:~0,1!
set "texttmp=%texttmp:~1%"
if "%texttmp%" NEQ "" goto Reader1
I found an easy solution which is way easier to understand (for me at least)
#echo off
setlocal ENABLEDELAYEDEXPANSION
title Test
color a
mode 150
:start
cls
echo test
echo.
set /p text=Input:
set pos=1
:Reader1
echo Val-%pos% = !text:~%pos%,1!
set c[%pos%]=!text:~%pos%,1!
set /a pos=%pos%+1
if "!text:~%pos%,1!" NEQ "" goto Reader1
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
)

How to remove results from batch command

I am sort of new to coding, and tried looking around. My goal is to remotely obtain Domain Controller from a machine on the network. The command I am using via batch is below. I want to filter the results from nltest to only display the DC Name. I don't want connection status, and flags.
:Start
color 02
cls
#echo off
Echo.
Echo Domain Controller Finder
Echo.
Set /P Computer=Enter the Asset ID:
If "%Computer%"== "" goto BadName
nltest /sc_query:<Domain_Name> /server:%Computer%
pause
Goto End
:BadName
Cls
Echo.
Echo You have entered an incorrect name or left this field blank
Echo Please enter a valid Name or press Ctr-C to exit.
Echo.
Pause
Goto Start
:End
I'm guessing that the DC Name begins with \\, so you could try this:
#ECHO OFF
:Begin
SET/P "AssetID=Enter the Asset ID: "
IF "%AssetID%"=="" GOTO BadName
SET "DCName="
FOR /F "TOKENS=1* DELIMS=\" %%A IN (
'nlest /sc_query:<Domain_Name> /server:%AssetID%^|FIND "\\"'
) DO SET "DCName=\\%%B"
IF DEFINED DCName (ECHO %%DCName%% = %DCName%
PAUSE & GOTO End)
:BadName
Note. I changed :Start to :Begin because Start is a command and looks wrong, the decision is yours.

How should I Create a Batch Menu based on List of Output

I think I may struggle to explain the how I want my batch script to behave so here goes. My script is meant to act as a menu system requiring user input. What I have managed so far is to send a list of datestamps to output to a user. However I am attempting to arrange the script so any timestamp can be selected such as 1,2,3 and based on a prompt for user input. Later I want to save that to a variable in which I can do another operation with afterwards.
So this is what an example of what I can see:
FOR /f "tokens=3 delims= " %%G IN ('%history% ^| findstr /C:"Start Time:"') DO (call :subroutine %%G)
:subroutine
echo %1
GOTO :EOF
My output now looks like this:
20150706232400
20150706232707
20150706232757
20150706233058
20150706233144
Any pointers appreciated. Thanks.
#echo off
setlocal EnableDelayedExpansion
rem Show menu and store options in an array
echo Available datestamps:
echo/
set i=0
FOR /f "tokens=3" %%G IN ('%history% ^| findstr /C:"Start Time:"') DO (
set /A i+=1
echo !i!. %%G
set "option[!i!]=%%G"
)
echo/
:getChoice
set /P "choice=Enter desired option: "
if "!option[%choice%]!" equ "" echo ERROR: no such option & goto getChoice
set "variable=!option[%choice%]!"
ECHO/
ECHO Selected datestamp: %variable%

Find and Replace a batch file using DOS

This is what i need.
Lets say there is a file temp.txt
Lets say temp.txt has the following content :
name=#name
age=#age
email=#email
And my batch file create.bat should ask for the three parameters as input from the user,
and then replace the temp.txt with the respective values.
Say.
Pls enter your name:Tom
Tom - Confirm Y/N: Y
<<now anywhere the temp.txt says #name, it is replace by Tom>>
Please help me write a script for this??
Thanks,
Naveen.
rem #echo off
setlocal enabledelayedexpansion enableextensions
rem Don't use the same file name for both here
set InputFile=temp.txt
set OutputFile=temp2.txt
call :ask name name
call :ask age age
call :ask "e-Mail address" email
del %OutputFile% 1>nul 2>&1
for /f "delims=" %%l in (%InputFile%) do (
set Line=%%l
set Line=!Line:#name=%name%!
set Line=!Line:#age=%age%!
set Line=!Line:#email=%email%!
>>%OutputFile% echo.!Line!
)
goto :eof
rem :ask "placeholder title" variable_name
:ask
set /p %2=Please enter your %~1:
choice /M "!%2! - Confirm"
if errorlevel 2 goto ask
goto :eof

Resources