首页 > 解决方案 > 如果文件被编辑,我如何在 bat 文件中调用 resgen?

问题描述

仅当文件被编辑时,我才需要使用 resgen.exe 创建资源文件。我找到了一种方法,我需要遍历所有可用的语言。

这是我的脚本。

@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

)

问题在于从 obj[] 获取字符串,sintax 应该如何?我发现如果我按照以下方式进行操作,它会起作用。

call echo resource = %%obj[0].Resource%%

标签: batch-file

解决方案


使用以下代码可以更轻松地完成任务:

@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

外部FOR循环在目录中搜索Resources与通配符模式匹配的非隐藏文件Strings.*-*.txt。也可以使用通配符模式Strings.*.txt或模式Strings.??-??.txt

对于找到的文本文件,首先RunResgen使用字符串值定义环境变量1,该值无关紧要。

接下来再执行一次FOR处理登陆Resources目录下的资源文件。如果该文件根本不存在,则%%~tJ扩展为一个空字符串,这意味着IF条件""与类似的内容进行比较"10.11.2021 19:00",因此条件不成立。如果存在相应的.resources文件,则将其最后修改时间与文件的最后修改时间进行比较.txt。如果两个文件时间戳相等,RunResgen则为下一个IF条件未定义环境变量,因为不需要resgen.exe为这对文本和资源文件运行。

第二个IF条件检查环境变量是否存在RunResgen,如果该变量由于.resources文件根本不存在或与文件的最后修改时间不同而仍然存在,则使用两个文件名执行.txt可执行文件。resgen.exe

请注意,文件日期/时间格式取决于所用帐户的国家/地区设置。在我的 Windows 机器上,日期/时间格式是dd.MM.yyyy hh:mm,因此,简单的字符串比较适用于这 16 个字符加上两个周围的引号,在比较两个字符串时也考虑在内。

resgen.exe必须创建.resources文件,最后修改日期/时间明确设置为文件的最后修改日期/时间,.txt否则此代码根本不起作用。

在 Windows 上通常使用存档文件属性来确定自上次处理后源文件是否被修改。但我想这是不可能的,因为一个.txt文件可以用于.resources多个配置和平台的多个文件(无论环境变量platform在实际批处理文件中使用的位置)。

好吧,主要代码可以通过使用以下单行来进一步优化:

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"

可执行文件在根本不存在的文件resgen.exe上执行,.resources或者其最后修改日期/时间与.txt文件的最后修改日期/时间不同。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • mkdir /?
  • set /?
  • setlocal /?

另请参见使用 Windows 批处理文件执行多条命令的单行,以了解操作员在ECHO之后&始终执行GOTO的说明。


推荐阅读