read one line of txt file into variable
Results 1 to 2 of 2

Thread: read one line of txt file into variable

  1. #1
    Join Date
    Nov 2001
    Location
    mechanicsburg, pa, usa
    Posts
    33

    read one line of txt file into variable

    I need a way to read one line of a txt file into a variable based on a number stored in another variable.

    Example
    If "var-a" = 45 then read the 45th line of file.txt into "var-b"
    Or
    If "var-a" = 87 then read the 87th line of file.txt into "var-b"

    Would also like to do this based on errorlevel but really need it for a variable

    Would really appreciate your help
    Thanks

  2. #2
    Join Date
    Feb 2000
    Location
    26.03°N 80.14°W
    Posts
    9,410
    Hopefully the following example will come close to meeting your requirements. Once you get it saved to your hard drive as "GetLine.bat", get into the same directory with it and type
    • GetLine /?
    for brief instructions. A quick example of usage would be:
    • GetLine 28 c:\windows\system.ini
    This would display (and store) a string containing the first six words from the 28th line of the file "c:\windows\system.ini".
    Code:
    @echo off
     if (%2)==() goto Syntax
     if (%2)==(?) goto Syntax
     if (%2)==(/?) goto Syntax
     if (%1)==() goto NoVarA
     if not exist %2 goto NoFile
     goto Begin
    :Syntax
     cls
     echo.
     echo     Name: GetLine.bat - (For U.S.A.'s MS-DOS 5.nn/6.nn and Windows 95/98/Me).
     echo.
     echo  Purpose: Display (and store) the first six words from the "nth" line in the
     echo           text file specified.
     echo.
     echo   Syntax: GetLine ## [d:][\path\]filename
     echo.
     echo    Where: "##" is the line number you want to see.
     echo.
     echo      and: "[d:][\path\]filename" is the complete "drive:\path\filename.ext" of
     echo           the file you want to use. (Note: The "[d:]" (drive) and "[\path\]"
     echo           are optional. Whether you need either or both depends on the file's
     echo           location in relation to your current location.
     echo.
     echo  Example: GetLine 28 c:\windows\system.ini
     echo.
     echo           Would return a string containing the first six words from the 28th
     echo           line of the file "c:\windows\system.ini".
     echo.
     echo Requires: FIND.EXE (anywhere in the PATH) and enough room in the environment
     echo           to hold the length of: the line number (times two), the complete
     echo           filename as well as the line retrieved, plus 41 characters.
     echo.
     goto End
    :Begin
     for %%x in (File VarA VarB) do set %%x=
     for %%x in (~ enter) do if exist %%x.bat del %%x.bat
     set VarA=%1
     set File=%2
     :FindIt
     type %File%|find /n /v "!@#"|find "[%VarA%]">~.bat
     if errorlevel 1 goto NotFound
     echo.>>~.bat
     date<~.bat | find "Enter">~.bat
     echo set VarB=%%4 %%5 %%6 %%7 %%8 %%9>enter.bat
     for %%x in (call del) do %%x ~.bat
     del enter.bat
    :Display
     echo.
     echo File: %File%
     echo VarA: %VarA%
     echo VarB: %VarB%
     echo.
     set cmdline=
     goto End
    :NoFile
     echo.
     echo   Error: File "%2" does not exist
     echo.
     goto End
    :NoVarA
     echo.
     echo   Error: You must specify a line number
     echo.
     goto End
    :NotFound
     echo.
     echo   Error: File "%File%" does not contain a line number "%VarA%"
     echo.
    :End
    BTW, GetLine.bat sets three environment variables: "VARA" will contain the specified line number, "VARB" the returned six-word string and "FILE" the filename specified.
    Vernon Frazee, Microsoft MVP (Windows - Shell/User)

    Defenses Up!
    Tip: When prompted for a password, give an incorrect one first. A phishing site will accept it; a legitimate one won't.


    Inside Spyware: A Guide to Finding, Removing and Preventing Online Pests


    If you don't keep up with security fixes, your computer|network won't be yours for long.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •