将'If'和'ElseIf'与来自不同目录的get-childitem一起使用

Wil*_*ker 0 powershell if-statement get-childitem

我要从3个目录中提取3个目录。我设置了一个GUI以输出$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
Run Code Online (Sandbox Code Playgroud)

这就是想法,它不起作用。由于以下代码,我希望将此输出放入变量$ 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Adm*_*ngs 5

在进行比较的条件语句中,应考虑使用Comparison_Operators。等于由运算符表示-eq=用于变量分配。您可以转换代码以反映这一点。if-else块的输出可以设置为变量($files)。

$files = 
    If ($year -eq '2017') {
        Get-ChildItem -Path $sourcePath'\Warranty Claims 2017'  -Recurse
     } 
    ElseIf ($year -eq '2018') {
        Get-ChildItem -Path $sourcePath'\Warranty Claims 2018'  -Recurse
    }   
    ElseIf ($year -eq '2019') {
        Get-ChildItem -Path $sourcePath'\Warranty Claims 2019'  -Recurse
    }     
    ElseIf ($year -eq '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't work"
    }
Run Code Online (Sandbox Code Playgroud)

如果您的情况特殊,除了操作员问题外,您可能会得到意想不到的结果。考虑以下示例:

If ($year = '2017') {
   "It is 2017"
} Else {
    "Wrong year"
}

It is 2017
$year
2017
Run Code Online (Sandbox Code Playgroud)

如果$year可以成功分配一个$true条件中通常可以求值的值,则if条件将为true, $year使用新值进行更新。

考虑下面的示例,其中的if不会为$true。在这里0评估到$false 并且分配成功更新$year0

If ($year = 0) {
   "It is 2017"
} Else {
    "Wrong year"
}

Wrong year
$year
0
Run Code Online (Sandbox Code Playgroud)