首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果对文件进行了编辑,如何在bat文件中调用resgen?

如果对文件进行了编辑,如何在bat文件中调用resgen?
EN

Stack Overflow用户
提问于 2021-11-10 08:57:41
回答 1查看 75关注 0票数 0

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

这是我的剧本。

代码语言:javascript
复制
@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[]获得字符串,新税应该是怎样的?我发现如果我按下面的方式做,它就会起作用。

代码语言:javascript
复制
call echo resource = %%obj[0].Resource%%
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-10 18:00:51

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

代码语言:javascript
复制
@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,因此该值并不重要。

接下来,再执行一个,用于Resources目录中处理资源文件。如果该文件根本不存在,则%%~tJ扩展为一个空字符串,这意味着如果条件将""与类似于"10.11.2021 19:00"的东西进行比较,则条件是不正确的。如果存在适当的.resources文件,则将其最后修改时间与.txt文件的最后修改时间进行比较。如果两个文件时间戳相等,环境变量RunResgen将不为下一个(如果条件)定义,因为不需要为这对文本文件和资源文件运行resgen.exe

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

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

resgen.exe必须创建.resources文件,并将最后修改日期/时间显式地设置为.txt文件的最后修改日期/时间,否则此代码根本无法工作。

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

通过使用以下一行代码,可以对主代码进行更多的优化:

代码语言:javascript
复制
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批处理文件使用多条命令的单行,以了解操作符&回显之后始终执行GOTO的说明。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69910301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档