Happy Independence Day from all of us at the Connect IT Community! Our US offices will be closed on Monday, July 4th, 2022 in recognition of the holiday. Limited Support staff in the US will be on-call and available for critical Service(s) Down issues only. Normal Support operations in the US will resume on Tuesday, July 5th, 2022.

Find Newest (Log?) File in a Directory

Onno de Vries
Onno de Vries Member, Kaseya Certified
edited January 24 in Solutions

Product Name: Find Newest (Log?) File in a Directory

Description : This scripts selects the file in a path that was updated/created today. This is ideal for selecting today's log file from an application, when you don't know the name of the log file (it's randomly created). The only things you do know is that it's daily and has a certain extension.
So now you can work with that log file going forward using another script.

Instructions :

You need to type in a full path and the filename using wildcards in
specific steps for this script to properly work. Use LINES 10 and 12 for

If there is no file or more than one file with today's update/creation date, an alarm is raised.

That last updated file and path  will then become available in a global variables, the actual File Pattern you searched in that Directory is also available as a global variable. You can run this script from another procedure and then call out these variables in that procedure for further processing.

Line 10 has #global:SearchPath# : The Directory searched. For Example "C:\temp\".

Line 12 has #global:SearchFile#: The File Pattern Searched. This can you wildcards, just like you would use in the Command Box. For example "*.log".

Line 35 has #global:NewestFile#: Todays created/updated file as the result of the script.


  • Buster Davis
    Buster Davis Member
    edited March 2017
    Does this script look only at the parent directory or would it look at subdirectories in the parent directories
  • Onno de Vries
    Onno de Vries Member, Kaseya Certified
    edited May 2018
    Hi Buster Davis, this script in it's current state, would only work on the parent directory and not it's subdirectories.

    Changing it to have it look through subdirectories as well is easy actually:
    1. Open the Agent Procedure for editing
    2. Click on line 15, this currently says "dir #global:SearchPath##global:SearchFile# /a-d /o-d /tw|findstr /C:"%date:~-10%">>#awd#\NewestFile\FileList.txt"
    3. change that line to say "dir #global:SearchPath##global:SearchFile# /s /a-d /o-d /tw|findstr /C:"%date:~-10%">>#awd#\NewestFile\FileList.txt"
    4. Save the Agent Procendure and Run that on a test machine that you know has that file type in at least one subdirectory of the chosen search path.

    Let me now how you're getting along with that!?