Я собрал скрипт, который рекурсивно копирует из одного каталога в другой, пропуская файлы с определенным шаблоном в имени файла:
function Copy-RevitFiles ([string]$source, [string]$destination, [boolean]$recurse) {
$pattern = '\.\d\d\d\d\.[RVT]'
if ($recurse) {$files = Get-ChildItem $source -Recurse}
else {$files = Get-ChildItem $source}
$files | ForEach-Object {
if ((Select-String -InputObject $_.Name -pattern $pattern -AllMatches -quiet) -eq $null) {
#Write-Host $_.Name
#Write-Host $_.Fullname
#Write-Host "$($destination)\$($_.FullName.TrimStart($source))"
Copy-Item $_.FullName -Destination "$($destination)\$($_.FullName.TrimStart($source))" #add on full name of item, less $source start end of file path
#Write-Host "----------"
}
}
}
В большинстве случаев он работает хорошо.Однако у меня проблема в том, что внутри каждой папки создается дополнительная подпапка с файлами в ней.Например:
Если ввести источник как каталог с такой структурой:
Source
-file1.rvt
-file1.0225.rvt (will not copy as it matches the pattern)
-file1.0226.rvt (will not copy as it matches the pattern)
-folder1
|-file2.rvt
|-file2.0121.rvt (will not copy as it matches the pattern)
|-file2.0122.rvt (will not copy as it matches the pattern)
-folder2
Я ожидаю, что в целевой папке будет создана следующая структура:
Destination
-file1.rvt
-folder1
|-file2.rvt
-folder2
Но вместо этого я получаю:
Destination
-file1.rvt
-folder1
|-file2.rvt
|-folder1 (extra folder not in source)
-folder2
Есть идеи, где я ошибаюсь?