Jump to content

Locate the latest file in a folder and display it.


eDecisions
 Share

Recommended Posts

Not my work, I took code form two locations and put them together to fit my needs, change the path to the path to your folder, change the filter to the file type to filter by, add as a script, run it, and then open your Last Executions to see the contents of the file. I was needing to check status of a log file on a server every so often, the log file name would change daily and be time stamped, this solved it for me.

 

$dir = "C:\data\Sync_Reports"
$filter="*.txt"
$latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
Get-Content $dir"\"$latest
 

Link to comment
Share on other sites

Made one change this morning after testing, the log file is 50+ pages, I only really need to see the last 10 to 20lines of the log to see how things are going, added the -tail 20 to return the last 20 lines, tested this morning, I imagine next step would be to combine this for any/all longs I want and export to a text file and email as a daily summary. 

 

$dir = "C:\aktdata\quickbooks\Sync_Reports"
$filter="*.txt"
$latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
Get-Content $dir"\"$latest -tail 20

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...