jeudi 14 novembre 2019

Using 'If' and 'ElseIf' with get-childitem from different directories

I'm new to PowerShell and I've put together some code which in brain should work but I can't piece it all together. There are 3 directories that I want to take the files from out of 20 directories. And I have a GUI set up to output a $year variable. The options being 2017, 2018, 2019 and Select All. With the files I want to copy them to a different folder, preferably with folder structure intact.


$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

This was the idea, It doesn't work. I want the output of this to be put into the variable $files because of the code below. I am more than open to alternative ways of doing this but this seemed the most logical from a newbie perspective.


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  
            }  
        }  
    }  
}
``

Aucun commentaire:

Enregistrer un commentaire