有没有办法在cmd或powershell中做否定?换句话说,我想要的是找到所有不符合某个标准的文件(除了指定为" - "的属性),在名称中说.如果存在可以在其他环境中使用的一般否定,将会有所帮助.另外,对于powershell,有没有办法获取文件名列表,然后将其存储为可以排序的数组等?
对于问一些看起来如此基本的东西而道歉.
使用PowerShell有很多方法可以否定一组标准,但最好的方法取决于具体情况.在每种情况下使用单一否定方法有时可能效率非常低.如果您想要返回所有不是早于05/01/2011的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 $_}
Run Code Online (Sandbox Code Playgroud)
这可能是非常低效的,因为通过管道的每个项目将与另一组项目进行比较.获得相同结果的更有效方法是:
Get-ChildItem |
Where-Object {($_.Name -notlike *.dll) -or ($_.CreationTime -ge '05/01/2011')}
Run Code Online (Sandbox Code Playgroud)
正如@riknik所说,退房:
get-help about_operators
get-help about_comparison_operators
Run Code Online (Sandbox Code Playgroud)
此外,许多cmdlet都有"Exclude"参数.
# This returns items that do not begin with "old"
Get-ChildItem -Exclude Old*
Run Code Online (Sandbox Code Playgroud)
将结果存储在可以排序,过滤,重用等的数组中:
# 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]
Run Code Online (Sandbox Code Playgroud)