Check created time for files with batch?

January 3, 2011 at 08:25:48
Specs: Windows XP
Hi all,

I want to check a created time for some files with batch.
Actually I want to add here to check when the file was created.

@echo off

for %%n in (2, 3) do (ping -n 2 192.168.1.%%n

if errorlevel 1 ( @echo %%n NO PING>>test.txt) else (
if not exist \\192.168.1.%%n\c$\test\file*.pdf (@echo %%n ok-->no >>test.txt) else (
@echo %%n ok--yes >>test.txt)))

I tried but earn only the current date. Have you any idea how to do?



See More: Check created time for files with batch?

Report •


#1
January 3, 2011 at 12:36:36
If you want to check dates you need to convert it to Julian Dates. Note the subrutine.

@echo off
setLocal EnableDELAYedExpansion

::Set the Compair Date to Todays Date.
Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%
Call :jdate %Year% %Month% %Day%
Set /a CompairDate=%jdate%
Set FileDate=1

for /f "tokens=*" %%a in ('dir C:\ /b') do (
  :: Extract the files date.
  set T=%%~Ta
  set FileDay=!T:~3,2!
  set FileMonth=!T:~0,2!
  set FileYear=!T:~6,4!
  call :jdate !FileYear! !FileMonth! !FileDay!
  set /a FileDate=!Jdate!
  
  Echo File Date:!FileDate!  Compair Date:!CompairDate!
  Echo The Files is !CompairDate!-!FileDate! days old.
  Pause
Goto :EOF


:JDate
:: This subroutine converts date to Julian

  Set MM=%2
  Set DD=%3
  If %MM:~0,1% EQU 0 Set MM=%MM:~1%
  If %DD:~0,1% EQU 0 Set DD=%DD:~1%

  Set /A Month1 = ( %MM% - 14 ) / 12
  Set /A Year1  = %1 + 4800
  Set /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
  Set Month1=
  Set Year1=
Goto:EOF

This converts Dates to a number that represents the number days in the date so that you can do date math and comparisons on the date.


Report •

#2
January 3, 2011 at 13:04:41
dir /tc test.txt


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
January 4, 2011 at 08:49:19
Hi,
Thank you, but I want to check the date of file *. pdf, but I do not understand how to check with your explanations. Would you specified. Thank you in advance.

Report •

Related Solutions

#4
January 4, 2011 at 11:39:58
Need more details. What are you checking the date against? Is it today's date, date of another file or what? If it is against today's date then how old are you checking for. Are you checking to see if the file is 1 day old or 1 year old? Also, what do you want to do if a condition is met? Do you want to pop an error message, delete the file or copy the file?

Please provide more details.


Report •

#5
January 4, 2011 at 21:42:08
This is not knee surgery.

dir /tc *.pdf


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
January 5, 2011 at 08:41:46
Hi all,
The aim of the batch file is to check whether there is a connection to 192.168.1.2, if there is to be noted that there is, after thet check whether test *. pdf exist and noted to test.txt. This works properly, but I want to add on what date the test*.pdf was created and write the date of creation in test.txt, but I do not know how. Thank you.

Report •

#7
January 5, 2011 at 10:30:55
Create your log with:

echo %date% > log


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
January 5, 2011 at 11:04:32
NOT TESTED...

@echo off
setlocal ENABLEEXTENSIONS

for %%n in (2, 3) do (
  set CheckForFiles=\\192.168.1.%%n\c$\test\file*.pdf
  set LogFile=c:\test.txt

  ping -n 2 192.168.1.%%n
  if errorlevel 1 (
    echo %%n NO PING >>%LogFile%
  ) else (if not exist %CheckForFiles% (
    echo %%n ok-->no >>%LogFile%
  ) else (
    echo %%n ok--yes >>%LogFile%
    call :PrintFileDates %CheckForFiles% %LogFile%)
  )
)
goto :EOF

:PrintFileDates
for /f "tokens=*" %%a in ('dir %1 /b /tc') do (
  set T=%%~Ta
  echo %%a Date: !T! >> %2
)
goto :EOF


Report •


Ask Question