[SOLVED] Batch file reading text files

Spazz_1219

Reputable
Jul 3, 2016
11
0
4,510
I've slowly gotten closer, step by step, to finishing this batch file of mine.
This time, I need to be able to pull information from a text file into the batch file.
For example, I'd like to be able to pull information such as a username and a password from a text file, and use it in the batch file as some kind of variable. Something like this:

+login %username% %password%

in this case the username and password would be stored on a separate file.

I'm hoping it'll be something easy-ish in order to do something like
set username=[information pulled from file here] set password=[information pulled from file here]
 
Last edited:
Solution
There are literally thousands of tutorials on reading text from a text file into variables in a batch file. A web search on [read text file command prompt] or [read text file batch file] will turn up more than you can care to read.

The solution here: https://social.technet.microsoft.co...om-a-text-file-with-a-command-line-batch-file that discusses using a FOR loop for a delimited text file should fill the bill. There are equally many tutorials on the FOR in command prompt.

britechguy

Commendable
Jul 2, 2019
1,479
243
1,340
There are literally thousands of tutorials on reading text from a text file into variables in a batch file. A web search on [read text file command prompt] or [read text file batch file] will turn up more than you can care to read.

The solution here: https://social.technet.microsoft.co...om-a-text-file-with-a-command-line-batch-file that discusses using a FOR loop for a delimited text file should fill the bill. There are equally many tutorials on the FOR in command prompt.
 
Solution

Spazz_1219

Reputable
Jul 3, 2016
11
0
4,510
There are literally thousands of tutorials on reading text from a text file into variables in a batch file. A web search on [read text file command prompt] or [read text file batch file] will turn up more than you can care to read.

The solution here: https://social.technet.microsoft.co...om-a-text-file-with-a-command-line-batch-file that discusses using a FOR loop for a delimited text file should fill the bill. There are equally many tutorials on the FOR in command prompt.

I was kind of hoping you'd mention that one because I've been poking at one of the answers for the last hour and a half.
Oddly enough the response:
for /f "delims=," %%a in file.txt do set hostname=%%a & set backupdrive=%%b & backupdir=%%c & xbackupdir=%%d
doesn't exactly seem to work as it just closes the command prompt each time.
Since this would need to be ran before I started calling variables, having that happen is a problem.

I could be doing it wrong but I had edited it as follows:
for /f "delims=," %%a in user.txt do set USERNAME=%%a & set PASSWORD=%%b

And attempted using it in this way: (mind you this is only to make sure the command is ran and not to actually run the command)
echo message login.exe +login %USERNAME% %LOGIN%

The two commands are separated by a pause so I can figure out where things are breaking.