Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Help with batch file- read Nth line in delimited text file  (Read 18460 times)

0 Members and 1 Guest are viewing this topic.

RForte

  • Guest
Help with batch file- read Nth line in delimited text file
« on: September 10, 2009, 03:33:31 PM »
I used the search and couldn't find any topics that answered my question.  If topics like this already exist, I apologize.

I have to write a batch file in DOS for work that requires me to take information from a specified line in a comma-delimited file, and be able to change the 2nd field which serves as a flagging system.

For instance, if I run the batch file, it prompts me for a number - I want to be able to enter 2079, have it go to the 2079th line, return whatever is in the first field as an argument or variable, and change the second field from 0 to 1 (the flagging system).

If it were linux, I'd have no problem, but I don't know how to do this with DOS/Windows batch files.  I'd greatly appreciate any assistance in writing this batch file.

gh0std0g74



    Apprentice

    Thanked: 37
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #1 on: September 10, 2009, 06:08:45 PM »
    vbscript
    Code: [Select]
    Set objFS=CreateObject("Scripting.FileSystemObject")
    strFile = "c:\test\file"
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfLine
    linenumber = objFile.Line
    strLine = objFile.ReadLine
    If linenumber = 2049 Then
    WScript.Echo linenumber
    csv = Split(strLine,",")
    csv(1) = "new value" 'change 2nd field
    strLine = Join(csv,",")
    End If
    WScript.Echo strLine
    Loop
    objFile.Close
    usage
    Code: [Select]
    c:\test> cscript /nologo myscript.vbs

    since you know linux, and if you can download stuff, there is more simpler way to do that, using gawk (see my sig ) for windows

    Code: [Select]
    C:\test>gawk -F"," "NR==2049{$2=\"new\"}1" OFS="," file

    Salmon Trout

    • Guest
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #2 on: September 11, 2009, 12:16:50 AM »
    Quote
    For instance, if I run the batch file, it prompts me for a number - I want to be able to enter 2079, have it go to the 2079th line, return whatever is in the first field as an argument or variable, and change the second field from 0 to 1 (the flagging system).

    Do you then need to write out the complete file to disk, replacing the previous version?

    billrich

    • Guest
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #3 on: September 11, 2009, 09:13:32 AM »
    REM  Don't ask for line number; use command line argument :
    test10.bat 2079
    rem if flag is set to 0, will not change to 1
    Change this line of code:
    if !c! EQU %1  set str2=!str:,1,=,0,! && echo.!str2! >> newdata.txt
    to
    if !c! EQU %1  set str2=!str:,0,=,1,! && echo.!str2! >> newdata.txt
    (* see Below)

    Code: [Select]
    @ echo off

    setLocal EnableDelayedExpansion

    echo.  > newdata.txt
    set /a c=0
    for /f "delims==" %%a in (testdata.txt) do (

    set /a c+=1
    if !c! NEQ %1 echo %%a  >> newdata.txt


    if !c! EQU %1 set str=%%a && echo.!str!
    rem if !c! EQU %1  set str2=!str:,1,=,0,! && echo.!str2! >> newdata.txt
    if !c! EQU %1  set str2=!str:,0,=,1,! && echo.!str2! >> newdata.txt



    )
    echo.
    echo newdata.txt
    echo.

    type newdata.txt

    echo.
    echo old testdata.txt
    echo.
    type  testdata.txt


    Output:

    C:\> test10.bat  5
    5,1,mess,nothing

    newdata.txt


    1,0,mess,nothing
    2,0,mess,nothing
    3,0,mess,nothing
    4,0,mess,nothing
    5,0,mess,nothing

    old testdata.txt

    1,0,mess,nothing
    2,0,mess,nothing
    3,0,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    C:\>

    Reference:
    http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace

    ____________________________________
    *Below:

    C:\>test10.bat 3
    3,0,mess,nothing

    newdata.txt


    1,0,mess,nothing
    2,0,mess,nothing
    3,1,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    old testdata.txt

    1,0,mess,nothing
    2,0,mess,nothing
    3,0,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    C:\>
    « Last Edit: September 11, 2009, 12:02:36 PM by billrich »

    billrich

    • Guest
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #4 on: September 11, 2009, 03:53:57 PM »
    C:\>type field1.bat
    Code: [Select]
    @echo off
    REM  use command line argument for line number  field1.bat 3

    setLocal EnableDelayedExpansion

    set /a c=0
    for /f "tokens=1,2* delims==," %%i in (testdata.txt) do (

    set /a c+=1

    set Field1=%%i

    if !c! EQU %1 echo  Field one is !Field1!


    )
    C:\>field1.bat 3
     Field one is 3

    C:\>
    C:\>field1.bat 5
     Field one is 5

    C:\>
    C:\>type  testdata.txt
    1,0,mess,nothing
    2,0,mess,nothing
    3,0,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    C:\>
    « Last Edit: September 11, 2009, 04:09:28 PM by billrich »

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #5 on: September 11, 2009, 05:17:44 PM »
    How was this comma delimited file created?
    If it was the output of a spreadsheet, you might use the macro language of the original program to get what you need.

    billrich

    • Guest
    Re: Help with batch file- read Nth line in delimited text file
    « Reply #6 on: September 11, 2009, 05:29:59 PM »
    gh0std0g74,

    I ran your myscript.vbs with my test data:


    C:\test>type  myscript.vbs
    Code: [Select]
    Set objFS=CreateObject("Scripting.FileSystemObject")
    strFile = "c:\testdata.txt"
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfLine
            linenumber = objFile.Line
            strLine = objFile.ReadLine
            If linenumber = 3 Then
                    WScript.Echo linenumber
                    csv = Split(strLine,",")
                    csv(1) = "new value" 'change 2nd field
                    strLine = Join(csv,",")
            End If
            WScript.Echo strLine
    Loop
    objFile.Close
    C:\test>cscript /nologo myscript.vbs
    1,0,mess,nothing
    2,0,mess,nothing
    3
    3,new value,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    C:\test>

    The output for the second field ( flag field ) should be a "0" or "1";
    your program put "new value."
    Also, the number 3 record remains?
    Is testdata.txt not good test data?


    C:\>type testdata.txt
    1,0,mess,nothing
    2,0,mess,nothing
    3,0,mess,nothing
    4,0,mess,nothing
    5,1,mess,nothing

    C:\>

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Help with batch file- read Nth line in delimited text file
      « Reply #7 on: September 11, 2009, 05:43:59 PM »
      @billrich, what are you suggesting? that i do everything nicely for OP?

      billrich

      • Guest
      Re: Help with batch file- read Nth line in delimited text file
      « Reply #8 on: September 11, 2009, 06:14:39 PM »
      @billrich, what are you suggesting? that i do everything nicely for OP?

      Yes and for me.  I have never  run *.vbs script.  I'm trying to learn something new at 72.

      The line number (2049) is hardcoded  in the script.  Would not a variable or command argument work better?

      You have had questions about my code in the past.  I learn from such questions.  I'm still a newbie with batch.  My code for this problem works but has several bugs. The first field save could/should be part of second field replace?

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Help with batch file- read Nth line in delimited text file
        « Reply #9 on: September 11, 2009, 07:35:14 PM »
        Yes and for me.  I have never  run *.vbs script.  I'm trying to learn something new at 72.
        the questions you posed has very very trivial solutions to it, that's why i was puzzled about it. I mean, its not as if there's a logic problem in my code that you don't understand.

        Quote
        The line number (2049) is hardcoded  in the script.  Would not a variable or command argument work better?
        of course , putting it into a variable or argument is better... and that's also trivial to do. setting variables and using command line arguments are so common in programming languages that the only thing you need to ever do is look up the syntax for it. With vbscript, see here for example.

        Quote
        The first field save could/should be part of second field replace?
        i am going to assume i know what you are asking... do you mean csv(1) ?? its the 2nd field. When you split a string in vbscript using split() , the first array element that is returned has index 0, which is the first field.


        please look at my sig (link for vbscript manual) if you are interested.

        billrich

        • Guest
        Re: Help with batch file- read Nth line in delimited text file
        « Reply #10 on: September 12, 2009, 10:32:26 AM »
        the questions you posed has very very trivial solutions to it, that's why i


        You should move to *.vbs area and leave the batch area for batch script.

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Help with batch file- read Nth line in delimited text file
          « Reply #11 on: September 12, 2009, 06:16:14 PM »

          You should move to *.vbs area and leave the batch area for batch script.
          no, the moderator should do that, not me. Besides, no where in this forum is there any rules that say vbs is not allowed.  I am going to pose this question to you again.... what EXACTLY is a batch script/file? if you an accurately tell me the answer, i will give it to you.

          billrich

          • Guest
          Re: Help with batch file- read Nth line in delimited text file
          « Reply #12 on: September 12, 2009, 07:07:01 PM »
          no, the moderator should do that, not me. Besides, no where in this forum is there any rules that say vbs is not allowed.  I am going to pose this question to you again.... what EXACTLY is a batch script/file? if you an accurately tell me the answer, i will give it to you.

          Definition of: BAT file

          (BATch file) A file of DOS commands, which are executed one after the other. It has a .BAT extension and is created with a text editor . See DOS batch file and DOS AUTOEXEC.BAT.


          http://www.pcmag.com/encyclopedia_term/0,2542,t=BAT+file&i=38451,00.asp

          A file that contains a sequence, or batch, of commands. Batch files are useful for storing sets of commands that are always executed together because you can simply enter the name of the batch file instead of entering each command individually.
          In DOS systems, batch files end with a.BAT extension. For example, the following DOS batch file prints the date and time and sets the prompt to GO>:
          date
          time
          prompt [GO>]
          http://www.webopedia.com/TERM/B/batch_file.html



          billrich

          • Guest
          Re: Help with batch file- read Nth line in delimited text file
          « Reply #13 on: September 12, 2009, 07:38:28 PM »
          no, the moderator should do that, not me. Besides, no where in this forum is there any rules that say vbs is not allowed.  I am going to pose this question to you again.... what EXACTLY is a batch script/file? if you an accurately tell me the answer, i will give it to you.

          GhostDog,

          I'm pulling your leg.  We like to see your VBS and other code.

          Keep posting.   But post your Output along with your code. 

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Help with batch file- read Nth line in delimited text file
            « Reply #14 on: September 12, 2009, 11:19:03 PM »
            Definition of: BAT file

            (BATch file) A file of DOS commands, which are executed one after the other. It has a .BAT extension and is created with a text editor . See DOS batch file and DOS AUTOEXEC.BAT.


            http://www.pcmag.com/encyclopedia_term/0,2542,t=BAT+file&i=38451,00.asp

            A file that contains a sequence, or batch, of commands. Batch files are useful for storing sets of commands that are always executed together because you can simply enter the name of the batch file instead of entering each command individually.
            In DOS systems, batch files end with a.BAT extension. For example, the following DOS batch file prints the date and time and sets the prompt to GO>:
            date
            time
            prompt [GO>]
            http://www.webopedia.com/TERM/B/batch_file.html




            there you go....
            Quote
            A file that contains a sequence, or batch, of commands. .... Batch files are useful for storing sets of commands .. .....

            a quiz for you....tell me if the code below is a batch file....
            Code: [Select]
            @echo off
            setLocal EnableDelayedExpansion
            cscript /nologo myscript.vbs > newfile
            mv newfile original file
            for /F %%a in ('type file') do (echo %%a)

            the above, contains a series of commands as defined by your definition....

            billrich

            • Guest
            Re: Help with batch file- read Nth line in delimited text file
            « Reply #15 on: September 13, 2009, 01:35:01 AM »
            Ghost Dog wrote:

            <<"A quiz for you....tell me if the code below is a batch file....

            Code:
            @echo off
            setLocal EnableDelayedExpansion
            cscript /nologo myscript.vbs > newfile
            mv newfile original file
            for /F %%a in ('type file') do (echo %%a)


            The above, contains a series of commands as defined by your definition.... ">>


            Gosh, GhostDog is really smart. Now GhostDog will demostrate that the *.vbs script fits the defintion of a Batch File.

            That is great GhostDog. Keep posting your VBS script and other code.

            But please, GhostDog, post your  test data and that your  *.vbs( VBScript ) script actually works.   

            Salmon Trout

            • Guest
            Re: Help with batch file- read Nth line in delimited text file
            « Reply #16 on: September 13, 2009, 02:02:57 AM »
            Well, you guys seem to have scared off the OP, who wrote:

            Quote
            I have to write a batch file in DOS for work

            vbscript is probably disabled...


            gh0std0g74



              Apprentice

              Thanked: 37
              Re: Help with batch file- read Nth line in delimited text file
              « Reply #17 on: September 13, 2009, 02:25:56 AM »
              Gosh, GhostDog is really smart.
              that sounds sarcastic...but nevertheless...thank you for your compliment.

              Quote
              Now GhostDog will demostrate that the *.vbs script fits the defintion of a Batch File.
              its already done. like that i posted.

              Quote
              But please, GhostDog, post your  test data and that your  *.vbs( VBScript ) script actually works.  [/font]

              since you are so insistent
              Code: [Select]
              C:\test>more file
              1,0,mess,nothing
              2,0,mess,nothing
              3,0,mess,nothing
              4,0,mess,nothing
              5,1,mess,nothing

              C:\test>cscript /nologo test.vbs
              1,0,mess,nothing
              2,0,mess,nothing
              3,new value,mess,nothing
              4,0,mess,nothing
              5,1,mess,nothing

              C:\test>more test.vbs
              Set objFS=CreateObject("Scripting.FileSystemObject")
              strFile = "c:\test\file"
              Set objFile = objFS.OpenTextFile(strFile)
              Do Until objFile.AtEndOfLine
                      linenumber = objFile.Line
                      strLine = objFile.ReadLine
                      If linenumber = 3 Then
                              csv = Split(strLine,",")
                              csv(1) = "new value"
                              strLine = Join(csv,",")
                      End If
                      WScript.Echo strLine
              Loop
              objFile.Close


              2nd field, line 3 is changed to "new value". does that answer your query?

              billrich

              • Guest
              Re: Help with batch file- read Nth line in delimited text file
              « Reply #18 on: September 13, 2009, 10:46:40 AM »
              Quote
              GhostDog wrote:

              "2nd field, line 3 is changed to "new value". does that answer your query?"

              No,  the second field is a flag field and must be either 1 or 0. "new value" is the wrong answer. Your code does not work.

              You also did not assign the the first field to a variable.

              Why post code that has not been tested?

              The OP requested  real "Batch code" not incorrect VBScript.

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: Help with batch file- read Nth line in delimited text file
                « Reply #19 on: September 13, 2009, 06:54:52 PM »
                Your code does not work.
                yes it does work. Except that I did not provide OP with FULL code. Implementing that flagging system is fairly easy.
                I want OP to try himself.
                Code: [Select]
                if csv(1) = 0 Then
                   csv(1) = 1
                end if
                in fact, there is really no need to do the above, because by changing "new value" in my code to "1", it will still do the same thing...

                Quote from: billrich
                You also did not assign the the first field to a variable.
                if OP has the motivation to learn vbscript, he should be able to do that himself.

                Quote
                Why post code that has not been tested?
                its tested .... you just don't see the point i am making...

                Quote
                The OP requested  real "Batch code" not incorrect VBScript.
                once again, define what is real Batch code, accurately.

                Dude, if you are going to provide him your solutions in full , go ahead. Depending on my mood, I am not obliged to solve everything for OP.

                If i really want to nitpick about your code, i would do that, but i will not. for example, Input argument checking, input file format ambiguity, eg your code checks for ",0,"  .... what if  the input data is  " , 0 ,"
                therefore, would you just give it a rest...

                « Last Edit: September 13, 2009, 07:05:19 PM by gh0std0g74 »