Simple PS task causing me PAIN!!!

I am trying to complete a simple of task of looking at a source folder for certain file extensions and if each file doesn't exist in the Destination, copy the file.

The code i am using is below and this works however the "test-path" section doesn't work as it always copies the files. This seems such a simple task and no mount of searching online has helped at all.

#Source(s) and Destination(s) of files to use attributed to different variables.
$source = "C:\Folder1"
$dest = "C:\Folder2"

#Groups files of a certain extension into variables to use later
$srctxt = Get-ChildItem $source -Filter *.txt

foreach ($file in $srctxt)
{

If(!(Test-Path -path "$dest))
    {
        "Files Exists - No further action required" 
    }
else
    {
        "Files do not exist. Missing files will now be copied" 
        Copy-Item -Path $source\*.txt -Destination $dest -Verbose
    }
}

Appreciate your help

February 14th, 2015 4:15pm

Hi,

Try it this way:

$sourceFolder = 'C:\F1'
$destinationFolder = 'C:\F2'

Get-ChildItem $sourceFolder *.txt | ForEach {

    If (!(Test-Path (Join-Path -Path $destinationFolder -ChildPath $_.Name))) {

        Copy-Item -Path $_.FullName -Destination $destinationFolder

    }

}


Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 4:32pm

Thanks Mike... that worked a treat. Appreciate the assistance.
February 15th, 2015 3:26pm

Cheers, you're very welcome.
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 4:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics