我目前正在研究一个应该执行以下操作的Power shell脚本: 检查文件夹中的所有项目,如果有超过一定大小的项目(比如10MB)创建一个文件夹(名为“toobig”)并将那些项目移动到那里. 到目前
检查文件夹中的所有项目,如果有超过一定大小的项目(比如10MB)创建一个文件夹(名为“toobig”)并将那些项目移动到那里.
到目前为止,这是我的脚本:
function delbig {
param (
[parameter (Mandatory=$true)]
$p
)
$a= Get-ChildItem $p | Where-Object {$_.Length -gt 10000000} | Measure- Object
$a.count
if ($a -gt 0){
mkdir "$p\tooBig"
}
"$([int]$a)"
}
delbig
我已经弄清楚如何移动项目以及如何创建文件夹,但我的if条件决定是否应该触发操作会给我以下错误:
Cannot compare "Microsoft.PowerShell.Commands.GenericMeasureInfo" because it is not IComparable.
At C:\Powertest\movbig.ps1:14 char:1
+ if ($a -gt 0){
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NotIcomparable
Cannot convert the "Microsoft.PowerShell.Commands.GenericMeasureInfo" value of type "Microsoft.PowerShell.Commands.GenericMeasureInfo" to type "System.Int32".
At C:\Powertest\movbig.ps1:20 char:4
+ "$([int]$a)"
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
那么$a中的值应该是一个int吗?如果值大于0,我的if条件应该看起来(我也尝试过“0”).
任何帮助将不胜感激!
此致,Gerfi
$a是GenericMeasureInfo类型的实例,无法与零(int)进行比较.使用$a的Count属性比较为零:if ($a.Count -gt 0){
mkdir "$p\tooBig"
}
另外,我注意到Measure-Object中有一个空间需要删除.我猜这只是帖子里的一个错字.
