Jump to content

Locate the latest file in a folder and display it.


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...