# Batch File Read Line Of Text File



## neo3114

Hi,

Can anyone tell me how to get part of a bacth file to read a line from a text file and then set it to a variable.

For example the file contains computername =computer1

how do i get the batch to read that and set computer1 to a variable that can be used within the batch file.

If possible what i would also like to be able to do is once set that to variable, compare it against the correct computer number and if incorrect go back into the batch file and set the line to the correct computer name

Any help is appreciated.

Thanks


----------



## stantley

I'm not sure if you could do that in a batch file, but it could be easily done with AutoHotkey (freeware). It would basically be a FileReadLine command and a FileAppend command (pretty much the same as a write).


----------



## Rollin_Again

This can also easily be done using VBScript. Please post more details of what other actions will be performed within the script and if possible post a sample text file. If you insist on using a batch file, you should be able to integrate the batch file to call a VB script if needed.

Regards,
Rollin


----------



## DarkCyber

Mabe this is what you are looking for I made it one for my thumb drive

Example:

@echo off
echo "Read a line of text from a text file" >Text.txt
set /p var= <Text.txt
echo %var%
pause
exit

Thumb drive batch:

@echo off
title Stealth Launcher By DarkCyber

if exist launch\app1.txt. (
set /p app1= <launch\app1.txt.
)

if exist launch\app1.txt %app1%

if exist launch\app2.txt. (
set /p app2= <launch\app2.txt.
%app2%.
)

if exist launch\app2.txt %app2%

if exist launch\app3.txt. (
set /p app3= <launch\app3.txt.
%app3%.
)

if exist launch\app3.txt %app3%

if exist launch\app4.txt. (
set /p app4= <launch\app4.txt.
%app4%.
)

if exist launch\app5.txt %app5%

if exist launch\app5.txt. (
set /p app5= <launch\app5.txt.
%app5%.
)

if exist launch\app5.txt %app5%

If anyone now how to read more lines from a singel text file I will be happy to hear it


----------



## ghostdog74

Code:


Option Explicit
Dim objFSO,objFile,myFile,line,Result
myFile="C:\temp\test.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(myFile,1)
Do Until objFile.AtEndOfLine
	line=objFile.ReadLine
	If InStr(1,line,"computername") > 0 Then
		Result=Split(line,"=")
		WScript.Echo Result(1)
		WScript.Quit
	End If
Loop
Set objFSO=Nothing
Set objFile=Nothing

how to use


Code:


C:\vbscript>for /F %I in ('cscript /nologo test1.vbs') do echo %I

C:\vbscript>echo computer1
computer1

C:\vbscript>

or in batch


Code:


C:\vbscript>for /F "tokens=2 delims==" %I in ('findstr computername c:\temp\test.txt') do echo %I

C:\vbscript>echo computer1
computer1


----------



## lsantos2000

Hi... I have a program that needs to be called from command prompt as:

c:\migrate -olduser peter -newuser peter1

that's ok to do one username at a time, but I want to read the list from a file

So 'user.txt' would have a comman separated list of names. e.g.

peter,peter1
john,john_2000
leo,leonardo

A plain batch solutions is OK for me, I guess using FOR?... scripting is also OK, not sure if I can call the external program from scripting

Any ideas?

Thanks

Leo


----------

