Search code examples
parsingcsvbatch-filefindstr

How can a batch program parse a text data file for values and then save into a csv type file?


I have a bunch a raw data that really only has four values I care about: DX, DY, Width and Height in addition to the corresponding offset name.

The text file with this raw data looks like this:

0:walk: Length=5 Offset=#b0 U1=#1058000 U2=#ff U3=#0 U4=#0
  0: Offset=#218 Size=#6 U1=#0
    0: Type=0 Offset=#ed8 Size=#3f8 Width=39 Height=54 HSType=#2 HS=#23030
      HotSpot: X=2 Y=-34 DX=4 DY=-22 U1=0 U2=7 U3=0 U4=0
    1: Type=0 Offset=#12d0 Size=#3e8 Width=39 Height=53 HSType=#2 HS=#23040
      HotSpot: X=1 Y=-35 DX=1 DY=-24 U1=0 U2=7 U3=0 U4=0
    2: Type=0 Offset=#16b8 Size=#3e5 Width=38 Height=53 HSType=#2 HS=#23050
      HotSpot: X=0 Y=-35 DX=-1 DY=-24 U1=0 U2=7 U3=0 U4=0
    3: Type=0 Offset=#1aa0 Size=#3fe Width=38 Height=54 HSType=#2 HS=#23060
      HotSpot: X=0 Y=-34 DX=-3 DY=-22 U1=0 U2=7 U3=0 U4=0

...

And my csv type file needs to look like this:

00-walk-00-000,4,-22,39,54,4

00-walk-00-001,1,-24,39,53,4 ...

In other words: %a,%b,%c,%d,4 where %a is the name, %b is the DX value, %c is the DY value,%d is the Width,%e is the height.

I understand that making the %a is a little reach and maybe beyond the scope of what Im trying to do. I would be happy with just the %b,%c,%d values being extracted to separate line for each set. It is ugly data and difficult to see but the name, or %a, comes from three sources. The 00 comes from the first character the single 0. The next part of the %a is the text 'walk' right after 0, the final part comes from each subsequent frame number starting from 0. The random "offset=" blank is not the offset name, like I said I would just be happy to have the four variables %b-%e parsed into a csv file.

How can findstring be used to find wildcard values? For instance how to find the substring 'some number' in the string DX=some number when I dont know what that some number is? And whether or not this is possible with batch?


Solution

  • @echo off
    setlocal EnableDelayedExpansion
    
    set "spaces=    " & set "i=0"
    for /F "delims=" %%A in (input.txt) do for /F "tokens=1*" %%a in ("%%A") do for %%i in (!i!) do (
       set "line=%%b" & for %%c in ("!line: =" "!") do set %%c & set "line=%%A"
       if %%i lss 6 (
          if "!line:~0,%%i!" equ "!spaces:~0,%%i!" (
             set "Ofs[%%i]=00%%a" & set "Ofs[%%i]=!Ofs[%%i]::=-!"
          ) else (
             set /A i-=2
             for %%i in (!i!) do if "!line:~0,%%i!" equ "!spaces:~0,%%i!" (
                set "Ofs[%%i]=00%%a" & set "Ofs[%%i]=!Ofs[%%i]::=-!"
             ) else (
                set /A i-=2
                for %%i in (!i!) do set "Ofs[%%i]=00%%a" & set "Ofs[%%i]=!Ofs[%%i]::=-!"
             )
          )
          set /A i+=2
       ) else (
          echo !Ofs[0]:~1!!Ofs[2]:~1!!Ofs[4]:~0,-1!,!DX!,!DY!,!Width!,!Height!,4
          set /A i-=2
       )
    )
    

    Output:

    00-walk-00-000,4,-22,39,54,4
    00-walk-00-001,1,-24,39,53,4
    00-walk-00-002,-1,-24,38,53,4
    00-walk-00-003,-3,-22,38,54,4
    00-walk-00-004,-2,-23,39,54,4
    00-walk-00-005,0,-24,39,54,4
    00-walk-01-000,8,-22,37,57,4
    00-walk-01-001,9,-24,37,57,4
    00-walk-01-002,8,-25,38,55,4
    00-walk-01-003,7,-24,38,55,4
    00-walk-01-004,8,-25,38,55,4
    00-walk-01-005,9,-24,37,57,4
    00-walk-02-000,3,-20,29,61,4
    00-walk-02-001,6,-21,29,61,4
    00-walk-02-002,9,-21,31,60,4
    00-walk-02-003,8,-22,38,59,4
    00-walk-02-004,7,-21,35,60,4
    00-walk-02-005,7,-21,27,61,4
    00-walk-03-000,3,-18,38,60,4
    00-walk-03-001,4,-18,33,62,4
    00-walk-03-002,6,-19,28,61,4
    00-walk-03-003,4,-18,28,60,4
    00-walk-03-004,5,-19,29,61,4
    00-walk-03-005,4,-18,33,62,4
    00-walk-04-000,-5,-19,40,54,4
    00-walk-04-001,-3,-19,39,57,4
    00-walk-04-002,0,-18,40,58,4
    00-walk-04-003,2,-17,38,58,4
    00-walk-04-004,0,-18,40,58,4
    00-walk-04-005,-3,-19,39,57,4
    01-faces-00-000,-1,-24,38,54,4
    01-faces-01-000,9,-24,38,56,4
    01-faces-02-000,8,-21,29,60,4
    01-faces-03-000,5,-18,31,62,4
    01-faces-04-000,-1,-19,40,57,4
    02-offense_move-00-000,7,-25,36,56,4
    02-offense_move-00-001,6,-21,29,54,4
    02-offense_move-00-002,5,-17,27,61,4
    02-offense_move-00-003,-1,-32,25,77,4
    02-offense_move-00-004,0,-23,25,51,4
    02-offense_move-01-000,12,-24,40,57,4