1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch File Read Line Of Text File

Discussion in 'Software Development' started by neo3114, Mar 15, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. neo3114

    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
     
  2. stantley

    stantley

    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).
     
  3. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    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
     
  4. DarkCyber

    DarkCyber

    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
     
  5. ghostdog74

    ghostdog74

    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
    
     
  6. lsantos2000

    lsantos2000

    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
     
  7. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/551867

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice