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

发布于 2024-12-04 08:56:05 字数 98 浏览 0 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2024-12-11 08:56:06

PowerShell 主要与管道一起工作,因此您编写的大部分内容将包括创建表示某些信息的对象,以及过滤和操作它们。在本例中,对象是一堆文件夹。

  1. 获取文件夹中的所有项目。这将获取文件文件夹,这就是为什么步骤 2 是必要的。该行末尾的 | 表示管道将在下一行继续 - 由 Get-ChildItem 创建的对象将被一个接一个地传递给另一个命令。

    Get-ChildItem c:\temp |
    
  2. 过滤文件夹。遗憾的是,没有真正优雅的方法。不要担心它说的是“容器”,而不是“文件夹”——这些命令适用于许多不同的事物,而不仅仅是文件和文件夹,因此在命名时使用了更通用的概念。

    其中 { $_.PSIsContainer } |
    
  3. 按日期降序排序,因此最新的文件夹是第一个。

    对创建时间进行降序排序 |
    
  4. 选择第一个(最新)文件夹。

    选择 - 第 1 个
    

简而言之:

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

或者

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

这两行都大量使用 PowerShell 中命令的默认别名,例如 ? 代表 Where-Object。不过,您应该在脚本中使用全名,因为您永远不知道别名在代码可能运行的其他计算机上是什么样子。


编辑: PowerShell 3 为 Get-ChildItem 提供了额外的参数,允许您直接对文件或文件夹进行过滤,因此您不需要 Where code>:

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

通常您将在 PowerShell 中使用对象及其属性。两个非常有用的命令是 Get-Member 及其别名 gmGet-Command 或只是 gcmGet-Member 会告诉你一个对象有哪些属性和方法;您只需将其他内容输入其中即可:

Get-ChildItem | gm

会告诉您文件和目录具有哪些属性。

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.

  1. 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 by Get-ChildItem will then be passed one by one to another command.

    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 ? for 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:

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

Generally you will work with objects and their properties in PowerShell. Two very helpful commands are Get-Member and its alias gm and Get-Command or just gcm. Get-Member will tell you what properties and methods an object has; 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. PowerShell commands try to be very consistent in their use of verbs and nouns. To find all commands that end in Object you can try gcm *-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文