Tenho um arquivo em lote que verifica se um arquivo exe compilado anteriormente existe. Se não, ele irá recompilar, caso contrário, ele irá verificar se algum arquivo foi modificado desde a criação do arquivo exe. Se arquivos modificados forem encontrados, ele irá recompilar. Ele então verifica o diretório, mantendo o controle dos nomes e carimbos de data/hora para o conteúdo do arquivo.
Estou pensando em como fazer isso para não ter que listar manualmente:
file[0]=
file[1]=
file[2]=
etc.
Agora eu já tenho uma lista aqui que pode ser modificada sempre que quiser e funciona, mas ela é modificada assim:
set SOURCES=!SOURCES! "%%F"
e minha lista de timestamps é assim:
set LIST=!LIST! %FILE_TIMESTAMP_FORMAT%
Já verifiquei muitas fontes, e nenhuma delas é dinâmica, ou simplesmente não funciona para mim. Alguém sabe por que o formato não será echo
editado?
@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
Acredito que o seguinte atenderia às suas necessidades:
O nome do arquivo de log se adapta perfeitamente à minha configuração de teste.
A rotina
existing
define#filename
o carimbo de data/hora de cada arquivo encontrado nafilesrequired
lista, defineabort
sey
algum estiver faltando e grava a lista de nomes de arquivos/carimbos de data/hora emlogfile.new
.Então verifique se o novo arquivo de log é o mesmo que o antigo. Se não, ou se um dos arquivos estiver faltando, então recompile, redefina as #variáveis com as novas versões e reescreva o arquivo de log.
Em seguida, execute o jogo e limpe todos os arquivos indesejados.