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 18459 times)

0 Members and 1 Guest are viewing this topic.

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 »