简体   繁体   中英

Powershell: Get path of last alphanumerically sorted file in a directory

I have a directory with the following files:

  1. Program-3.0.79.J1231.exe
  2. Program-3.0.79.J1230.exe
  3. Program-3.0.79.J1229.exe
  4. Program-3.0.79.J1228.exe
  5. etc...

These files will keep updating every few weeks. For example Program-3.0.79.J1232.exe will automatically be put in this directory in a week or two

Using Power Shell, how would I easily go about getting the path of the latest (NOT WITH last modified) file added. Preferably via alphanumeric sorting

It's fairly simple:

((Gci 'C:\\Myfolder' | sort-object name)[-1]).fullname

The [-1 ] index means "last item in the array".

Here's another version that also filters out folders in case they exist in the path:

Get-ChildItem c:\temp | `
 Where-Object {-not $_.PSIsContainer} | `
  Sort-Object Name | `
   Select-Object -Last 1 -ExpandProperty FullName 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM