Easily get the most recent modified files recursively in order
One way to do this with PowerShell would be to...
Use Get-ChildItem to get a recursive list of files .txt
extensions
Pipe that through to a ForEach-Object loop and use
grouping and subexpression
operators to return the
LastWriteTime
property value followed by a comma and the full
path file name of each file—sort that in descending order by the
LastWriteTime
value.
With the #1 and #2 result being a variable, pipe that over in a loop to Split(), with the comma
being the delimiter, and return only the 2nd index (i.e. [1]
) of the
iterated value and retain the descending order of the time stamp but return only the full path file names.
PowerShell
$Src = "C:\YourFolder";
$z = (Get-Childitem -Path $Src -include "*.txt" -File -Recurse | % {"$($_.LastWriteTime), $($_.FullName)"}) | Sort-Object -Descending;
$z | % {$_.Split(",")[1]};
Example Structure
C:\Users\user\Desktop\Dir\A
C:\Users\user\Desktop\Dir\B
C:\Users\user\Desktop\Dir\Test.txt
C:\Users\user\Desktop\Dir\Test2.txt
C:\Users\user\Desktop\Dir\Test3.txt
C:\Users\user\Desktop\Dir\A\2019-07-29 22_18_52-Inquiries.txt
C:\Users\user\Desktop\Dir\A\blah.txt
C:\Users\user\Desktop\Dir\A\Test4.txt
C:\Users\user\Desktop\Dir\B\FLLtest.txt
C:\Users\user\Desktop\Dir\B\FLLtest2.txt
Example Command Output (with date time)
PS C:\Users\user> (Get-Childitem -Path $Src -include "*.txt" -File -Recurse | % {"$($_.LastWriteTime), $($_.FullName)"}) | Sort-Object -Descending;
12/09/2019 18:14:09, C:\Users\user\Desktop\Dir\A\Test4.txt
12/08/2019 20:27:26, C:\Users\user\Desktop\Dir\Test2.txt
12/03/2019 23:55:50, C:\Users\user\Desktop\Dir\Test3.txt
11/02/2019 12:51:35, C:\Users\user\Desktop\Dir\B\FLLtest2.txt
11/02/2019 12:49:09, C:\Users\user\Desktop\Dir\B\FLLtest.txt
07/29/2019 22:38:33, C:\Users\user\Desktop\Dir\A\blah.txt
07/29/2019 22:38:17, C:\Users\user\Desktop\Dir\A\2019-07-29 22_18_52-Inquiries.txt
06/21/2019 23:38:01, C:\Users\user\Desktop\Dir\Test.txt
Example Full Script Output
C:\Users\user\Desktop\Dir\A\Test4.txt
C:\Users\user\Desktop\Dir\Test2.txt
C:\Users\user\Desktop\Dir\Test3.txt
C:\Users\user\Desktop\Dir\B\FLLtest2.txt
C:\Users\user\Desktop\Dir\B\FLLtest.txt
C:\Users\user\Desktop\Dir\A\blah.txt
C:\Users\user\Desktop\Dir\A\2019-07-29 22_18_52-Inquiries.txt
C:\Users\user\Desktop\Dir\Test.txt
Supporting Resources
Editor's history must show the names of the files edited last. – Akina – 2019-12-09T16:47:21.873
@PimpJuiceIT - they may exist in subfolders as well – Adel – 2019-12-09T16:51:47.543
@Akina - ok I see, so perhaps Notepad doesn't show the names of those files ? – Adel – 2019-12-09T16:52:19.743
On Windows 10 Right-Click on the Notepad icon on QuickLaunch pane shows last edited files list. Or you may use alternative text editor - for example, NPP may remember up to 99 last files. – Akina – 2019-12-09T17:17:47.607
@PimpJuiceIT - This gave me an error : .Split : The term '.Split' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:8
@PimpJuiceIT - I'll play with this code , I see what ur trying to do – Adel – 2019-12-09T18:01:04.477
@PimpJuiceIT - its 5.1.18362.145 – Adel – 2019-12-09T18:07:36.863