Output sub-folder with the latest write access

笑着哭i 提交于 2019-12-11 05:06:19

问题


I have the following powershell command

get-childitem $FilePath | select {$_.Fullname}

This will output the name of ALL subfolders in $FilePath. How do I output the subfolder with the latest write time?


回答1:


I believe this is what you are after:

Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | Select {$_.FullName} -First 1

If you want to see the last write time too, you can use this:

Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | Select {$_.FullName, $_.LastWriteTime} -First 1

This will look at the Last Write Time of both files and folders in the given $FilePath. If you are after just files then provide the -File switch to Get-ChildItem, and if you are only interested in folders then provide the -Directory switch. Also, if you want to know the Last Write Time of any files/folders in the $FilePath including in sub-folders, then provide the -Recurse switch to Get-ChildItem.

If you want to see more than just the 1 file, change -First 1 to the number of files that you want to see.

Also, if you want to instead see the file with the oldest Last Write Time, just remove the -Descending switch parameter to the Sort command.




回答2:


Sort the items and select the last one (default sort order is ascending):

Get-ChildItem $FilePath | sort LastWriteTime | select -Last 1 FullName


来源:https://stackoverflow.com/questions/19458186/output-sub-folder-with-the-latest-write-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!