我有一个用于提取文件元数据的脚本。我正在尝试修改它,以便它跳过指定的子文件夹。
如果您阅读脚本,您可以看到有一个 Param 块,其中包含“ExcludesFiles”等选项。我可以让它排除文件,但无法让它排除文件夹。
这是脚本:
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","/UNILOG:c:\temp\test.txt"))
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 | Out-Null
get-content "c:\temp\test.txt" | 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
#FileHash = Get-FileHash -LiteralPath "\\?\$($matches.FullName)" |Select -Expand Hash
#Created = ([System.IO.FileInfo] $matches.FullName).creationtime
#Created = ([System.IO.FileInfo] "\\?\$($matches.FullName)").creationtime
#LastWriteTime = ([System.IO.FileInfo] "\\?\$($matches.FullName)").LastWriteTime
#Characters = (Get-Content -LiteralPath "\\?\$($matches.FullName)" | Measure-Object -ignorewhitespace -Character).Characters
#Size = ([System.IO.FileInfo] "\\?\$($matches.FullName)").length
#Access = ((Get-Acl -LiteralPath "\\?\$($matches.FullName)") |Select -Expand AccessToString)-replace '[\r\n]',' '
#$permission = (Get-Acl $Folder).Access | ?{$_.IdentityReference -match $User} | Select IdentityReference,FileSystemRights
#Owner = (Get-ACL $matches.Fullname).Owner
}
$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
}
}
}
}
$a = Get-FolderItem "C:\Temp\ExtractMetadata" | Export-Csv -Path C:\Temp\2023-12-12Testing.csv -Encoding Unicode
我已尝试以下操作,但未跳过指定文件夹:
$a = Get-FolderItem "C:\Temp\ExtractMetadata" -ExcludeFile "C:\Temp\ExtractMetadata\Folder_to_Skip\" | Export-Csv -Path C:\Temp\2023-12-12Testing.csv -Encoding Unicode
我也尝试过以下操作,但输出文件是空白的:
$a = Get-FolderItem "C:\Temp\ExtractMetadata" -Filter "C:\Temp\ExtractMetadata\Folder to Skip\*.*" | Export-Csv -Path C:\Temp\2023-12-12Testing.csv -Encoding Unicode
我认为它可能基于 Get Child Item (它有一个内置的排除参数),但我不认为这个脚本使用 Get-Child Item - 它只是一个循环。 这个答案表明我需要的是一个“继续”命令,但是我把它放在哪里呢?我不熟悉 PowerShell 语法。
该脚本基于 Robocopy。 Robocopy 允许您跳过文件夹,如此处所述。
将脚本中的 /XF 替换为 /XD,并使用行
$a = Get-FolderItem "C:\Temp\ExtractMetadata" -ExcludeFile "C:\Temp\ExtractMetadata\Folder_to_Skip\" | Export-Csv -Path C:\Temp\2023-12-12Testing.csv -Encoding Unicode
。