0

I need to create a resource file using resgen.exe only if the file is edited. I've found a way to do it, and i need to loop through all the Language available.

This is my script.

@echo on

echo ------------------------------
echo -- Starting a run of resgen --
echo ------------------------------

Set resourcesPath=%~1Resources\
Set configuration=%~2
Set platform=%~3

set landingPath=%~1bin\%configuration%\Resources\

echo %landingPath%

IF exist %landingPath% ( echo %landingPath% exists ) ELSE ( mkdir %landingPath% && echo %landingPath% created)

set obj[0].Resource="%landingPath%Strings.en-GB.resources"
set obj[0].Text="%resourcesPath%Strings.en-GB.txt"
set obj[1].Resource="%landingPath%Strings.ru-RU.resources"
set obj[1].Text="%resourcesPath%Strings.ru-RU.txt"
set obj[2].Resource="%landingPath%Strings.es-ES.resources"
set obj[2].Text="%resourcesPath%Strings.es-ES.txt"


FOR /L %%i IN (0 1 2) DO  (

    for %%x in ("%%obj[%%i].Text%%") do set date_of_filenameTxt=%%~tx
    for %%x in ("%%obj[%%i].Resource%%") do set date_of_filenameRes=%%~tx

    ECHO %date_of_filenameTxt:~0, 16%
    ECHO %date_of_filenameRes:~0, 16%

    IF "%date_of_filenameTxt:~0, 16%" == "%date_of_filenameRes:~0, 16%" call :same

    call :notsame

    :same
    (ECHO "No Copy for the :" %%obj[%%i].Text%% )
    call :end

    :notsame
    "%resourcesPath%resgen.exe" %%obj[%%i].Text%% %%obj[%%i].Resource%%

    :end

)

The problem is on getting the string from the obj[], how should be the sintax? i've found if i do as below, it works.

call echo resource = %%obj[0].Resource%%
Diego
  • 5
  • 2
  • Do not `call` a label inside of the loop. Put the labels outside the loop, and why the spaces in the variable substitution strings? – Gerhard Nov 10 '21 at 09:57
  • The first thing I would advise, right at the top of the script, is to validate that your script has received at least three arguments, and that those arguments are valid strings for the file and directory objects they are supposed to represent. If those arguments are not received, and/or are not valid, then your code should perform some action based upon that, before proceeding with the rest of the code. BTW, you have a typo, additional trailing double-quote character on line `17` – Compo Nov 10 '21 at 10:34
  • thanks, @Gerhard i will move it outside. can you be more specific on the variable substitution are you referring to? – Diego Nov 10 '21 at 11:25
  • the issue is that i cannot retrive the path from the obj. if i do echo %%obj[%%i].Text%% ==>%obj[0].Text% if i do call echo resource = %%obj[0].Resource%% ==> "c:\xx\yy" how should i format that to feed the set? – Diego Nov 10 '21 at 14:14

1 Answers1

-1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143