我正在修改我們在其中呼叫@make 函式的批處理檔案。我們想在批處理檔案中添加一個腳本來檢查外部頭檔案,找到帶有日期資訊(APP_VERSION_DATE)的行并用新的日期資訊更新那里的資訊(我想出了如何用批處理獲取 windows 日期資訊,這是沒什么大不了)
我知道要遵循哪些步驟,但是批處理語法對我來說完全違反直覺,我被卡住了。
這些是我想遵循的步驟:
1- 逐行瀏覽 app_version.h 檔案(對于 /f)
2- 查找帶有字串 APP_VERSION_DATE 的行(如果 findstr ...)
3- 洗掉除 APP_VERSION_DATE 之外的所有內容
4- 將日期資訊連接到 APP_VERSION_DATE,如 APP_VERSION_DATE "23-05-2022"
5- 每隔一行重復一次
6-管道資訊一個新的頭檔案。
7-洗掉頭檔案
8- 將新標題行重命名為舊標題行。
set strToFind="app_version_date"
set result="Not Found"
for /f "tokens=2 delims=[]" %%A in ('findstr %strToFind% %filename%') do (
set result=%%A
if defined result (
if %result%==this is something
echo hurra this is it
) ELSE echo
)
這就是我現在所處的位置,顯然我離做我想做的事情還很遙遠。
我可以制作一個程式,可以在檔案中找到給定的字串并更改它,但在這種情況下,我想找到包含我正在搜索的字串的行,洗掉其余的并修改它。我想找到字串所在的行并修改它,而不是字串本身。這僅僅是因為日期資訊如下所示;
#define APP_VERSION_DATE [2022-05-16 12:13]
不會是靜態的,并且隨著每次編譯嘗試而不斷變化。
我有這樣的東西,但這離我想做的太遠了。
任何幫助都會很棒!提前致謝
uj5u.com熱心網友回復:
替換頭檔案 app_version.h 中的日期/時間
此任務可以使用以下注釋的批處理檔案:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "HeaderFile=app_version.h"
if not exist "%HeaderFile%" exit /B 20
rem Get current local date/time in format [yyyy-MM-dd HH:mm].
for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "AppVersionDate=[%%G-%%H-%%I %%J:%%K]" & goto UpdateHeaderFile
rem Let FINDSTR output all lines of the header file with a line number and
rem a colon at the beginning for processing really all lines including the
rem empty lines in the header file and output all lines without the line
rem number and the colon with exception of the line containing the string
rem #define APP_VERSION_DATE which is ignored and instead is output a line
rem defined here with the local date/time determined before. All lines output
rem by the loop are written into a newly created temporary header file.
:UpdateHeaderFile
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%HeaderFile%" 2^>nul') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
if "!Line:#define APP_VERSION_DATE=!" == "!Line!" (
echo(!Line:*:=!
) else (
echo #define APP_VERSION_DATE %AppVersionDate%
)
endlocal
))>"%HeaderFile%.tmp"
rem Replace the original header file with the temporary header file.
if exist "%HeaderFile%.tmp" move /Y "%HeaderFile%.tmp" "%HeaderFile%" >nul
rem Delete the temporary header file if the command line above failed
rem because of the original header file is read-only or write-protected
rem or currently opened by an application with shared access denied.
if exist "%HeaderFile%.tmp" del "%HeaderFile%.tmp"
endlocal
環境變數HeaderFile可以用絕對路徑或相對路徑來定義。
請閱讀我在午夜后時間設定錯誤的答案中使用 ROBOCOPY 獲取當前日期/時間一章,以了解第一個命令列的說明。for /F
請閱讀我關于如何逐行讀取和列印文本檔案內容的答案?它詳細描述了第二個for /F回圈,對附加的IF條件進行了小修改,以將包含字串的行替換為#define APP_VERSION_DATE當前日期/時間。
要了解使用的命令及其作業原理,請打開命令提示符視窗,在其中執行以下命令,并仔細閱讀每個命令顯示的所有幫助頁面。
del /?echo /?endlocal /?exit /?findstr /?goto /?if /?move /?rem /?set /?setlocal /?
有關無條件命令運算子的說明,另請參見使用 Windows 批處理檔案的單行多命令&。
使用日期/時間創建頭檔案 current_date_time.h
app_version.h如果檔案包含以下行的任何位置,則可以更輕松地完成任務:
#include "current_date_time.h"
在這種情況下,批處理檔案可能只是:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do echo #define APP_VERSION_DATE [%%G-%%H-%%I %%J:%%K]>"current_date_time.h"& goto EndBatch
:EndBatch
endlocal
此批處理檔案始終current_date_time.h只使用一行創建新檔案:
#define APP_VERSION_DATE [2022-05-23 18:48]
并且這個單一的前處理器宏定義行包含在編譯成app_version.h.
使用當前日期/時間定義前處理器宏 APP_VERSION_DATE
每個 C/C 編譯器都有一個在命令列上定義前處理器宏的選項,并且可以在命令列上多次使用該選項來定義多個前處理器宏。
例如參見:
- GNU gcc/g : 控制前處理器的選項解釋區分大小寫的選項
-D - Microsoft C/C : /D(前處理器定義)
So it is possible to define an environment variable with the current date/time in the wanted format with the single command line below and reference this environment variable value on running the C/C compiler with the appropriate option.
@set "AppVersionDate=" & for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @if not defined AppVersionDate set "AppVersionDate=[%%G-%%H-%%I %%J:%%K]"
GNU gcc/g 稍后將-D "APP_VERSION_DATE=%AppVersionDate%"與 Microsoft C/C 編譯器一起運行,/D "APP_VERSION_DATE=%AppVersionDate%"作為編譯 C/C 源代碼檔案的選項之一。
還有預定義的宏__DATE__和__TIME__:
- GNU gcc/g : 預定義宏
- Microsoft C/C : 預定義的宏
還可以搜索有關環境變數的資訊,該變數SOURCE_DATE_EPOCH可以控制由 C/C 編譯器本身添加到生成的二進制檔案的時間戳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482629.html
