Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:53:12+00:00 2026-06-11T15:53:12+00:00

I’m trying to run a batch script that will find the last date modified

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:53:13+00:00Added an answer on June 11, 2026 at 3:53 pm

    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%
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.