[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • Last Modified:

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

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
AFAIT
Asked:
AFAIT
  • 3
  • 3
  • 2
  • +2
4 Solutions
 
GnarOlakCommented:
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
 
AFAITAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AFAITAuthor Commented:
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
 
hclgroupCommented:
%~1 is the first parameter (which will be the filename)

%~t1 gives you the modified date and time of the filename.
0
 
GnarOlakCommented:
How is the batch file that does this going to be invoked?
0
 
GnarOlakCommented:
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
 
dasmcseCommented:
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
 
t2weiCommented:
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
 
AFAITAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now