gpt4 book ai didi

powershell - cmd 和 powershell 中的否定

转载 作者:行者123 更新时间:2023-12-04 01:17:48 26 4
gpt4 key购买 nike

有没有办法在 cmd 或 powershell 中进行否定?换句话说,我想要的是找到所有不符合特定条件的文件(除了将否定指定为“-”的属性)在名称中说。如果有一个通用的否定也可以用于其他上下文,那将会很有帮助。另外,对于powershell,有没有办法获取文件名列表,然后将其存储为可以排序的数组等?

很抱歉问了一些看起来如此基本的事情。

最佳答案

使用 PowerShell 有多种方法可以否定一组条件,但最好的方法取决于具体情况。在每种情况下使用单一的否定方法有时可能非常低效。如果要返回 2011 年 5 月 1 日之前不是 DLL 的所有项目,可以运行:

#This will collect the files/directories to negate
$NotWanted = Get-ChildItem *.dll| Where-Object {$_.CreationTime -lt '05/01/2011'}
#This will negate the collection of items
Get-ChildItem | Where-Object {$NotWanted -notcontains $_}

这可能非常低效,因为通过管道的每个项目都将与另一组项目进行比较。获得相同结果的更有效方法是:
Get-ChildItem | 
Where-Object {($_.Name -notlike *.dll) -or ($_.CreationTime -ge '05/01/2011')}

正如@riknik 所说,请查看:
get-help about_operators
get-help about_comparison_operators

此外,许多 cmdlet 都有一个“排除”参数。
# This returns items that do not begin with "old"
Get-ChildItem -Exclude Old*

要将结果存储在可以排序、过滤、重用等的数组中:
# Wrapping the command in "@()" ensures that an array is returned
# in the event that only one item is returned.
$NotOld = @(Get-ChildItem -Exclude Old*)

# Sort by name
$NotOld| Sort-Object
# Sort by LastWriteTime
$NotOld| Sort-Object LastWriteTime

# Index into the array
$NotOld[0]

关于powershell - cmd 和 powershell 中的否定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6104049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com