Я пытаюсь скопировать файлы в определенную папку на основе имени файла.
Например:
Текущая папка - C:\Stuff\Old Files\
Файл- 206.Little Rock.map.pdf
Папка назначения — D:\Cleanup\206\Repository
Таким образом, в основном начальный номер файла (206) является частью подпапки. «\Repository» останется постоянным. Изменится только начальное число.
Если бы файл был 207.Little Rock.map.pdf, папка назначения была бы
D:\Очистка\207\Репозиторий
Я начал с кода, который я получил отсюда, но я не уверен, как учесть изменение номера и как заставить его создать папку, если папка не существует. Таким образом, 206\Repository, вероятно, уже существует, но мне понадобится скрипт для создания папки, если это не так.
$SourceFolder = "C:\Stuff\Old Files\"
$targetFolder = "D:\Cleanup\"
$numFiles = (Get-ChildItem -Path $SourceFolder -Filter *.pdf).Count
$i=0
clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $SourceFolder ' to ' $targetFolder
Read-host -prompt 'Press enter to start copying the files'
Get-ChildItem -Path $SourceFolder -Filter *.PDF | %{
[System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $Name.Repository(".*","\"))
if(!(Test-Path -Path $destination.Directory )){
New-item -Path $destination.Directory.FullName -ItemType Directory
}
[int]$percent = $i / $numFiles * 100
copy-item -Path $_.FullName -Destination $Destination.FullName
Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose
$i++
}
Write-Host 'Total number of files read from directory '$SourceFolder ' is ' $numFiles
Write-Host 'Total number of files that was copied to '$targetFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;