用于比较文件夹结构的 Powershell 脚本
Powershell script to compare folder structure
我正在尝试编写一个 powershell 脚本,该脚本将根据模板文件夹结构布局检查目录的文件夹结构,并在其不同(即缺少文件夹或添加了不同文件夹)时报告。
Folder Template Structure
Folder A
Folder B
Directory 1 to check
Folder A
Folder B
Folder C
Directory 2 to check
Folder A
因此,对于目录 1,它会报告文件夹 C 是附加的,而对于目录 2,它会报告文件夹 B 丢失
如有任何帮助,我们将不胜感激
# Get the directories inside the template dir. as relative paths
$templateDirs = Get-ChildItem -Directory -Recurse -Name $templatePath
# Ditto for directory 1 and directory 2
$dir1Dirs = Get-ChildItem -Directory -Recurse -Name $dir1Path
$dir2Dirs = Get-ChildItem -Directory -Recurse -Name $dir2Path
# Compare to the template dirs.
Compare-Object $templateDirs $dir1Dirs
'---' # Output separator string just to show distinct outputs.
Compare-Object $templateDirs $dir2Dirs
请注意 -Name
与 Get-ChildItem
的使用,这会导致所有子目录(-Directory
、-Recurse
被报告为 相对于input directory,这样方便比较目录树。
另请注意,Compare-Object
cmdlet 默认输出具有两个属性的 [pscustomobject]
个实例,并且仅针对输入集之间的 差异:
.InputObject
,在您的情况下,一个输入集唯一的相对目录路径。
.SideIndicator
,这是一个字符串,指示输入对象是否对 left 侧是唯一的(first 输入设置,隐式绑定到参数 -ReferenceObject
) - '<='
- 或 right 侧(second 输入集,隐式绑定到参数 -DifferenceObject
) - '=>'
上面的结果类似于:
InputObject SideIndicator
----------- -------------
C => # folder C only in dir. 1, not in template dir.
---
B <= # folder B only in template dir., not in dir. 2
要比较两个文件夹,请执行以下步骤(来自:https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/08/easily-compare-two-folders-by-using-powershell/):
使用带有递归切换参数的 Get-ChildItem cmdlet 和
路径参数(指向要用作参考的文件夹)
获取文件信息对象的集合。将这些对象存储在
多变的。
使用带有递归切换参数的 Get-ChildItem cmdlet 和
路径参数(指向用于比较的文件夹)到
获取文件信息对象的集合。将这些对象存储在
不同的变量。
使用 Compare-Object cmdlet 并指定存储在
ReferenceObject 参数的第一个变量。提供对象
存储在 DifferenceObject 参数的第二个变量中。
代码:
$fso = Get-ChildItem -Recurse -path C:\fso
$fsoBU = Get-ChildItem -Recurse -path C:\template
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
我正在尝试编写一个 powershell 脚本,该脚本将根据模板文件夹结构布局检查目录的文件夹结构,并在其不同(即缺少文件夹或添加了不同文件夹)时报告。
Folder Template Structure
Folder A
Folder B
Directory 1 to check
Folder A
Folder B
Folder C
Directory 2 to check
Folder A
因此,对于目录 1,它会报告文件夹 C 是附加的,而对于目录 2,它会报告文件夹 B 丢失
如有任何帮助,我们将不胜感激
# Get the directories inside the template dir. as relative paths
$templateDirs = Get-ChildItem -Directory -Recurse -Name $templatePath
# Ditto for directory 1 and directory 2
$dir1Dirs = Get-ChildItem -Directory -Recurse -Name $dir1Path
$dir2Dirs = Get-ChildItem -Directory -Recurse -Name $dir2Path
# Compare to the template dirs.
Compare-Object $templateDirs $dir1Dirs
'---' # Output separator string just to show distinct outputs.
Compare-Object $templateDirs $dir2Dirs
请注意 -Name
与 Get-ChildItem
的使用,这会导致所有子目录(-Directory
、-Recurse
被报告为 相对于input directory,这样方便比较目录树。
另请注意,Compare-Object
cmdlet 默认输出具有两个属性的 [pscustomobject]
个实例,并且仅针对输入集之间的 差异:
.InputObject
,在您的情况下,一个输入集唯一的相对目录路径。.SideIndicator
,这是一个字符串,指示输入对象是否对 left 侧是唯一的(first 输入设置,隐式绑定到参数-ReferenceObject
) -'<='
- 或 right 侧(second 输入集,隐式绑定到参数-DifferenceObject
) -'=>'
上面的结果类似于:
InputObject SideIndicator
----------- -------------
C => # folder C only in dir. 1, not in template dir.
---
B <= # folder B only in template dir., not in dir. 2
要比较两个文件夹,请执行以下步骤(来自:https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/08/easily-compare-two-folders-by-using-powershell/):
使用带有递归切换参数的 Get-ChildItem cmdlet 和 路径参数(指向要用作参考的文件夹) 获取文件信息对象的集合。将这些对象存储在 多变的。
使用带有递归切换参数的 Get-ChildItem cmdlet 和 路径参数(指向用于比较的文件夹)到 获取文件信息对象的集合。将这些对象存储在 不同的变量。
使用 Compare-Object cmdlet 并指定存储在 ReferenceObject 参数的第一个变量。提供对象 存储在 DifferenceObject 参数的第二个变量中。
代码:
$fso = Get-ChildItem -Recurse -path C:\fso
$fsoBU = Get-ChildItem -Recurse -path C:\template
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU