我正在使用Boe Prox 的脚本来打印目录中所有文件和文件夹的列表。我需要 Prox 的脚本(与其他 Windows 打印目录命令相反),因为它使用 robocopy 打印长度超过 260 个字符的文件路径。
我的问题是,我还希望将文件哈希与文件名一起打印。我已经修改了脚本以获取哈希值(请参见下文),它通常可以工作,除非文件路径长于 260 个字符。长文件路径在最终输出的哈希列中为空白。
我做过的研究:
- 根据这个stackoverflow question,Robocopy 有几个可以修改的开关。我浏览了Boe 的博客以及robocopy命令列表,但没有关于 filehash 开关的内容。
尝试解决问题:
- 我还尝试修改 filehash 的语法,使其更符合脚本的其余部分,即。
Hash = $matches.Hash
(这将返回所有空白代替文件哈希) - 我尝试删除似乎指定项目而不是项目内容的正则表达式部分,即:(
If ($_.Trim() -match "^(?<Children>\d+)\s+") {
这会导致错误代码WARNING: Cannot bind argument to parameter 'Path' because it is null.
)
不过,我非常希望这会发生:Boe 原始脚本中的注释包括以下行:“1.1 版 - 增加了计算文件哈希的能力”
这是我的(部分工作脚本):
Function Get-FolderItem {
[cmdletbinding(DefaultParameterSetName='Filter')]
Param (
[parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias('FullName')]
[string[]]$Path = $PWD,
[parameter(ParameterSetName='Filter')]
[string[]]$Filter = '*.*',
[parameter(ParameterSetName='Exclude')]
[string[]]$ExcludeFile,
[parameter()]
[int]$MaxAge,
[parameter()]
[int]$MinAge
)
Begin {
$params = New-Object System.Collections.Arraylist
$params.AddRange(@("/L","/E","/NJH","/BYTES","/FP","/NC","/XJ","/R:0","/W:0","T:W"))
If ($PSBoundParameters['MaxAge']) {
$params.Add("/MaxAge:$MaxAge") | Out-Null
}
If ($PSBoundParameters['MinAge']) {
$params.Add("/MinAge:$MinAge") | Out-Null
}
}
Process {
ForEach ($item in $Path) {
Try {
$item = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath
If (-Not (Test-Path -LiteralPath $item -Type Container -ErrorAction Stop)) {
Write-Warning ("{0} is not a directory and will be skipped" -f $item)
Return
}
If ($PSBoundParameters['ExcludeFile']) {
$Script = "robocopy `"$item`" NULL $Filter $params /XF $($ExcludeFile -join ',')"
} Else {
$Script = "robocopy `"$item`" NULL $Filter $params"
}
Write-Verbose ("Scanning {0}" -f $item)
Invoke-Expression $Script | ForEach {
Try {
If ($_.Trim() -match "^(?<Children>\d+)\s+(?<FullName>.*)") {
$object = New-Object PSObject -Property @{
FullName = $matches.FullName
Extension = $matches.fullname -replace '.*\.(.*)','$1'
FullPathLength = [int] $matches.FullName.Length
Length = [int64]$matches.Size
FileHash = (Get-FileHash -Path $matches.FullName).Hash
Created = (Get-Item $matches.FullName).creationtime
LastWriteTime = (Get-Item $matches.FullName).LastWriteTime
}
$object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
Write-Output $object
} Else {
Write-Verbose ("Not matched: {0}" -f $_)
}
} Catch {
Write-Warning ("{0}" -f $_.Exception.Message)
Return
}
}
} Catch {
Write-Warning ("{0}" -f $_.Exception.Message)
Return
}
}
}
}
Get-FolderItem "C:\TestingFileFolders"