Powershell для объединения каталогов - PullRequest
1 голос
/ 16 января 2020

Я пытаюсь использовать powershell для объединения каталогов. Я пытаюсь избавиться от родительских каталогов, поскольку они не нужны (восстановленные файлы с неисправного диска). Таким образом, пример текущей структуры: JunkFolder1> RecoveredDirectory> Year> ClientDirectory

Существует несколько нежелательных папок, и они могут иметь или не иметь одинаковые RecoveredDirectory, Year, ClientDirectory.

Я хочу запустить скрипт, который удалит все ненужные папки и объединит остальные, не удаляя файлы на уровне ClientDirectory.

Я супер новичок в PS, но подумал, что это будет быть лучшим маршрутом. Спасибо!

1 Ответ

0 голосов
/ 16 января 2020

Используйте этот скрипт:

Param(
[Parameter(Mandatory=$true)]
[string]$sourcePath,
[Parameter(Mandatory=$true)]
[string]$destinationPath
)


$files = Get-ChildItem -Path $sourcePath -Recurse -Filter "*.*"

foreach($file in $files){
    $sourcePathFile = $file.FullName
    $destinationPathFile = $file.FullName.Replace($sourcePath,  $destinationPath)

    $exists = Test-Path $destinationPathFile

    If (!$exists){
        $dir = Split-Path -parent $destinationPathFile
        if (!(Test-Path($dir))) { New-Item -ItemType directory -Path $dir }
        Copy-Item -Path $sourcePathFile -Destination $destinationPathFile -Recurse -Force
    }
    else{
        $isFile = Test-Path -Path $destinationPathFile -PathType Leaf

        if($isFile){
            $different = Compare-Object -ReferenceObject $(Get-Content $sourcePathFile) -DifferenceObject $(Get-Content $destinationPathFile)
            if(Compare-Object -ReferenceObject $(Get-Content $sourcePathFile) -DifferenceObject $(Get-Content $destinationPathFile)){
            $dir = Split-Path -parent $destinationPathFile
            if (!(Test-Path($dir))) { New-Item -Type directory -Path $dir }

                Copy-Item -Path $sourcePathFile -Destination $destinationPathFile -Recurse -Force
            }
        }
    }
}

Позвоните по этому номеру: ScriptName.ps1 "SourceDir" "DestinationDir"

Атрибуция: https://dejanstojanovic.net/powershell/2018/february/merge-folders-with-windows-powershell-script/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...