0

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

}`
wazzie
  • 23
  • 1
  • 8

1 Answers1

0

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

}`
Bonneau21
  • 544
  • 1
  • 5
  • 20
  • thanks for this, but it still generates an exception by saying that the file already exists. It doesn't output the "folder already exists" portion. – wazzie Jul 03 '18 at 16:10
  • folder already exists, copying files... copy-item : An item with the specified name \\wdevkofx127\C$\temp\Oracle Clients already exists. At C:\temp\copyfilesandfolders.ps1:28 char:4 + copy-item -Path $sourceRoot -Recurse -Destination "\\$computer\$de ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (\\wdevkofx127\C$\temp\Oracle Clients:String) [Copy-Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand – wazzie Jul 03 '18 at 16:11
  • I edit the script i add -force at the end of the copy-item try this – Bonneau21 Jul 03 '18 at 17:15
  • Now it looks like it's overwriting the existing files that have already been copied over. It still does not output the "folder already exists" portion. – wazzie Jul 05 '18 at 20:51
  • Yes the -force overwrite everything my bad. i'm trying to find another solution – Bonneau21 Jul 06 '18 at 14:04
  • You could use robocopy instead of copy-item like @Olaf suggest – Bonneau21 Jul 06 '18 at 14:07
  • Ok, thanks for the help Frederic. I will look into robocopy as I am not too familiar with it. – wazzie Jul 09 '18 at 18:11
  • Take a look at this script https://gallery.technet.microsoft.com/scriptcenter/Robocoy-Files-to-Remote-bdfc5154 – Bonneau21 Jul 09 '18 at 20:33