The task could be done much easier with the following code:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo ------------------------------
echo -- Starting a run of resgen --
echo ------------------------------
set "resourcesPath=%~1Resources\"
set "configuration=%~2"
set "platform=%~3"
set "landingPath=%~1bin\%configuration%\Resources\"
if exist "%landingPath%" echo "%landingPath%" exists.& goto ProcessFiles
mkdir "%landingPath%"
if exist "%landingPath%" echo "%landingPath%" created.& goto ProcessFiles
echo ERROR: "%landingPath%" could not be created!& goto EndBatch
:ProcessFiles
for %%I in ("%resourcesPath%Strings.*-*.txt") do (
set "RunResgen=1"
for %%J in ("%landingPath%%%~nI.resources") do if "%%~tJ" == "%%~tI" set "RunResgen="
if defined RunResgen "%resourcesPath%resgen.exe" "%%I" "%landingPath%%%~nI.resources"
)
:EndBatch
endlocal
The outer FOR loop searches in the directory Resources
for non-hidden files matching the wildcard pattern Strings.*-*.txt
. There could be used also the wildcard pattern Strings.*.txt
or the pattern Strings.??-??.txt
.
For a found text file there is first defined the environment variable RunResgen
with string value 1
whereby the value does not matter.
Next is executed one more FOR processing the resource file in landing Resources
directory. If that file does not exist at all, %%~tJ
expands to an empty string which means the IF condition compares ""
with something like "10.11.2021 19:00"
and so the condition is not true. If the appropriate .resources
file exists, its last modification time is compared with the last modification time of the .txt
file. If the two file time stamps are equal, the environment variable RunResgen
is undefined for the next IF condition because of no need to run resgen.exe
for this pair of text and resources files.
The second IF condition checks the existence of the environment variable RunResgen
and if this variable still exists because of .resources
file does not exist at all or has not the same last modification time as the .txt
file, the executable resgen.exe
is executed with the two file names.
Please note that the file date/time format depends on the country setting of the used account. On my Windows machine the date/time format is dd.MM.yyyy hh:mm
and for that reason the simple string comparison works with these sixteen characters plus the two surrounding quotes taken also into account on comparing the two strings.
resgen.exe
must create the .resources
file with last modification date/time explicitly set to last modification date/time of the .txt
file or this code does not work at all.
There is usually used on Windows the archive file attribute do determine if a source file was modified since last processing it. But I suppose this is not possible here as one .txt
file could be used for multiple .resources
files for multiple configurations and platforms (wherever the environment variable platform
is used in real batch file).
Well, the main code could be even more optimized by using just following single line:
for %%I in ("%resourcesPath%Strings.*-*.txt") do for %%J in ("%landingPath%%%~nI.resources") do if not "%%~tJ" == "%%~tI" "%resourcesPath%resgen.exe" "%%I" "%landingPath%%%~nI.resources"
The executable resgen.exe
is executed on .resources
file not existing at all or its last modification date/time is different to the last modification date/time of the .txt
file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
mkdir /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of the operator &
to execute always a GOTO after an ECHO.