Solved

Batch file to check to execute a command if a file timestamp is older than 5 days

Posted on 2006-11-06
10
757 Views
Last Modified: 2012-06-27
Hi,

Anyone know how to write a batch file that will execute 'shutdown -r -t 60' if the date created (or modified) stamp on a text file is older than 5 days?

I basically want to check to see if a workstation has rebooted in 5 days and if not, force a reboot.  

Thank you.
0
Comment
Question by:AFAIT
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 6

Assisted Solution

by:GnarOlak
GnarOlak earned 75 total points
ID: 17883379
I don't know of any way to test for a files age in windows without using a third party program.

I've incouded a batch file that might work for you.  When run once a day it keeps a series of tag files and when the fifth file exists it's time to shutdown.  It's fairly straight forward.

@echo off

if exist log.5 goto SD

if exist log.4 copy /Y log.4 log.5
if exist log.3 copy /Y log.3 log.4
if exist log.2 copy /Y log.2 log.3
if exist log.1 copy /Y log.1 log.2
if not exist log.1 echo >log.1

goto END

:SD
del /F log.*
echo >log.1
shutdown -r -t 60

:END
0
 

Author Comment

by:AFAIT
ID: 17883426
Ah, yes, I have thought about this too actually...but it means I need to setup a scheduled task or something on each workstation which won't work in my particular instance.  Good idea though.

Any other ideas?  Your help is greatly appreciated.
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 17883627
0
 

Author Comment

by:AFAIT
ID: 17883683
Sorry, I may be asking a real silly quesiton here but how does that work?  I guess I dont really understand that posting...can you provide an example or more explanation?  I really appreciate your help.
0
 
LVL 4

Assisted Solution

by:hclgroup
hclgroup earned 125 total points
ID: 17883827
%~1 is the first parameter (which will be the filename)

%~t1 gives you the modified date and time of the filename.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:GnarOlak
ID: 17884051
How is the batch file that does this going to be invoked?
0
 
LVL 6

Expert Comment

by:GnarOlak
ID: 17884140
This is kind of brutal but what about putting:

shutdown -r -t 432000

in AUTOEXEC.BAT?

That would automatically reboot every 5 days (5 * 24 * 60 * 60 = 432000 seconds).

I have no idea if always being in a pending reboot state would create any problems but it's food for thought.
0
 
LVL 4

Assisted Solution

by:dasmcse
dasmcse earned 150 total points
ID: 17884196
You could schedule this vbScript to run at a certain time.

' Get the computer name
Set objNet = WScript.CreateObject( "WScript.Network" )
strComputer = objNet.ComputerName
Set objWshShell = WScript.CreateObject("WScript.Shell")
' Create WMI object
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Get the last boot Time
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
objSWbemDateTime.Value = objOperatingSystem.LastBootUpTime
strLastBootTime = objSWbemDateTime.GetVarDate
Next
' Split date and time to get the date by itself
arrSplitLine=Split(strLastBootTime," ")
strLastBootDate=trim(arrSplitLine(0))
intDaysPassed=DateDiff("d", Now(),strLastBootDate)
If (intDaysPassed >= 5) Then
      WScript.Echo("At least " & intDaysPassed & " days have passed.")
      command = "shutdown.exe -r -t 60"
      objWshShell.Run command
Else
      WScript.Echo("Only " & intDaysPassed & " days have passed.")
End If
0
 
LVL 1

Accepted Solution

by:
t2wei earned 150 total points
ID: 17895276
Here is a function that can get file timestamp in delphi 7
//please remember to use unit : windows & sysutils
function GetFileTimeNew(FileName:string):TDateTime;
var hfile,htempfile:THandle;
    timecreate,timeAccess,timewrite:TFileTime;
begin
  Result:=0;
  if FileExists(FileName) then
  begin
    try
      hfile:=CreateFile(PChar(FileName),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,htempfile);
      Result:=FileDateToDateTime(FileGetDate(hfile));
      finally
        CloseHandle(hfile);
      end;
  end;
end;

and the shutdown function in delphi 7, I used it to auto shutdown my Computer on the time I set. It works in win XP.

procedure TForm1.Timer1Timer(Sender: TObject);
var tkp,pretkp:_TOKEN_PRIVILEGES;
    hToken:THandle;
    Returnlen:cardinal;
begin
  if datepicker.Date+timePicker.Time<Now then
  begin
    Timer1.Enabled:=false;
    // Get a token for this process.
    if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)) then
      raise Exception.Create('OpenProcessToken');
    // Get the LUID for the shutdown privilege.
    LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
    //if (GetLastError() <> ERROR_SUCCESS) then
      //raise Exception.Create('LookupPrivilegeValue');
    tkp.PrivilegeCount := 1;  // one privilege to set
    tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    // Get the shutdown privilege for this process.
    AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,Returnlen);
    // Cannot test the return value of AdjustTokenPrivileges.
    if (GetLastError() <> ERROR_SUCCESS) then
      raise Exception.Create('AdjustTokenPrivileges');
    // Shut down the system and force all applications to close.
    if (not ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE, 0)) then
      raise Exception.Create('ExitWindowsEx');
    close;
  end;
end;


So with this, you can write an exe file to shutdown PC automatically.
0
 

Author Comment

by:AFAIT
ID: 17897387
Hi all,

Thank you so much for your help.  I found a small program called "wasfile.exe" on the Web that did the check for me.  Though I am sure your suggestions would have all worked...going to do some point slitting...
Thanks again!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now