Batch File Read Line Of Text File

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

neo3114

Thread Starter
Joined
Mar 15, 2007
Messages
1
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
 
Joined
May 22, 2005
Messages
7,091
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).
 
Joined
Sep 4, 2003
Messages
4,916
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
 
Joined
Aug 21, 2007
Messages
1
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
 
Joined
Dec 7, 2005
Messages
146
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
 
Joined
Sep 27, 2007
Messages
1
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
 
Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top