Скопировать файл в папку на основе папки, созданной из файла - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть сценарий PowerShell, который просматривает каталог, и, если есть какие-либо типы файлов .xlsx, он создает папку на основе имени файла каждого типа файла xlsx и копирует файл в каждую папку.Эта часть работает.Теперь мне нужно скопировать файлы из \\ paculfs3 \ Deptfiles \ SharedFiles \ Compliance \ Создание сертификата \ Размещение в каждой папке при ее создании.

Вот мой код:

$SourceFolder = "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation"
$TargetFolder = "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation"

Get-ChildItem -Path $SourceFolder -Filter *.xlsx | ForEach-Object {
    $ChildPath = Join-Path -Path $_.Name.Replace('.xlsx','') -ChildPath $_.Name

    [System.IO.FileInfo]$Destination = Join-Path -Path $TargetFolder -ChildPath $ChildPath

    if (-not (Test-Path -Path $Destination.Directory.FullName)) {
        New-Item -ItemType Directory -Path $Destination.Directory.FullName
    }

    Copy-Item -Path $_.FullName -Destination $Destination.FullName
    Copy-Item -Path "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation\Staging\Compliance Webinar Certificate Template (1).docx" -Destination $Destination -Force
    Copy-Item -Path "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation\Staging\Email Mail Merge for Certificates.docx" -Destination $Destination -Force
    Copy-Item -Path "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation\Staging\Manual Attendee Info List.xlsx" -Destination $Destination -Force
}

1 Ответ

0 голосов
/ 27 ноября 2018

IMO, вы слишком усложняете вещи, чтобы добавить имя без extenson просто Join-Path $TargetPath $_.BaseName

Чтобы уменьшить избыточность с такими же длинными путями, используйте переменную, которую вы уже определили.

$SourceFolder = "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation"
$TargetFolder = "\\paculfs3\Deptfiles\SharedFiles\Compliance\Certification Creation"

Get-ChildItem -Path $SourceFolder -Filter *.xlsx | ForEach-Object {
    $Destination = Join-Path -Path $TargetFolder -ChildPath $_.BaseName
    New-Item -ItemType Directory -Path $Destination -Force | Out-Null
    $_ | Copy-Item -Destination $Destination
    Copy-Item -Path "$SourceFolder\Staging\Compliance Webinar Certificate Template (1).docx" -Destination $Destination -Force
    Copy-Item -Path "$SourceFolder\Staging\Email Mail Merge for Certificates.docx" -Destination $Destination -Force
    Copy-Item -Path "$SourceFolder\Staging\Manual Attendee Info List.xlsx" -Destination $Destination -Force
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...