Powershell 比较两个文件夹
我有两个文件夹,我想比较 在这两个文件夹中都有带编号的目录 文件夹a类似于=000123到000999 文件夹b类似于=000123文本到000999文本 现在我要匹配文件夹名称中的数字,当数字匹配时,我要将folderb\000123文本的内容复制到foldera\000123 我得到了下面的脚本,但这不起作用Powershell 比较两个文件夹,powershell,Powershell,我有两个文件夹,我想比较 在这两个文件夹中都有带编号的目录 文件夹a类似于=000123到000999 文件夹b类似于=000123文本到000999文本 现在我要匹配文件夹名称中的数字,当数字匹配时,我要将folderb\000123文本的内容复制到foldera\000123 我得到了下面的脚本,但这不起作用 $doel = "G:\Testkopiescript\" $regex = "^[0-9]*{6}" $bronfolder = Get-ChildItem "$p001" | F
$doel = "G:\Testkopiescript\"
$regex = "^[0-9]*{6}"
$bronfolder = Get-ChildItem "$p001" | ForEach-Object{$_.Name} | where {$_name -like $regex}
$checkfolder = Get-ChildItem "$doel*" | ForEach-Object{$_.Name} | where {$_name -like $regex}
foreach ($folder in $bronfolder){
$result = test-path -path $doel\$folder -Filter $regex
Copy-Item $m001\$folder $doel\$folder\ where {$_directoryname -like $folder}
}
我想这正是你想要的:
$source = 'C:\Temp\a'
$dest = 'C:\Temp\b'
$regex = '^\d{6}'
# Get all folders under folder 'a' whose name starts with 6 digits.
$sourceDirs = @(Get-ChildItem -Path $source|
Where-Object{$_.PSIsContainer -and $_.name -match $regex})
foreach($dir in $sourceDirs) {
# Get destination folders that match the first 6 digits of the
# source folder.
$digits = $dir.Name.Substring(0,6)
$destFolders = $null
$destFolders = @(Get-ChildItem "$dest\$digits*"| %{$_.FullName})
# Copy if the destination path exists.
foreach($d in $destFolders) {
Get-ChildItem -Path $dir.FullName|
%{Copy-Item -Path $_.FullName -Destination $d -Recurse}
}
}
我想这正是你想要的:
$source = 'C:\Temp\a'
$dest = 'C:\Temp\b'
$regex = '^\d{6}'
# Get all folders under folder 'a' whose name starts with 6 digits.
$sourceDirs = @(Get-ChildItem -Path $source|
Where-Object{$_.PSIsContainer -and $_.name -match $regex})
foreach($dir in $sourceDirs) {
# Get destination folders that match the first 6 digits of the
# source folder.
$digits = $dir.Name.Substring(0,6)
$destFolders = $null
$destFolders = @(Get-ChildItem "$dest\$digits*"| %{$_.FullName})
# Copy if the destination path exists.
foreach($d in $destFolders) {
Get-ChildItem -Path $dir.FullName|
%{Copy-Item -Path $_.FullName -Destination $d -Recurse}
}
}
另一种选择:
Get-ChildItem .\b | Where-Object {$_.PSIsContainer -and $_.Name -match '^\d{6}' -and (Test-Path ".a\$($_.name.substring(0,6))" -PathType Container) } | Foreach-Object{
Copy-Item -Recurse -Force -Path $_.FullName -Destination ".\a\$($_.name.substring(0,6))"
}
另一种选择:
Get-ChildItem .\b | Where-Object {$_.PSIsContainer -and $_.Name -match '^\d{6}' -and (Test-Path ".a\$($_.name.substring(0,6))" -PathType Container) } | Foreach-Object{
Copy-Item -Recurse -Force -Path $_.FullName -Destination ".\a\$($_.name.substring(0,6))"
}
没有文件被复制,我也没有得到任何错误。文件夹b中的文件夹名称是6位数字,然后是文本。示例:用于x8719的000123手册。000123与文件夹a中的一个目录的编号000123相同。“b”中的文件夹名称是否与“a”中的文件夹名称相同,还是只有6位数字匹配?这6位数字是唯一匹配的。没有复制任何文件,我也没有收到任何错误。文件夹b中的文件夹名称是6位数字,然后是文本。示例:用于x8719的000123手册。000123与文件夹a中编号为000123的目录相匹配。“b”中的文件夹与“a”中的文件夹名称相同,还是只有6位数字匹配?只有6位数字匹配