3

I have a text file, that has some text with this syntax:

mytextfile.txt:

websiteurl1 username1 password1  
websiteurl2 username2 password2                                       
websiteurl3 username3 password3

And so on....

And I'd like to be able to find username and password strings by pointing the websiteurl, so let's say I tell the batch file, find websiteurl3, it should print the username3 and password3 and I was able to write a "FOR LOOP" but I am not sure how to use the syntax of the loop, as my code finds only the last line always, here is what I have:

FOR /F "tokens=2,3 delims= " %%A IN (URL.txt) DO IF EXIST URL.txt (set WEBUserName1=%%A) && (SET WEBUserPass1=%%B)  
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit

I know the loop is looking on the "URL.txt" for the tokens 2 and 3, and then sets the variables accordingly, what I'd like to know, is how I can use the loop, or if needed, any other command to be able to:

  • Find the "URL.txt file
  • Then find the specific string, in this case, the first word of the specified string line
  • Then find tokens 2 and 3 of that string line.
DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Wolf
  • 115
  • 3
  • 13

4 Answers4

1

And I'd like to be able to find username and password strings

Use the following batch file.

test.cmd:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3" %%a in ('type mytextfile.txt ^| findstr "%1"') do (
  echo Username:"%%a"
  echo Password:"%%b"
  pause
  exit
  )
endlocal

Notes:

  • Pass the website URL string as a parameter to the batch file.
  • findstr is used to find the matching line from the file, so we only need to parse a single line using for /f.

Example usage and output:

F:\test>type mytextfile.txt
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3

F:\test>test websiteurl3
Username:"username3"
Password:"password3"
Press any key to continue . . .

Further Reading

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
0

You get the last line, because you process the whole file. Filter your textfile and process only that one line:

set "site=websiteurl2"
FOR /F "tokens=2,3 delims= " %%A IN ('find "%site%" URL.txt') DO (
  set "usr=%%A" 
  set "pwd=%%B"
)
echo %usr%, %pwd%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

bad way, but this too work:

@echo off
setlocal
set srch_site=websiteurl2
FOR /F "tokens=1,2,3 delims= " %%A IN (URL.txt) DO (
    IF EXIST URL.txt (
        if "%%A"=="%srch_site%" (
            (set WEBUserName1=%%B) && (SET WEBUserPass1=%%C)&goto:stdout
        )
    )
)
:stdout
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
mir16
  • 61
  • 4
  • The `IF EXIST URL.txt (` line have no sense in this place. If the file does not exist, an error is issued in the _previous_ `FOR /F` command. If the file does exist, the `IF EXIST` command is executed _with every line_ in the file... – Aacini Aug 13 '16 at 14:41
  • @Aacini, of course ;)) i just copypast and then edit example of the code from the question.. with least changes – mir16 Aug 13 '16 at 20:29
0

May I offer you a different approach?

The code below get all usernames and passwords and store they in two vectors:

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-3" %%A in (URL.txt) do set "User[%%A]=%%B" & set "Pass[%%A]=%%C"

After that, you may test if the username and password of a certain site was given this way:

set "site=websiteurl3"

if defined User[%site%] echo The site "%site%" have username and password

... or display their values this way:

echo Username:"!User[%site%]!"
echo Password:"!Pass[%site%]!"

You may review a detailed explanation of array management in Batch files at this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108