0

I am building a Username and Password function for a batch file, and I have the system to read from 2 text files, Uname and Pass, and find the usernames and passwords, but the system is pulling any password listed in Pass. How can I read the line the username is on, set the password to only accept the corresponding line in the Pass text file?

FOR /F "tokens=* delims=" %%x in (Users.txt) DO IF '%uname%'=='%%x' goto AdmCont
rem FOR /F "tokens=* delims=" %%x in (Pass.txt) DO IF '%code%'=='%%x' goto AdmCont
A-c0rN
  • 33
  • 7
  • You could try using something like awk to find the relevant line, but I am sure there are some better ways – lazyguy Dec 20 '18 at 17:02
  • In batch files the single quote has no special meaning. To just check if the entered `%uname%` is present in the users.txt I'd use `for /f "delims=:" %%A in ('findstr /n "%uname%" user.txt') do set "LineNo=%%A"`. Then use a for /f with skip to start reading pass.txt from that line and compare with the entered `%code%` –  Dec 20 '18 at 17:34
  • 1
    So you are saying that if `Squashman` is on line 52 of the user file, the password is on line 52 of the password file? – Squashman Dec 20 '18 at 17:51
  • @Squashman Yes, Exactly. – A-c0rN Dec 20 '18 at 19:06
  • Possible duplicate of: [how-can-two-text-files-be-read-in-parallel-by-a-batch-file](https://stackoverflow.com/questions/38214874/how-can-two-text-files-be-read-in-parallel-by-a-batch-file) –  Dec 20 '18 at 20:58

1 Answers1

0

Here is a simpler solution:

for /f "delims=: tokens=1,2" %A IN ('findstr /n "%uname%" Users.txt') do ( @set "number_of_line=%A" && @set "_uname=%B" )

Or, using find instead of findstr (quite complicated):

for /f "eol=- delims=[] tokens=1-2" %A IN ('find /n "%uname%" Users.txt') do ( @set "number_of_line=%A" && @set "_uname=%B" )

As in your post you have requested batch file code (you have double percent signs (%%)) you can double the percent-signs of the variables.

Let me explain my code:

In both two loops, we parse output of commands. You should know the command-output-format and why using findstr is much simpler.

findstr

  • We use /n option with findstr which tells it to echo the line that found string specified.
  • After /n option, we specify string that should be searched in specified after file.
  • After that, we specify the file we want to search the specified string.

The output will be:

%line_number%:%content_of_line%

So, we parse it saying:

  • DON'T PARSE : symbol into token with delims=: option!
  • Access the line_number with %(%)A and content_of_line with %(%)B using tokens=1,2 option.
  • Well known: specify variable letter add IN, then command to be parsed and set to variables number of line and its content.

find

  • We use /n option with find which tells it to echo the line that found string specified.
  • After /n option, we specify string that should be searched in specified after file.
  • After that, we specify the file we want to search the specified string.

The syntax is quite similar, but the output is completely different!:

---------- %FILENAME_SPECIFIED_WITH_CAPITAL_LETTERS%
[%line_number%]%line_content%

So, here:

  • We ignore lines starting with - with eol=- option.
  • We don't parse [] symbols into tokens with delims=[].
  • We want to access line_number with %(%)A and line_content with %(%)B, so, we add tokens=1,2 option.
  • Then, continuing as above (well known).

For better understanding how these commands work, I suggest you to open a new cmd window and type:

  • for /?
  • find /?
  • findstr /?

Try some examples of yours with find and finstr to completely understand how they work.

Some interesting references for further reading:

double-beep
  • 5,031
  • 17
  • 33
  • 41