Есть 3 каталога, из которых я хочу взять файлы из 20 каталогов. И у меня есть графический интерфейс, настроенный для вывода переменной $year
. Возможные варианты: 2017, 2018, 2019 и Выбрать все. С файлами я хочу скопировать их в другую папку, желательно с неповрежденной структурой папок.
$year = '2018'
if ($year = '2017') {
Get-ChildItem -Path $sourcePath'\Warranty Claims 2017' -Recurse
} elseif ($year = '2018') {
Get-ChildItem -Path $sourcePath'\Warranty Claims 2018' -Recurse
} elseif ($year = '2019') {
Get-ChildItem -Path $sourcePath'\Warranty Claims 2019' -Recurse
} elseif ($year = 'Select All') {
Get-ChildItem -Path $sourcePath'\Warranty Claims 2017'
Get-ChildItem -Path $sourcePath'\Warranty Claims 2018'
Get-ChildItem -Path $sourcePath'\Warranty Claims 2019'
} else {
"This didn'nt work"
}
# = files
Это была идея, это не работает. Я хочу, чтобы результат этого был помещен в переменную $ files из-за приведенного ниже кода. Я более чем открыт для альтернативных способов сделать это, но с точки зрения новичка это показалось наиболее логичным.
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 -ItemType Directory -Path $dir }
Copy-Item -Path $sourcePathFile -Destination $destinationPathFile -Recurse -Force
}
}
}
}
if
) равенство представляется оператором-eq
, а не=
.=
выполняет задание. - person AdminOfThings   schedule 14.11.2019$files = if () {}; else {}
- person AdminOfThings   schedule 14.11.2019