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

Author Topic: Batch file to convert MarkShulze winds txt to CSV file in right format  (Read 4556 times)

0 Members and 1 Guest are viewing this topic.

ker

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 10
    Hi.
    I searched the forum and found some anwers that allmost helped me, but not enough, because of my missing programming skillz. So i hope that some of u guys&girls can help me out.

    I need to convert this txt file containing this

    Wind forecast from NOAA RAP
    Lat       Lon        Time
    45.322   -75.888   2000Z
    QNH hPa   QFE hPa
    1017.5   1002.2
     Alt    Dir   Spd   Temp
    ftAGL    deg   kts   degC
        0   102     4      8
     1000   134     9      7
     2000   213    23     12
     3000   241    28     13
     4000   254    30     12
     5000   263    33     12
     6000   264    37     12
     7000   260    40     11
     8000   256    43      8
     9000   253    46      6
    10000   252    47      4
    11000   253    48      2

    into a CSV looking like this

    ;Altitude(feet), Direction(degrees), Speed(knots)
    0,120,4
    1000,134,09
    2000,213,23
    3000,241,28
    4000,254,30
    5000,263,33
    6000,264,37
    7000,260,40
    8000,256,43
    9000,253,46
    10000,252,47
    11000,252,48

    So the top 7 lines are replaced with 1 line.
    And the last colum showing temperature is removed. (if possible)

    Can any1 help solving this, in a batch file ?

    Salmon Trout

    • Guest
    Re: Batch file to convert MarkShulze winds txt to CSV file in right format
    « Reply #1 on: October 07, 2018, 03:08:26 AM »
    Your wanted output doesn't match the input values, but, however...

    Process.bat:

    @echo off
    echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv
    for /f "skip=7 tokens=1,2,3" %%A in (input.txt) do echo %%A,%%B,%%C >> output.cs
    v


    Example of usage, showing input & output files:

    C:\Batch>type input.txt
    Wind forecast from NOAA RAP
    Lat       Lon        Time
    45.322   -75.888   2000Z
    QNH hPa   QFE hPa
    1017.5   1002.2
     Alt    Dir   Spd   Temp
    ftAGL    deg   kts   degC
        0   102     4      8
     1000   134     9      7
     2000   213    23     12
     3000   241    28     13
     4000   254    30     12
     5000   263    33     12
     6000   264    37     12
     7000   260    40     11
     8000   256    43      8
     9000   253    46      6
    10000   252    47      4
    11000   253    48      2

    C:\Batch>process.bat
    C:\Batch>type output.csv
    ;Altitude(feet), Direction(degrees), Speed(knots)
    0,102,4
    1000,134,9
    2000,213,23
    3000,241,28
    4000,254,30
    5000,263,33
    6000,264,37
    7000,260,40
    8000,256,43
    9000,253,46
    10000,252,47
    11000,253,48


    ker

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Windows 10
      Re: Batch file to convert MarkShulze winds txt to CSV file in right format
      « Reply #2 on: October 07, 2018, 07:19:49 AM »
      What!! I was making way longer scritps to make this happen and you come up with something as short and beautiful as this.

      Tx man! Works like a charm.

      Salmon Trout

      • Guest
      Re: Batch file to convert MarkShulze winds txt to CSV file in right format
      « Reply #3 on: October 07, 2018, 07:51:50 AM »
      You can get it into just 1 line:

      Batch script line:

      @echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv & for /f "skip=7 tokens=1,2,3" %%A in (input.txt) do @echo %%A,%%B,%%C >> output.csv
       


      If you wanted to paste it into a console window, change all of the double percents to single percents:

      Console command:

      @echo ;Altitude(feet), Direction(degrees), Speed(knots) > output.csv & for /f "skip=7 tokens=1,2,3" %A in (input.txt) do @echo %A,%B,%C >> output.csv


      Salmon Trout

      • Guest
      Re: Batch file to convert MarkShulze winds txt to CSV file in right format
      « Reply #4 on: October 07, 2018, 02:14:36 PM »
      The batch script as written is a bit limited; it only works with an input file called "input.txt" and it only produces an output file called "output.txt". This version requires an input file name as a parameter and emits an output file with the input name with a .csv extension, like so NOA1172xvy.txt ---> NOA1172xvy.csv

      Process3.bat:

      @echo ;Altitude(feet), Direction(degrees), Speed(knots) > "%~n1.csv" & for /f "skip=7 tokens=1,2,3" %%A in ('type "%~1"') do @echo %%A,%%B,%%C >> "%~n1.csv"

      Works with spaces in input filename if you use quotes

      C:\Batch>type "ABC NOA1172xvy.txt"
      Wind forecast from NOAA RAP
      Lat       Lon        Time
      45.322   -75.888   2000Z
      QNH hPa   QFE hPa
      1017.5   1002.2
       Alt    Dir   Spd   Temp
      ftAGL    deg   kts   degC
          0   102     4      8
       1000   134     9      7
       2000   213    23     12
       3000   241    28     13
       4000   254    30     12
       5000   263    33     12
       6000   264    37     12
       7000   260    40     11
       8000   256    43      8
       9000   253    46      6
      10000   252    47      4
      11000   253    48      2

      C:\Batch\>process3.bat "ABC NOA1172xvy.txt"
      C:\Batch\>type "ABC NOA1172xvy.csv"
      ;Altitude(feet), Direction(degrees), Speed(knots)
      0,102,4
      1000,134,9
      2000,213,23
      3000,241,28
      4000,254,30
      5000,263,33
      6000,264,37
      7000,260,40
      8000,256,43
      9000,253,46
      10000,252,47
      11000,253,48


      ker

        Topic Starter


        Starter

        • Experience: Experienced
        • OS: Windows 10
        Re: Batch file to convert MarkShulze winds txt to CSV file in right format
        « Reply #5 on: October 08, 2018, 08:27:49 PM »
        hahau read my mind -  tx. I was just playing around with it to do exatly that.

        ker

          Topic Starter


          Starter

          • Experience: Experienced
          • OS: Windows 10
          Re: Batch file to convert MarkShulze winds txt to CSV file in right format
          « Reply #6 on: October 09, 2018, 07:40:46 AM »
          Hm becausefile name is

          winds_45.28,-75.91_1200Z.txt

          it will only put in
          ;Altitude(feet), Direction(degrees), Speed(knots)

          into the file.   If I rename the file it works.. Whats the issue?

          Salmon Trout

          • Guest
          Re: Batch file to convert MarkShulze winds txt to CSV file in right format
          « Reply #7 on: October 09, 2018, 09:39:26 AM »
          Use quotes for filenames with spaces or other troublesome characters like dots or commas. In fact you can always use quotes to be safe.

          No quotes around input file name:

          C:\Batch>process3.bat winds_45.28,-75.91_1200Z.txt
          The system cannot find the file specified.


          A file called "winds45.csv" (shortened name!) was created with just the first line, like you showed.

          Using quotes around input file name:

          C:\Batch\>process3.bat "winds_45.28,-75.91_1200Z.txt"
          C:\Batch>


          This time, the batch completed. Show output file:

          C:\Batch>type "winds_45.28,-75.91_1200Z.csv"
          ;Altitude(feet), Direction(degrees), Speed(knots)
          0,102,4
          1000,134,9
          2000,213,23
          3000,241,28
          4000,254,30
          5000,263,33
          6000,264,37
          7000,260,40
          8000,256,43
          9000,253,46
          10000,252,47
          11000,253,48



          Salmon Trout

          • Guest
          Re: Batch file to convert MarkShulze winds txt to CSV file in right format
          « Reply #8 on: October 09, 2018, 11:47:42 AM »
          A file called "winds45.csv" (shortened name!) was created with just the first line, like you showed.
          In fact, the one-line file with the shortened name was called winds_45.csv with an underscore like you'd expect.