2011-05-23 71 views
5

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

道歉,问一些看起来很基本的东西。

回答

5

使用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 $_} 

这可能是非常低效的,因为通过管道的每个项目会相比,另一组项目。一个更有效的方式来获得同样的结果会是这样做:

Get-ChildItem | 
    Where-Object {($_.Name -notlike *.dll) -or ($_.CreationTime -ge '05/01/2011')} 

正如@riknik说,检查出:

get-help about_operators 
get-help about_comparison_operators 

此外,许多命令有一个“排除”参数。

# 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] 
3

不知道我完全理解你在找什么。也许这样(在PowerShell中)?

get-childitem | where-object { $_.name -notlike "test*" } 

这将获取当前目录中的所有文件,其名称不以短语test开头。

要获得运营商的详细信息,可以使用PowerShell的内置帮助:

get-help about_operators 
get-help about_comparison_operators 
+0

伟大的作品,谢谢。 – soandos 2011-05-24 01:11:27