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

Author Topic: XP pro DOS batch file editing  (Read 13446 times)

0 Members and 1 Guest are viewing this topic.

LesD

    Topic Starter


    Rookie

    XP pro DOS batch file editing
    « on: June 04, 2007, 04:16:47 AM »
    Hi,

    Can I use, maybe the FOR command, in a batch file run from the DOS Command prompt, to change say the value  60.0 on an arbitary line in a text file to the value 50.0?

    For example if in my text file I had the lines:


    Any Text1
    Any Text2
    .
    .
    .
    Any TextN
    60.0
    Any TextM
    .
    .
    Any TextZ

    Can a batch file be written to change my file's content to be:

    Any Text1
    Any Text2
    .
    .
    .
    Any TextN
    50.0
    Any TextM
    .
    .
    Any TextZ

    Typically an "Any Text" line would contain ASCII characters, either letter or numbers separated by commas.

    Finally if this is possible can the tecnique be applied to all the files in a directory of maybe a 100 or more files so that each one, with its own unique name, has the value 60.0 in it changed to 50.0?

    I am told that the only way is to write a C program but I am not conversant with C and  thought I might be able to do it in a batch file but so far I have drawn a blank!

    Any help or advice would be very much appreciated.  :)

    Regards,

    Les.


    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: XP pro DOS batch file editing
    « Reply #1 on: June 04, 2007, 04:46:50 AM »
    Something like this may work:

    Code: [Select]
    @echo off
    setlocal enableextensions
    setlocal enabledelayedexpansion
    for /f %%x in ('dir /a:-d /s /b') do (
    for /f "tokens=* delims=" %%i in (%%x) do (
    set input=%%i
    set input=!input:60.0=50.0!
    echo !input! >> %%x.chg
    )
    )
    endlocal

    The output files are labeled with chg appended to the original file name.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    LesD

      Topic Starter


      Rookie

      Re: XP pro DOS batch file editing
      « Reply #2 on: June 04, 2007, 06:26:01 AM »
      Something like this may work:

      An understatement if ever there was one!

      My most grateful thanks Sidewinder.

      It does just what I wanted my only challenge now is to understand how!  :)

      Regards,

      Les.

      LesD

        Topic Starter


        Rookie

        Re: XP pro DOS batch file editing
        « Reply #3 on: June 06, 2007, 04:09:05 AM »
        Hi again,

        With the code Sidewinder so generously provided I thought I had the task cracked but a mystery remains that is haunting me!  ???

        My modified output file has an extra unwanted space added at the end of every line except the one that has been modified!  :o

        Try as I may I cannot see where this space is coming from. I wrote a little tester batch file that does not do the echo >> appends inside the DO loop and it behaves as expected i.e. does not add unwanted spaces.

        This tester contains:

        @echo off
        (set input=A)
        echo %input%>> les.txt
        set input=b
        echo %input%>> les.txt
        set input=%1
        echo %input%>> les.txt
        set input=%SystemRoot%
        echo %input%>> les.txt

        whereas the little utility I need that does add the mysterious spaces to all but the modified line contains:

        @echo off

        if "%1"=="/?" echo.
        if "%1"=="/?" echo.
        if "%1"=="/?" echo To change 60 Hz to 50 Hz the syntax is:  CFGChgFreq 60.0 50.0
        if "%1"=="/?" echo.
        if "%1"=="/?" echo To change 50 Hz to 60 Hz the syntax is:  CFGChgFreq 50.0 60.0
        if "%1"=="/?" echo.
        if "%1"=="/?" echo To  display  this  syntax  information:  CFGChgFreq /?
        if "%1"=="/?" echo.
        if "%1"=="/?" echo Note the COMTRADE file names must NOT contain SPACES
        if "%1"=="/?" echo.
        if "%1"=="/?" echo.
        if "%1"=="/?" goto quit

        @echo off
        setlocal enableextensions
        setlocal enabledelayedexpansion
        for /f %%x in ('dir /a:-d /s /b') do (
           for /f "tokens=1* delims=," %%i in (%%x) do (
              set input1=%%i
              set input2=%%j
              set input1=!input1:%1=%2!
              if %%i==%1  echo !input1!>> %%x.chg
              if not %%i==%1 ( if not "%%j"=="" echo %%i,%%j>> %%x.chg )
              if not %%i==%1 ( if "%%j"=="" echo %%i>> %%x.chg )
           )
        )
        endlocal   

        del *.bat.chg
        del *.cfg
        ren *.chg *.

        :quit



        Any help or advice to end my bewilderment would be very much appreciated.  :)

        Regards,

        Les.

        contrex

        • Guest
        Re: XP pro DOS batch file editing
        « Reply #4 on: June 06, 2007, 05:44:00 AM »
        how are you passing parameters %1 and %2 to the batch?

        LesD

          Topic Starter


          Rookie

          Re: XP pro DOS batch file editing
          « Reply #5 on: June 06, 2007, 06:58:48 AM »
          Hi contrex,

          In the command line like this: CFGChgFreq.bat 60.0 50.0

          A typical file that this utility is to be used on contains the lines below and if you select and hence the highlight these lines, the extra spaces show up. This particular files has been changed from 60.0 to 50.0 and back a few times hence the build up of spaces at the end of all the lines except the one containing 60.0. Before the first change the Cr in hard up against the end of the data i.e. no spaces. The file name is, P74xTest100.cfg in this instance.

          RTDS Simulation, 0     
          42,42A,0D     
          1,ICT1A,,,Amps,660.0600,-7.69605,0,0,4096     
          2,ICT1B,,,Amps,550.0500,-12.3293,0,0,4096     
          3,ICT1C,,,Amps,9.64931E-4,-1.93509,0,0,4096     
          4,ICT2A,,,Amps,0.0384691,-88.9755,0,0,4096     
          5,ICT2B,,,Amps,0.0392433,-70.1688,0,0,4096     
          6,ICT2C,,,Amps,0.0010124,-2.07401,0,0,4096     
          7,ICT3A,,,Amps,0.0131376,-20.4884,0,0,4096     
          8,ICT3B,,,Amps,0.00950076,-25.7746,0,0,4096     
          9,ICT3C,,,Amps,0.00366039,-7.57839,0,0,4096     
          10,ICT4A,,,Amps,0.0220916,-42.0355,0,0,4096     
          11,ICT4B,,,Amps,0.0269869,-58.5826,0,0,4096     
          12,ICT4C,,,Amps,0.00545212,-11.1679,0,0,4096     
          13,ICT5A,,,Amps,0.00402454,-7.47994,0,0,4096     
          14,ICT5B,,,Amps,0.00327226,-7.53246,0,0,4096     
          15,ICT5C,,,Amps,9.64909E-4,-1.93504,0,0,4096     
          16,ICT6A,,,Amps,0.00407965,-8.90137,0,0,4096     
          17,ICT6B,,,Amps,0.00315066,-5.91296,0,0,4096     
          18,ICT6C,,,Amps,0.00119272,-2.44367,0,0,4096     
          19,VA1S,,,,0.0542696,-120.31,0,0,4096     
          20,VB1S,,,,0.0508312,-106.761,0,0,4096     
          21,VC1S,,,,0.0492263,-100.819,0,0,4096     
          22,VA2S,,,,0.0542593,-120.279,0,0,4096     
          23,VB2S,,,,0.0508256,-106.753,0,0,4096     
          24,VC2S,,,,0.0492212,-100.807,0,0,4096     
          25,VA3S,,,,0.0542782,-120.315,0,0,4096     
          26,VB3S,,,,0.0508488,-106.801,0,0,4096     
          27,VC3S,,,,0.0492443,-100.853,0,0,4096     
          28,VA4S,,,,0.0542058,-120.159,0,0,4096     
          29,VB4S,,,,0.0507793,-106.662,0,0,4096     
          30,VC5S,,,,0.0492208,-100.807,0,0,4096     
          31,VA5S,,,,0.0542602,-120.285,0,0,4096     
          32,VB5S,,,,0.0508255,-106.751,0,0,4096     
          33,VC5S,,,,0.0492208,-100.807,0,0,4096     
          34,VA6S,,,,0.0561917,-130.248,0,0,4096     
          35,VB6S,,,,0.0485811,-102.166,0,0,4096     
          36,VC6S,,,,0.0465697,-95.3779,0,0,4096     
          37,VBB1AS,,,,0.0542509,-120.26,0,0,4096     
          38,VBB1BS,,,,0.0508198,-106.742,0,0,4096     
          39,VBB1CS,,,,0.0492154,-100.795,0,0,4096     
          40,VBB2AS,,,,0.0561969,-130.258,0,0,4096     
          41,VBB2BS,,,,0.0485872,-102.177,0,0,4096     
          42,VBB2CS,,,,0.0465755,-95.3898,0,0,4096     
          60.0
          1     
          2159.83,5400     
          05/29/2007,16:01:35.460000     
          05/29/2007,16:01:35.510000     
          ASCII     




          Regards,

          Les.

          ghostdog74



            Specialist

            Thanked: 27
            Re: XP pro DOS batch file editing
            « Reply #6 on: June 06, 2007, 09:39:55 PM »
            alternative in vbscript
            Code: [Select]
            Const ForAppending=2
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFile = objFSO.OpenTextFile("C:\temp\test1.txt", 1)
            Set objOutFile = objFSO.OpenTextFile("c:\temp\temp.txt", ForAppending,True)
            Do Until objFile.AtEndOfStream
                strNextLine = objFile.ReadLine
                If InStr(strNextLine,"60.0") = 1 Then       
                    objOutFile.WriteLine(Replace("60.0",strNextLine,"50.0"))
                Else
                    objOutFile.WriteLine(strNextLine)
                End If
            Loop
            objOutFile.Close
            objFile.Close
            objFSO.DeleteFile("C:\temp\test1.txt")
            objFSO.MoveFile "C:\temp\temp.txt" , "C:\temp\test1.txt"

            LesD

              Topic Starter


              Rookie

              Re: XP pro DOS batch file editing
              « Reply #7 on: June 07, 2007, 02:17:07 AM »
              Thanks for the vb code Ghostdog74,

              Just a couple of things:  :-\

              I don't know what to do with vb! :-[
              Does this mean that the DOS FOR - DO route is exhausted and not capable of writing out the lines without a trailing space?  ???

              Regards,

              Les.

              contrex

              • Guest
              Re: XP pro DOS batch file editing
              « Reply #8 on: June 07, 2007, 02:20:27 AM »
              This is weird because when I write variables to text files from a batch, they have trailing CR+LFs but no spaces. I would investigate further.


              LesD

                Topic Starter


                Rookie

                Re: XP pro DOS batch file editing
                « Reply #9 on: June 07, 2007, 02:41:38 AM »
                Hi Contrex,

                The Cr Lf is there but after an additional space character that is added each time you run the batch file. A quick look at the file size confirms it is getting bigger every time the batch file switches 60.0 to 50.0 then 50.0 back to 60.0 etc.

                Bizarre isn't it! ???

                Regards,

                les.

                contrex

                • Guest
                Re: XP pro DOS batch file editing
                « Reply #10 on: June 07, 2007, 02:43:52 AM »
                don't give up on the batch method. Check back in about 6 hours!

                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: XP pro DOS batch file editing
                  « Reply #11 on: June 07, 2007, 02:48:03 AM »
                  Quote
                  I don't know what to do with vb! :-[
                  save the file as .vbs, open a dos prompt, then type cscript <yourfilename.vbs>

                  Quote
                  Does this mean that the DOS FOR - DO route is exhausted and not capable of writing out the lines without a trailing space?  ???
                  not sure. maybe contrex will tell you some time later...:) have fun.

                  LesD

                    Topic Starter


                    Rookie

                    Re: XP pro DOS batch file editing
                    « Reply #12 on: June 07, 2007, 04:20:35 AM »
                    I shall hang in there contrex but in the meantime, ever eager to learn.......

                    I have managed to get ghostdog74's vb script to run but on just the one file not my directory full. This time that one file is a perfect clone of the original apart from containing 50.0 instead of 60.0. Eureka absolutely no extra spaces using vb!  :)

                    Trouble is I don't have the vb skill (so far) to turn it into the little utility that I posted yesterday at 04:09:05 AM, the fourth one from the top.

                    Never satisfied am I.  ;)

                    Sidewinder



                      Guru

                      Thanked: 139
                    • Experience: Familiar
                    • OS: Windows 10
                    Re: XP pro DOS batch file editing
                    « Reply #13 on: June 07, 2007, 04:54:09 AM »
                    Sorry about the confusion. Seems the echo instruction takes everything quite literally. The change to the echo statement is minor but apparently critical.

                    Code: [Select]
                    @echo off
                    setlocal enabledelayedexpansion
                    for /f %%x in ('dir /a:-d /s /b') do (
                    for /f "tokens=1* delims=" %%i in (%%x) do (
                    set input=%%i
                    set input=!input:60.0=50.0!
                    echo !input!>> %%x.chg
                    )
                    )
                    endlocal

                    VBScript (which is installed free with Windows) is a better tool for these type of solutions. Batch code was never meant to be a programming language.

                    Good luck. 8)
                    The true sign of intelligence is not knowledge but imagination.

                    -- Albert Einstein

                    ghostdog74



                      Specialist

                      Thanked: 27
                      Re: XP pro DOS batch file editing
                      « Reply #14 on: June 07, 2007, 05:04:31 AM »
                      I have managed to get ghostdog74's vb script to run but on just the one file not my directory full.
                      to go over a directory in vb, have to make use of GetFolder function. anyway, if you are not familiar now, you can easily use for loop over your directory, then call the vbscript with arguments
                      Code: [Select]
                      cscript myscript.vbs test1.txt 60.0 50.0
                      then you for loop could be
                      Code: [Select]
                      for /F %%a in ( directory listing ) do (
                        cscript myscript.vbs %%a 60.0 50.0
                      )
                      I have not tried it, just a concept.

                      amended vbscript may look like this
                      Code: [Select]
                      Set objArgs = WScript.Arguments
                      fileName=objArgs(0)
                      oldString=objArgs(1)
                      newString=objArgs(2)
                      Const ForAppending=2
                      Set objFSO = CreateObject("Scripting.FileSystemObject")
                      Set objFile = objFSO.OpenTextFile(fileName, 1)
                      Set objOutFile = objFSO.OpenTextFile("c:\temp\temp.txt", ForAppending,True)
                      Do Until objFile.AtEndOfStream
                          strNextLine = objFile.ReadLine
                          If InStr(strNextLine,oldString) = 1 Then       
                              objOutFile.WriteLine(Replace(oldString,strNextLine,newString))
                          Else
                              objOutFile.WriteLine(strNextLine)
                          End If
                      Loop
                      objOutFile.Close
                      objFile.Close
                      objFSO.DeleteFile(fileName)
                      objFSO.MoveFile "C:\temp\temp.txt" , fileName