Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Check timestamp of a list of files  (Read 3938 times)

0 Members and 1 Guest are viewing this topic.

joec

  • Guest
Check timestamp of a list of files
« on: June 08, 2010, 08:04:08 AM »
Hi,
I am new in MS DOS scripting.

Trying to code a script to check and compare time stamp of a list of files. Below is the scripts, but is not working as expected. Need experts' help please.
Thanks a lot.

Line 6 works fine,
Line 7 doesn't work,
Line 8 print fName=null
Line 9 doesn't work,
Line 10 print ftime=null

1  REM===========================
2  SET DIRECTORY=C:\test
3  SET fileList=text1.txt,text2.txt,text3.txt
4  CD %directory% 
5  for %%f in (%fileList%) DO ( 
6   echo %%f found
7   set fName=%%f
8   echo fName=%fName%
9   for %%a in ( %%f ) do set ftime=%%~ta
10   echo ftime=%ftime%
 ... ) 

gpl



    Apprentice
  • Thanked: 27
    Re: Check timestamp of a list of files
    « Reply #1 on: June 08, 2010, 08:20:37 AM »
    Heres a hint, sorry I dont have enough time to rework your problem.

    This article describes 'delayed expansion' - it should sort you out
    http://www.computerhope.com/forum/index.php?topic=92017.0
    Graham

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Check timestamp of a list of files
    « Reply #2 on: June 08, 2010, 09:10:10 AM »
    In addition to delayed expansion, it might be wise to check if the file actually exists instead of assuming it does.

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set directory=c:\test
    set filelist=text1.txt,text2.txt,text3.txt
    cd /d %directory%
    for %%f in (%filelist%) do (
      if exist %%f (
        echo %%f found
        set fname=%%f
        echo fname=!fname!
        for %%a in ( %%f ) do set ftime=%%~ta
        echo ftime=!ftime!
      ) else ( echo %%f not found )

    endlocal

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    marvinengland



      Hopeful

      Thanked: 11
      Re: Check timestamp of a list of files
      « Reply #3 on: June 08, 2010, 08:43:50 PM »

      timestamp

      Joe,
       I could not get  else of if else to work.  I had to use a separate if for the missing files.


      C:\test>type sw4.bat
      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      set directory=c:\test
      cd /d %directory%
      for /f "delims=" %%i in (text.txt) do (
      set filename=%%i
      IF EXIST !filename! (
      echo file !filename! exists

      for %%a in ( %%i ) do set ftime=%%~ta
      echo ftime=!ftime!
      )
      IF not EXIST !filename! echo file !filename! missing
      )

      Output:

      C:\test>sw4.bat
      file text.txt exists
      ftime=06/08/2010 08:59 PM
      file text1.txt exists
      ftime=06/08/2010 07:04 PM
      file text2.txt exists
      ftime=06/08/2010 07:04 PM
      file text3.txt exists
      ftime=06/08/2010 07:04 PM
      file text4.txt exists
      ftime=06/08/2010 08:10 PM
      file text5.txt   missing
      file text77.txt   missing
      C:\test>
      USA