我有一个批处理文件,用于检查是否存在先前编译的 exe 文件。如果不存在,它将重新编译,否则它将检查自创建 exe 文件以来是否有任何文件被修改过。如果发现修改过的文件,它将重新编译。然后它检查目录,跟踪文件内容的名称和时间戳。
我想知道如何做到这一点,这样我就不必手动列出:
file[0]=
file[1]=
file[2]=
etc.
现在我已经有了一个可以随时修改的列表,并且可以工作,但它的修改方式如下:
set SOURCES=!SOURCES! "%%F"
我的时间戳列表如下:
set LIST=!LIST! %FILE_TIMESTAMP_FORMAT%
我已经检查了很多来源,没有一个是动态的,或者对我来说根本不起作用。还有谁知道为什么格式无法编辑echo
。
@echo off
setlocal enabledelayedexpansion
cd /d "C:\Users\ealos\OneDrive\Desktop\C++ Project"
echo Current directory: %CD%
set SOURCES=
set FILES_CHANGED=false
set LIST=
rem Check if cppgame.exe exists and get its timestamp
if exist cppgame.exe (
for %%F in (cppgame.exe) do (
set EXE_TIMESTAMP=%%~tF
)
echo cppgame.exe last modified: !EXE_TIMESTAMP!
rem Convert cppgame.exe timestamp to comparable format
set EXE_TIMESTAMP_FORMAT=!EXE_TIMESTAMP:~6,4!!EXE_TIMESTAMP:~0,2!!EXE_TIMESTAMP:~3,2!!EXE_TIMESTAMP:~11,2!!EXE_TIMESTAMP:~14,2!
echo EXE Timestamp Format: !EXE_TIMESTAMP_FORMAT!
) else (
echo cppgame.exe does not exist or was just created.
set FILES_CHANGED=true
)
rem Loop through all .cpp files and check if any are newer than cppgame.exe
for %%F in (*.cpp) do (
echo -----------
rem Add the .cpp file to the SOURCES variable for compilation
set SOURCES=!SOURCES! "%%F"
rem Get the timestamp of the current .cpp file
set FILE_TIMESTAMP=%%~tF
rem Convert .cpp file timestamp to comparable format
set FILE_TIMESTAMP_FORMAT=!FILE_TIMESTAMP:~6,4!!FILE_TIMESTAMP:~0,2!!FILE_TIMESTAMP:~3,2!!FILE_TIMESTAMP:~11,2!!FILE_TIMESTAMP:~14,2!
echo Checking: %%F (Modified: !FILE_TIMESTAMP!)
echo File Timestamp Format: (!FILE_TIMESTAMP_FORMAT!)
rem Add the formatted timestamp to the LIST for comparison
set LIST=!LIST! "%FILE_TIMESTAMP_FORMAT%"
echo list: %LIST%
)
rem Now compare each timestamp in LIST with cppgame.exe's timestamp
echo %LIST%
for /f "tokens=1, delims==" %%a in (%LIST%) do (
echo Comparing: %%a with !EXE_TIMESTAMP_FORMAT!
if %%a GTR %EXE_TIMESTAMP_FORMAT% (
set FILES_CHANGED=true
echo File has changed, setting FILES_CHANGED=true
)
echo EXE Timestamp Format: !EXE_TIMESTAMP_FORMAT!
echo File Timestamp Format: %%a
)
rem If no files have changed, just run the existing executable
if "!FILES_CHANGED!"=="false" (
echo No files changed, running existing executable...
cppgame.exe
pause
exit /b
)
rem If no .cpp files are found, exit
if "%SOURCES%"=="" (
echo No .cpp files found in the directory!
pause
exit /b
)
rem Compile all .cpp files into a single executable (cppgame.exe)
echo Compiling...
g++ %SOURCES% -o cppgame.exe
rem Check if compilation was successful
if %ERRORLEVEL% NEQ 0 (
echo Compilation failed!
pause
exit /b
)
rem Run the compiled executable
echo Running cppgame...
cppgame.exe
rem Pause to keep the console open after execution
pause