Account Text File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Account Text File

#1 Post by john924xps » 26 Oct 2012 07:55

Recently, I learnt about the "set /p variable=<file.txt", but know I want to take it up a notch. I want to have in a file with two pieces of data, a username and a password. Example, these are the contents of an account file:

john924xps
mypassword

Now I want to take the first line of the file, which is the username, then insert it into a variable "username". Then take the second line, the password, and set it into the "password" variable. Can this still be accomplished through the "set /p variable=<file.txt" function? Thanks.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Account Text File

#2 Post by Ed Dyreen » 26 Oct 2012 08:15

Code: Select all

@echo off

<file.txt (
       set "_name=" &set /p "_name="
       set "_passw=" &set /p "_passw="
)

set "_"

pause
exit
Beware that set /p does not unset the variable if it can't be read therefor the manual unset.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Account Text File

#3 Post by john924xps » 26 Oct 2012 08:32

um... explanation please? What's up with the underscores and sets?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Account Text File

#4 Post by Ed Dyreen » 26 Oct 2012 08:55

john924xps wrote:um... explanation please? What's up with the underscores and sets?
Any variables starting with the same set of characters can be printed on the console without having to print per variable eg:

Code: Select all

set _
instead of:

Code: Select all

set name
set passw

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Account Text File

#5 Post by john924xps » 26 Oct 2012 23:06

<file.txt (
set "_name=" &set /p "_name="
set "_passw=" &set /p "_passw="
)

That part. I thought the syntax of the command was set /p var=<. What happening over there? And why did you need to declare the variable and then set? Aren't they the same? The part where the brackets take place, I can understand, though.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Account Text File

#6 Post by Ed Dyreen » 27 Oct 2012 00:09

'
The syntax has remained the same, all I did was move the redirection <file to the left but that doesn't matter, it works just as well on the right.

The important thing is to place the set /p inside brackets so the redirection occurs for the entire body ( the part between brackets ).
A second set /p would set the variable to the contents of the 2nd line, etcetera..

Finally the variable isn't declared, it's just being initialized with 'nothing' or unSet.
You should always assign a default value ( which can be empty ) for if the file wasn't found or the line can't be read.
Ed Dyreen wrote:Beware that set /p does not unset the variable if it can't be read therefor the manual unset.

Code: Select all

set var=myDefaultValue^( file reading failed ^)
(
       set /p "var=" %=       set var to the contents of line[1]       =%
       set /p "var=" %=       set var to the contents of line[2]       =%
       set /p "var=" %=       etcetera...                              =%
)<file

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Account Text File

#7 Post by john924xps » 27 Oct 2012 01:02

That's awesome. I could try using a for loop to set all the lines of data into seperate variables... :D THANKS A LOT

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Account Text File

#8 Post by john924xps » 27 Oct 2012 06:34

There's one more part that I do not get... Who cares about it if a line cannot be read or a file cannot be found? What's the consequence of that?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Account Text File

#9 Post by foxidrive » 27 Oct 2012 07:38

The Russians press the red button and we are all DOOMED!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Account Text File

#10 Post by Squashman » 27 Oct 2012 07:47

This is one reason why you initialize your variables first. What if you used a variable that was already an environmental variable in your batch file.

Code: Select all

C:\Users\Squashman>set username
USERNAME=Squashman

C:\Users\Squashman>set /p username=What is your username:
What is your username:

C:\Users\Squashman>set username
USERNAME=Squashman

C:\Users\Squashman>set /p username=What is your username:
What is your username:Squishy

C:\Users\Squashman>set username
USERNAME=Squishy

C:\Users\Squashman>

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Account Text File

#11 Post by Aacini » 28 Oct 2012 10:48

john924xps wrote:I could try using a for loop to set all the lines of data into seperate variables...

The Batch segment below read all the lines of a file and store they in an array variable called line:

Code: Select all

setlocal EnableDelayedExpansion
set i=0
for /F "delims=" %%a in (thefile.txt) do (
   set /A i+=1
   set line[!i!]=%%a
)
set lastLine=%i%
This way, to show numbered lines:

Code: Select all

for /L %%i in (1,1,%lastLine%) do (
   echo %%i- !line[%%i]!
)
However, previous code requires certain modifications if the file include special Batch characters, like < > | ! etc.

Antonio

Post Reply