0
votes

I have created a script to copy the folder/sub folders/files from a specific location to a list of servers that I specified on a notepad.

It checks that if the folder has not been created, it will create it and copy over the files, but if the folder has already been created - then it stops.

However, I would like to still copy over newer files even if that folder has already been created, albeit with no subfolders or files in there.

My current code

[String] $KfxComputers = "C:\temp\Kofax Apps\servers.txt"

# This file contains the list of servers you want to copy files/folders to
$computers = get-content -Path $KfxComputers

# the folder you want to copy to the servers in the $computer variable
$sourceRoot = @("\\wdevkofx110\Kofax Software\Oracle Clients", 
            "\\wdevkofx110\Kofax Software\Kofax Capture 11")

# the destination location you want the file/folder(s) to be copied to
$destinationRoot = "C$\temp"

foreach ($computer in $computers) {

$testpath = Test-Path -Path \\$computer\$destinationRoot

if (!$testpath) 
{
    Write-Host "creating folder and copying files..." -ForegroundColor green

    New-Item -ItemType Directory -Force -Path "\\$computer\$destinationRoot"
    copy-item -Path $sourceRoot -Recurse -Destination 
    "\\$computer\$destinationRoot" -Container

} else {
        Write-Host "$computer\$destinationRoot folder already exists"
       }

}`
1
Why not simply using robocopy. It's made for copying files and folders. ;-)Olaf

1 Answers

0
votes

You can use Else IF

[String] $KfxComputers = "C:\temp\Kofax Apps\servers.txt"

# This file contains the list of servers you want to copy files/folders to
$computers = get-content -Path $KfxComputers

# the folder you want to copy to the servers in the $computer variable
$sourceRoot = @("\\wdevkofx110\Kofax Software\Oracle Clients", 
        "\\wdevkofx110\Kofax Software\Kofax Capture 11")

# the destination location you want the file/folder(s) to be copied to
$destinationRoot = "C$\temp"

foreach ($computer in $computers) {

$testpath = Test-Path -Path \\$computer\$destinationRoot

if (!$testpath) 
{
   Write-Host "creating folder and copying files..." -ForegroundColor green

   New-Item -ItemType Directory -Force -Path "\\$computer\$destinationRoot"
   copy-item -Path $sourceRoot -Destination 
   "\\$computer\$destinationRoot" -Container -Recurse -force 

} 

ElseIF ($testpath) {
   Write-Host "folder already exists, copying files..." -ForegroundColor green
   copy-item -Path $sourceRoot -Destination 
   "\\$computer\$destinationRoot" -Container -Recurse -force 
  }


 else {
    Write-Host "$computer\$destinationRoot folder already exists"
   }

}`