We produce files with date in the name. (* below is the wildcard for the date) I want to grab the last file and the folder that contains the file also has a date(month only) in its title.
I am using PowerShell and I am scheduling it to run each day. Here is the script so far:
$LastFile = *_DailyFile $compareDate = (Get-Date).AddDays(-1) $LastFileCaptured = Get-ChildItem -Recurse | Where-Object {$LastFile.LastWriteTime -ge $compareDate}
Getting a single latest from a given Directory using file extension filter. One can use a combination of properties like LastWriteTime and Select -First 1 to get the latest file in the given directory.
Use the Get-ChildItem LastWriteTime attribute to find the list of files with lastwritetime. Get-ChildItem in the PowerShell gets one or more child items from the directory and subdirectories.
Description. The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function. For files, the content is read one line at a time and returns a collection of objects, each of which represents a line of content.
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.
If you want the latest file in the directory and you are using only the LastWriteTime
to determine the latest file, you can do something like below:
gci path | sort LastWriteTime | select -last 1
On the other hand, if you want to only rely on the names that have the dates in them, you should be able to something similar
gci path | select -last 1
Also, if there are directories in the directory, you might want to add a ?{-not $_.PsIsContainer}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With