2013-08-06 69 views
1

我正在編寫批處理文件以開始執行自動固件版本。修改批處理文件中.c文件中定義的版本號

目前我已經從TFS(Team Foundation Server)獲取代碼,然後構建它(該工具是基於eclipse的)。

但是,在構建代碼之前,我想增加版本號,該版本號存儲在文件version.h中。該文件的內容是:

/* $Header: /NG EM Controller Firmware Eclipse based/APPS/EM MAIN/version.h 2  4/25/13 10:19a user $ 
*/ 

#ifndef Version_h 
#define Version_h 

#define DSP_FW_VERSION 500 
#define DSP_FW_ID 22 
#define DESTINATION 1 

#endif 

/* 
* More comments, the number increases with each check in*./ 

行的#define DSP_FW_VERSION 500是我需要增加(在這種情況下,以501)就行了。

我對批處理文件很陌生,正在學習,因爲我走了,但這讓我很難過。我寧願不必複製每一行並修改一個我想要的,我只想操縱原文。

感謝您的任何幫助或指導。

回答

0
line=`grep -E '#define DSP_FW_VERSION' version.h` 
fst=$[line% *} 
snd=${line##* } 
nvno=`echo "${snd##* } + 1" | bc` 
sed "s/$line/$fst $nvno/" version.h 

第一行找到您要修改的行。你可能想要防守,並確保只有一條線被找到。第二行將行的開頭,最後一行的第三行分成兩個變量。第四行使用'bc'增加你的版本號。最後一個通過重組第一部分和遞增的第二部分來對version.h進行更改。有點醜,但它應該在bash中工作(你需要cygwin)。

1
@ECHO OFF 
SETLOCAL 
SET "sourcedir=." 
SET targetfile=version.h 
SET newversion=%1 
IF NOT DEFINED newversion ECHO require new version number as parameter&GOTO :EOF 
IF NOT EXIST "%sourcedir%\%targetfile%" ECHO %targetfile% not found&GOTO :eof 
DEL "%sourcedir%\%targetfile%_before_%newversion%" >NUL 
ren "%sourcedir%\%targetfile%" "%targetfile%_before_%newversion%" 

(
FOR /f "delims=" %%a IN (
    ' FINDSTR /n /R "$" "%sourcedir%\%targetfile%_before_%newversion%" ' 
) DO (
    SET "line=%%a" 
    SETLOCAL ENABLEDELAYEDEXPANSION 
    SET line=!line:*:=! 
    IF "!line:~0,23!"=="#define DSP_FW_VERSION " SET line=!line:~0,23!%newversion% 
    ECHO(!line! 
    endlocal 
) 
)>"%sourcedir%\%targetfile%" 

FC "%sourcedir%\%targetfile%" "%sourcedir%\%targetfile%_before_%newversion%" 
GOTO :EOF 

上述批處理旨在使用新版本號的參數運行。

它應該將現有的version.h重命名爲version.h_before_newversionnumber,並重新生成包括空行在內的version.h文件,但有一個例外是替換目標行。

末的FC命令僅僅是爲了之前和你的蓬勃預生產測試過程中的文件的版本後,比較...

2

試試這個(純批):

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION 
SET "HFile=file" 
SET "search=#define DSP_FW_VERSION" 

FOR /f %%a IN ('^<"%HFile%" find /c /v ""') DO SET /a lines=%%a 
< "%HFile%" (
FOR /l %%a IN (1,1,%lines%) DO (
    SET "line=" 
    SET /p "line=" 
    IF NOT "!line!"=="" IF NOT "!line:%search%=!"=="!line!" (
      SET /a replace=!line:%search%=!+1 
      SET "line=%search% !replace!" 
    ) 
    ECHO(!line! 
))>"%HFile%.new" 
MOVE /y "%HFile%.new" "%HFile%" 
TYPE "%HFile%" 
+0

非常感謝!這正是我正在尋找的東西:D – sensslen

相關問題