Today I spent quite some time figuring out how to set up a batch script to run before a bulid in a CooCox IDE.
The script I wrote was to automatically change the bulid number of a program I am writing for my diploma thesis. What the batch script does is it reads a number from a header file, increments it by 1 and writes it back to the same file. The script itself is a derivate of this code (I got it from here):
REM you need this to set and read a variable inside
REM a parethetical structure such as a FOR loop
REM This is the file we are going to alter
REM Use temp file
REM delete if already exists
REM so we can use append operator for all output
if exist "%filename%.temp" del "%filename%.temp"
REM For each line in the file...
REM ...using FOR alone to parse the file skips blank lines so we...
REM ...parse the output (note single quotes) of...
REM running TYPE on the file and piping the output through FINDSTR...
REM ...with the /n switch (this adds a line number and a colon at the start of each line)
REM the FINDSTR search string is ".*" (find any characters including cr/lf)
REM Split into 2* tokens, the asterisk means %%R is the entire remainder of the line
REM delimiter being the colon thus...
REM The number is token 1, %%Q (discarded, along with the colon)
REM The original source file line is token 2, %%R
REM note we escape the pipe character with a caret ^ in the FOR dataset block
for /f "tokens=1,2* delims=:" %%Q in ('type "%filename%" ^| findstr /n ".*"') do (
REM if token 2 is null then the line is blank so we echo a blank line to the temp output file
if "%%R"=="" echo. >> "%filename%.temp"
REM This flag gets set to 1 if we have a line that needs changing
REM Split the line into 3 tokens with white space the delimiter
for /f "tokens=1-3 delims= " %%A in ("%%R") do (
REM test if an increment needs to happen and set the flag if it does
if "%%B"=="VERSION_MAJOR_NUMBER" set incflag=1
if "%%B"=="VERSION_MINOR_NUMBER" set incflag=1
REM %%C is the number
REM If the line contains a number to increment...
if !incflag! equ 1 (
REM do it...
set /a num=%%C+1
REM info msg to console
echo Incrementing %%B from %%C to !num!
REM write the altered line to file
echo %%A %%B !num!>>"%filename%.temp"
REM the line is a nonblank one that simply needs copying
) else (
echo %%R >> "%filename%.temp"
REM Match those parentheses!
REM delete original file
REM rename temp file to original file name
ren "%filename%.temp" "%filename%"
REM Original Author: Salmon Trout
REM Original Source: http://www.computerhope.com/forum/index.php?topic=118896.5
In my case I only changed the script to increase a different variable. The script worked as it should on clicking / running it manually. However only setting the script to be executed before the bulid in CooCox didn’t result in it working as it should. Only after a few hours of searching around and trying different things I found the solution. To make the batch script run correctly, you have to add the following two lines at the start of the batch script:
The two lines supposedly set the working environment and working directory. And the script works after adding them – at least for me 🙂