Correctly setting up prebulid batch script in CooCox
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
@echo off REM you need this to set and read a variable inside REM a parethetical structure such as a FOR loop setlocal enabledelayedexpansion REM This is the file we are going to alter set filename=version.h 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 set incflag=0 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 del "%filename%" 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:
1 2 3 |
%~d0 cd %~dp0 |
The two lines supposedly set the working environment and working directory. And the script works after adding them – at least for me 🙂
Leave a Reply