如何从PowerShell中的Get-ChildItem中排除文件和文件夹?

罗比

我制作了一个PowerShell脚本,该脚本运行带有md5检查的robocopy。

它工作正常,但是如果我尝试排除某些目录或文件,则robocopy会处理该排除,而比较哈希的脚本的MD5部分无效,返回一些错误,因为源文件/哈希多于目标文件。 。

我可能已经尝试过在这里和Internet上找到的所有方法!我不能从路径中排除目录和/或文件!

以下是我到目前为止所做的。在这种模式下,md5-copy有效(不排除):

$Source = "F:\"

$IgnoreDir = @(
    $Source + '$RECYCLE.BIN'
    $Source + "System Volume Information"
    $Source + "VMs"
)   
$IgnoreFile = @(
    $Source + "SHDrive.vmdk"
    $Source + "SHDrive-flat.vmdk"
)
$Ignored = $IgnoreDir + $IgnoreFile

Robocopy:

Robocopy.exe /R:1 /W:0 $Source $Dest /E /V /TEE /XD $IgnoreDir /XF $IgnoreFile /LOG:$LogDir\RBCY_MD5_F.txt

MD5:

$SourceHash = Get-ChildItem "$Source\*.*" -Recurse -Force -Exclude $Ignored | Where-Object {!$_.psiscontainer } | Get-FileHash
$SourceHash | Select-Object "Hash", "path" | ft -HideTableHeaders -AutoSize | Out-File -Width "300" $LogDir\SRC_MD5_REF.txt
$SourceHash.Hash | Out-File $LogDir\SRC_MD5.txt 

比较:

$Diff = Compare-Object -ReferenceObject $(get-content "$LogDir\SRC_MD5.txt") -DifferenceObject $(get-content "$LogDir\DST_MD5.txt")

F:\驱动器的内容:

PS C:\Users\Robbi> Get-ChildItem F:\ -force


    Directory: F:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--hs-       19/03/2019     06:40                $RECYCLE.BIN
d-----       16/05/2020     04:41                DATA
d-----       19/01/2020     06:34                Drivers
d-----       16/05/2020     04:55                Gumball
d-----       16/05/2020     04:58                SW
d--hs-       19/03/2019     06:36                System Volume Information
d-----       13/03/2020     16:08                Tools
d-----       12/12/2019     00:02                VMs
d-----       16/05/2020     04:55                _Pre-Cestino
-a----       08/02/2020     03:02    21474836480 SHDrive-flat.vmdk
-a----       08/02/2020     03:02            466 SHDrive.vmdk

如何从“获取子项”列表中排除不想复制的数据?在这种特定情况下,如果可能的话,在“所有情况”下,Get-ChildItem必须在整个文件系统中排除显式内容列表(可变字符串和/或数组)。

mklement0

从PowerShell 7.0开始,cmdlet-Exclude-Include提供程序参数(例如)Get-ChildItem仅对项目名称(文件/目录名称,对于文件系统提供程序而言)起作用,而不对完整路径起作用

鉴于您要排除的所有路径都是目标目录的直接子目录,我建议采用两步方法:

# Get all files and directories in $Source, except those to be excluded.
# Note the use of \* instead of \*.*, so as to also include the
# directories (whose names don't have an extension).
$items = Get-Item $Source\* -Force | Where-Object FullName -NotIn $Ignored

# Recursively process all resulting files and directories and
# calculate their hashes.
# Note the use of -File to limit output to files.
$SourceHash = $items | Get-ChildItem -Recurse -Force -File | Get-FileHash

当然,如果$Ignored根据文件/目录名称定义数组,则可以使用-Exclude

# Convert the ignore list to file/directory names only.
$Ignored = $Ignored | Split-Path -Leaf

$SourceHash = Get-ChildItem -File $Source -Recurse -Force -Exclude $Ignored |
                Get-FileHash

如果要排除的路径可以出现在子目录层次结构的任何级别,则需要做更多的工作:

$ignoredRegex = '^(?:{0})(?:\{1}|$)' -f
                  ($Ignored.ForEach({ [regex]::Escape($_) }) -join '|'),
                  [IO.Path]::DirectorySeparatorChar


$SourceHash = Get-ChildItem $Source -Recurse -File -Force |
                Where-Object FullName -NotMatch $ignoredRegex
                  Get-FileHash

上面的表达式-match运算符一起使用正则表达式以排除子目录树中任何位置的所有指定路径及其子级

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Powershell-在Get-ChildItem中排除文件夹

从 get-childitem 中排除多个文件夹

在 powershell 中使用 get-childitem 时排除文件夹和文件

如何从重复备份中排除所有隐藏的文件和文件夹

如何在LightTable的“搜索”窗格中排除子文件夹和文件?

如何从 rsync 中排除文件夹

如何从rsync中排除文件夹

如何从tslint中排除文件夹?

使用Get-ChildItem排除文件夹-排除

从 Get-childitem 中排除一个文件夹和一种文件类型

如何仅从Git的根文件夹中排除文件

如何从字符串搜索中排除文件夹

如何在compress-archive中排除文件夹

如何从“浏览”选项卡中排除文件夹?

如何从rsync中排除.cpan文件夹?

如何从sam deploy命令中排除文件夹?

如何从YUI压缩器中排除文件夹

如何在sshfs挂载中排除文件夹?

在grunt中,如何从dist文件夹中的干净文件夹中排除.git文件夹

从Brocfile中的构建中排除文件夹

在PhpStorm / WebStorm中从索引中排除文件夹

如何 Get-Childitem 并且只包含子文件夹和文件?

Get-ChildItem递归但排除父文件夹中的文件

如何从 testNG.xml 文件中的测试套件中排除测试文件夹

Powershell 如何从删除过程中排除文件夹

.gitignore中排除的文件夹中的文件出现在提交中

如何从.NET Core / Standard项目中排除文件/文件夹?

如何在Jenkins构建中从SVN监视中排除文件\文件夹

如何在项目或文件中排除文件夹,VS2015