Account Text File
Moderator: DosItHelp
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Account Text File
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.
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.
Re: Account Text File
Code: Select all
@echo off
<file.txt (
set "_name=" &set /p "_name="
set "_passw=" &set /p "_passw="
)
set "_"
pause
exit
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Account Text File
um... explanation please? What's up with the underscores and sets?
Re: Account Text File
Any variables starting with the same set of characters can be printed on the console without having to print per variable eg:john924xps wrote:um... explanation please? What's up with the underscores and sets?
Code: Select all
set _
Code: Select all
set name
set passw
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Account Text File
<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.
Re: Account Text File
'
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.
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
-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Account Text File
That's awesome. I could try using a for loop to set all the lines of data into seperate variables...
THANKS A LOT

-
- Posts: 65
- Joined: 08 Jun 2012 07:48
Re: Account Text File
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?
Re: Account Text File
The Russians press the red button and we are all DOOMED!
Re: Account Text File
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>
Re: Account Text File
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%
Code: Select all
for /L %%i in (1,1,%lastLine%) do (
echo %%i- !line[%%i]!
)
Antonio