简体   繁体   English

使用powershell从路径获取最新创建的文件夹

[英]get the latest created folder from a path using powershell

How to get the latest created folder from a path using Windows PowerShell? 如何使用Windows PowerShell从路径获取最新创建的文件夹?

I have the path C:\\temp and I want to find the most recently created folder in this path. 我有路径C:\\temp ,我想在此路径中找到最近创建的文件夹。

PowerShell works mainly with the pipeline, so most of what you'd write will consist of creating objects representing some information, and filtering and manipulating them. PowerShell主要与管道一起工作,因此您编写的大部分内容将包括创建表示某些信息的对象,以及过滤和操作它们。 In this case, the objects are a bunch of folders. 在这种情况下,对象是一堆文件夹。

  1. Get all items in the folder. 获取文件夹中的所有项目。 This will get files and folders, that's why step 2 is necessary. 这将获取文件文件夹,这就是为什么第2步是必要的。 The | | at the end of the line signals that the pipeline will continue in the next line – objects created by Get-ChildItem will then be passed one by one to another command. 在行的末尾表示管道将在下一行继续 - Get-ChildItem创建的对象将逐个传递给另一个命令。

     Get-ChildItem c:\\temp | 
  2. Filter for folders. 过滤文件夹。 There is no really elegant way, sadly. 可悲的是,没有真正优雅的方式。 Don't worry about that it says “container”, not “folder” – Those commands work with many different things, not only files and folders, so a more general concept was used in naming. 不要担心它说“容器”,而不是“文件夹” - 这些命令可以处理许多不同的东西,不仅仅是文件和文件夹,因此在命名时使用了更一般的概念。

     Where { $_.PSIsContainer } | 
  3. Sort by date, descending, so the newest folder is the first one. 按日期排序,降序,因此最新的文件夹是第一个。

     Sort CreationTime -Descending | 
  4. Select the first (newest) folder. 选择第一个(最新)文件夹。

     Select -First 1 

So in short: 简而言之:

gci c:\temp | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1

or 要么

(gci c:\temp | ? { $_.PSIsContainer } | sort CreationTime)[-1]

Both of those lines make heavy use of default aliases for commands in PowerShell, such as ? 这两行都大量使用PowerShell中的命令的默认别名,例如? for Where-Object . 对于Where-Object You should use the full names in scripts, though, as you'll never know what the aliases will look like on other machines the code might run on. 但是,您应该在脚本中使用全名,因为您永远不会知道代码可能在其他机器上运行的别名。


EDIT: PowerShell 3 has additional parameters for Get-ChildItem that allow you to do filtering for files or folders directly, so you don't need the Where : 编辑: PowerShell 3具有Get-ChildItem其他参数,允许您直接对文件或文件夹进行过滤,因此您不需要Where

Get-ChildItem -Directory C:\temp | ...

Generally you will work with objects and their properties in PowerShell. 通常,您将在PowerShell中使用对象及其属性。 Two very helpful commands are Get-Member and its alias gm and Get-Command or just gcm . 两个非常有用的命令是Get-Member及其别名gmGet-Command或只是gcm Get-Member will tell you what properties and methods an object has; Get-Member将告诉您对象具有哪些属性和方法; you just pipe something else into it for that: 你只需将其他东西输入其中:

Get-ChildItem | gm

will tell you what properties files and directories have. 会告诉你文件和目录有哪些属性。

Get-Command will list all commands there are or those that match a particular pattern. Get-Command将列出所有命令或与特定模式匹配的命令。 PowerShell commands try to be very consistent in their use of verbs and nouns. PowerShell命令在动词和名词的使用方面尝试非常一致。 To find all commands that end in Object you can try gcm *-Object – those are general commands working with pretty much everything. 要查找以Object结尾的所有命令,您可以尝试gcm *-Object - 这些是几乎所有内容的通用命令。 Get-Help ForEach-Object then would tell you about a particular command, ForEach-Object in this case. Get-Help ForEach-Object然后会告诉你一个特定的命令,在这种情况下是ForEach-Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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