Home > Enterprise >  How to Extract and copy content from one file to another using batch script?
How to Extract and copy content from one file to another using batch script?

Time:05-21

I have a main.c file which content looks like

const uint8 array1[] =
{
0x00,0x12 , ....
....,
....,
....,
}

const uint8 array2[] =
{
0x00,0x12 , ....
....,
....,
....,
}

/* Data dictionary */

const arrayInfoType Images[SIZE] =
{
  /* [0] = */
  {
   /* .pDataSource = */
  (uint8*)array1,
   /* .DestAddr = */
  (uint32)   0x00000000,
  /* .DataLength = */
  (uint32)   0x00002cA7
  },
  /* [1] = */
  {
   /* .pDataSource = */
  (uint8*)array2,
   /* .DestAddr = */
  (uint32)   0x00017000,
  /* .DataLength = */
  (uint32)   0x00026342
  },
};

I wanted to copy only DataLength values to a header which looks like

#ifndef Img_included
#define Img_included

#define NO_OF_ARRAYS 2u
#define Size_Array0        0x000053A7    
#define Size_Array1        0x0002516C
extern  const arrayInfoType Images[SIZE];

#endif

how to overwrite SIZE_Array0 value in header with content of Data length of array 0 form C file and same thing for Array 1 using batch script .

I tried using FINDSTR but not getting proper output. Script what I was trying to use to extract content of C file .

FOR /F "tokens=*" %%A IN ('FINDSTR /N "uint32" "%InFile%"') DO (
CALL :RemovePrecedingWordA "%%A"
FOR /F "tokens=1 delims=:" %%B IN ('ECHO.%%A') DO (
MORE  %%B "%InFile%"> "%TempFile%"
FINDSTR /V "}" "%TempFile%">> "%OutFile%"
FOR /F "tokens=*" %%C IN ('FINDSTR "}" "%InFile%"') DO (
CALL :RemoveWordB "%%C"
IF EXIST "%TempFile%" DEL "%TempFile%"
GOTO :eof
)
)
)
GOTO :eof

CodePudding user response:

@ECHO Off
SETLOCAL
:: remove variables starting #
FOR  /F "delims==" %%b In ('set # 2^>Nul') DO SET "%%b="

FOR /F "tokens=1-7delims=),[ " %%g IN (q72311923.txt) DO (
 rem test for const uint8 name`
 IF /i "%%g%%h"=="constuint8" SET "#%%i=undefined"
 rem test for (uint8*)name,
 IF /i "%%g%%i"=="(uint8*" SET "name=#%%h"
 rem test for (uint32)   value - datalength only
 IF /i "%%g"=="(uint32" IF DEFINED grabdatalength CALL SET "%%name%%=%%h"
 rem test for   /* .DataLength = */
 IF /i "%%g%%h%%i%%j"=="/*.datalength=*/" (SET "grabdatalength=Y") ELSE (SET "grabdatalength=")
)
:: Now substitute within template

FOR /f "delims=" %%b IN (q72311923t.txt) DO (
 FOR /F "tokens=1-7delims= " %%g IN ("%%b") DO (
  SET "regurgitate=y" 
  IF /i "%%g"=="#define" (
   FOR /f "tokens=1*delims==#" %%q IN ('set #^|sort /r') DO IF DEFINED regurgitate (
    ECHO %%h|FINDSTR /L /i /x /c:"size_%%q" >NUL
    IF NOT ERRORLEVEL 1 ECHO %%g %%h        %%r&SET "regurgitate="
   )
  )
  IF DEFINED regurgitate echo %%b
 )
)
GOTO :eof

Assuming that the secon listing is of a template (q72311923t.txt) and the first (q72311923.txt)is your C file, and noting that the first mentions array1 and array2 but the template has array0 and array1 and also that I've used if /i throughout to make comparisons case-insensitive,

First I've cleared out all variables that start #

Next, examine each line of the c file and tokenise using ),[ and Space as the delimiter set, look for lines that contain significant strings.

If the line is const uint8 name then set the variable #name to undefined.

If the line is (uint8*)name then record the name in name.

If the line is (uint32) value then assign the value to the variable whose name is in name, but only if the previous line was a .datalength line.

and finally, check whether the line is a .datalength line and set a Boolean flag that can be tested on its runtime value defined/not defined.

Having established the names and values defined in the c file, interpret the template file using no delimiter to assign each line to %%b then again using just Space as a delimiter.

If the first token on the line is not #define then don't change the value of the regurgitate flag to echo the line verbatim.

If it is #define, then check whether the second token is exactly size_#? where #? is one of the #names recorded. If it's found then echo the replacement value and set 'regurgitate` to not defined

CodePudding user response:

This works:

@echo off
setlocal EnableDelayedExpansion

rem Read second file from stdin
< test2.txt (

rem Search for .DataLength data in first file
for /F "tokens=2" %%a in (test.txt) do (
   if defined .DataLength (
      rem Search for desired line in second file and replace data
      call :searchSize_Array_and_insert %%a
      set ".DataLength="
   )
   set "%%a=1"
)

rem Copy rest of lines
findstr "^"

)

goto :EOF


:searchSize_Array_and_insert data
set "line="
set /P "line="
for /F "tokens=1,2" %%a in ("%line%") do (
   set "var=%%b"
   if "!var:~0,-1!" equ "Size_Array" (
      echo %%a %%b        %1
      exit /B
   )
)
echo/%line%
goto :searchSize_Array_and_insert

Output:

#ifndef Img_included
#define Img_included

#define NO_OF_ARRAYS 2u
#define Size_Array0        0x00002cA7
#define Size_Array1        0x00026342
extern  const arrayInfoType Images[SIZE];

#endif
  • Related