使用 powershell 从路径获取最新创建的文件夹
如何使用 Windows PowerShell 从路径获取最新创建的文件夹?
我有路径 C:\temp
,我想在该路径中找到最近创建的文件夹。
How to get the latest created folder from a path using Windows PowerShell?
I have the path C:\temp
and I want to find the most recently created folder in this path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PowerShell 主要与管道一起工作,因此您编写的大部分内容将包括创建表示某些信息的对象,以及过滤和操作它们。在本例中,对象是一堆文件夹。
获取文件夹中的所有项目。这将获取文件和文件夹,这就是为什么步骤 2 是必要的。该行末尾的
|
表示管道将在下一行继续 - 由Get-ChildItem
创建的对象将被一个接一个地传递给另一个命令。过滤文件夹。遗憾的是,没有真正优雅的方法。不要担心它说的是“容器”,而不是“文件夹”——这些命令适用于许多不同的事物,而不仅仅是文件和文件夹,因此在命名时使用了更通用的概念。
按日期降序排序,因此最新的文件夹是第一个。
选择第一个(最新)文件夹。
简而言之:
或者
这两行都大量使用 PowerShell 中命令的默认别名,例如
?
代表Where-Object
。不过,您应该在脚本中使用全名,因为您永远不知道别名在代码可能运行的其他计算机上是什么样子。编辑: PowerShell 3 为
Get-ChildItem
提供了额外的参数,允许您直接对文件或文件夹进行过滤,因此您不需要Where
code>:通常您将在 PowerShell 中使用对象及其属性。两个非常有用的命令是
Get-Member
及其别名gm
和Get-Command
或只是gcm
。Get-Member
会告诉你一个对象有哪些属性和方法;您只需将其他内容输入其中即可:会告诉您文件和目录具有哪些属性。
Get-Command
将列出所有命令或与特定模式匹配的命令。 PowerShell 命令在动词和名词的使用上尽量保持一致。要查找以Object
结尾的所有命令,您可以尝试gcm *-Object
– 这些是几乎适用于所有内容的通用命令。然后,Get-Help ForEach-Object
会告诉您有关特定命令的信息,在本例中为ForEach-Object
。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. In this case, the objects are a bunch of folders.
Get all items in the folder. This will get files and folders, that's why step 2 is necessary. The
|
at the end of the line signals that the pipeline will continue in the next line – objects created byGet-ChildItem
will then be passed one by one to another command.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.
Sort by date, descending, so the newest folder is the first one.
Select the first (newest) folder.
So in short:
or
Both of those lines make heavy use of default aliases for commands in PowerShell, such as
?
forWhere-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 theWhere
:Generally you will work with objects and their properties in PowerShell. Two very helpful commands are
Get-Member
and its aliasgm
andGet-Command
or justgcm
.Get-Member
will tell you what properties and methods an object has; you just pipe something else into it for that:will tell you what properties files and directories have.
Get-Command
will list all commands there are or those that match a particular pattern. PowerShell commands try to be very consistent in their use of verbs and nouns. To find all commands that end inObject
you can trygcm *-Object
– those are general commands working with pretty much everything.Get-Help ForEach-Object
then would tell you about a particular command,ForEach-Object
in this case.