我试图运行批处理脚本,该脚本将查找特定文件的最后修改日期。我使用类似于下面的脚本的内容:批处理脚本获取远程文件的最后修改日期
@echo off
set mainDir=\\subdomain.myintranet.net\c$
set txtFile=%mainDir%\tmp.txt
set txtFile2=%mainDir%\tmp2.txt
set "bodyText=^<p^>Hello,^<br /^>^<br /^>"
if exist %txtFile% (
for %%X in (%txtFile%) do (set fileDate=%%~tX)
set "bodyText=%bodyText%tmp.txt file updated as of %fileDate%^<br /^>"
) else (
set "bodyText=%bodyText%Warning: Issues finding %txtFile%.^<br /^>"
)
if exist %txtFile2% (
for %%X in (%txtFile2%) do (set fileDate2=%%~tX)
set "bodyText=%bodyText%tmp2.txt file updated as of %fileDate2%^<br /^>"
) else (
set "bodyText=%bodyText%Warning: Issues finding %txtFile2%.^<br /^>"
)
set "bodyText=%bodyText%^</p^>"
echo %bodyText% > %mainDir%\mylog.txt
测试此示例代码的时候,我发现它有时工作,有时没有。发生什么事是它找到了该文件,但fileDate
变量回到空白。
我也尝试在脚本的开头放置一个空变量fileDate=
,但那不起作用。
如果很重要:我已将批处理脚本连接到每日运行的SQL Server 2000作业。批处理文件和日志文件驻留在数据库所在的服务器上,但批处理脚本完全限定文件位置,如我在示例中所示(这是因为如果我想从我的桌面运行批处理文件,它将检查/更新正确的文件)。
由于提前, 约瑟夫
编辑:
输出应该是这样的:
Hello,
tmp.txt file updated as of 9/19/2012 2:24 PM
tmp2.txt file updated as of 9/19/2012 10:02 AM
虽然我有时得到的是:
Hello,
tmp.txt file updated as of
tmp2.txt file updated as of
而且其他时间我米ay得到:
Hello,
tmp.txt file updated as of 9/19/2012 2:24 PM
tmp2.txt file updated as of
弄清楚发生了什么问题很混乱。
感谢您的解释!在阅读你的解释和变量的延迟扩展信息之后,这更有意义。并感谢关于简化代码的说明。我对我的制作脚本做了非常类似的更改,并且效果很好。再次感谢! –