简体   繁体   中英

Batch Script Get Last Modified Date of Remote File

I'm trying to run a batch script that will find the last date modified of a particular file. I'm using something similar to the following script:

@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

Testing this example code out, I find that it sometimes works, and sometimes not. What happens is that it the file is found, but the fileDate variable is coming back blank.

I have also tried putting an empty variable fileDate= at the beginning of the script, but that didn't work.

If it matters: I have the batch script connected to a SQL Server 2000 Job that runs daily. The batch file and log file reside on the same server that the database does, however the batch script fully qualifies the file location as I showed in my example (this is because if I want to run the batch file from my desktop, it will check/update the correct files).

Thanks in advance, Joseph

EDIT:

The output should look like:

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

Whereas what I sometimes get is:

Hello,

tmp.txt file updated as of 
tmp2.txt file updated as of 

And other times I may get:

Hello,

tmp.txt file updated as of 9/19/2012 2:24 PM 
tmp2.txt file updated as of 

It's confusing to figure out what's going wrong.

Groan...

This has got to be the most common bug with Windows batch development. You are attempting to expand a variable that was set within the same code block. But the variable is expanded when the entire code block is parsed, so you are getting the value that existed prior to the block of code being executed. That obviously doesn't work.

Type HELP SET or SET /? from the command prompt and read the section dealing with Delayed Expansion. That shows you one way to solve the problem.

But in your case you don't need the variable at all, so you don't need delayed expansion. Simply use the FOR variable directly when you append to your bodyText:

@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 "bodyText=%bodyText%tmp.txt file updated as of %%~tX^<br /^>"
) else (
    set "bodyText=%bodyText%Warning: Issues finding %txtFile%.^<br /^>"
)

if exist %txtFile2% (
    for %%X in (%txtFile2%) do set "bodyText=%bodyText%tmp2.txt file updated as of %%~tX^<br /^>"
) else (
    set "bodyText=%bodyText%Warning: Issues finding %txtFile2%.^<br /^>"
)

set "bodyText=%bodyText%^</p^>"

echo %bodyText% > %mainDir%\mylog.txt


EDIT

There is a lot more room for simplification that should make the code easier to maintain. Since you are preparing an HTML file, there is no reason to worry about additional line breaks, so you don't have to put all of the text into one variable. You can use multiple ECHO statements. I would structure your code something like the following (untested, but the concepts are sound):

@echo off
setlocal
set "mainDir=\\subdomain.myintranet.net\c$"
set "br=^<br /^>"
set "p=^<p^>"
set "/p=^</p^>"
>"%mainDir%\mylog.txt" (
  echo %p%Hello,%br%%br%"
  for %%F in (
    "%mainDir%\tmp.txt"
    "%mainDir%\tmp2.txt"
  ) do (
    if exist %%F (
      echo %%~nxF file updated as of %%~tF%br%"
    ) else (
      echo Warning: Issues finding %%~nxF.%br%"
    )
  echo %/p%
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM