Announcement

Collapse
No announcement yet.

Copy_Item Command does not overwrite files if they exist in the destination folder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Copy_Item Command does not overwrite files if they exist in the destination folder

    found this code online. It works in copying the folder to the destination only if the folder doesnt exist but if i run it again it creates a subfolder within the folder instead of overwriting the files. Can you please assist?




    $computers = gc "C:\Scripts\ServerList.txt"
    $source = "C:\Tools"
    $dest = "c$\Tools"
    foreach ($computer in $computers) {
    if (test-Connection -Cn $computer -quiet) {
    Copy-Item $source -Destination \\$computer\$dest -Recurse -Force -Verbose
    } else {
    "$computer is not online"
    }
    }

  • #2
    Re: Copy_Item Command does not overwrite files if they exist in the destination folde

    Originally posted by joeyg2391 View Post
    found this code online. It works in copying the folder to the destination only if the folder doesnt exist but if i run it again it creates a subfolder within the folder instead of overwriting the files. Can you please assist?




    $computers = gc "C:\Scripts\ServerList.txt"
    $source = "C:\Tools"
    $dest = "c$\Tools"
    foreach ($computer in $computers) {
    if (test-Connection -Cn $computer -quiet) {
    Copy-Item $source -Destination \\$computer\$dest -Recurse -Force -Verbose
    } else {
    "$computer is not online"
    }
    }
    What happens if you use just the command with the relevant details?

    i.e
    Code:
    Copy-Item c:\Tools -Destination \\computer\c$\Tools
    Just had another thought.

    Do you want to copy the entire folder to the c:\Tools folder or to the c

    The way you have it scripted at the moment it will copy to the c:\Tools, it will automatically create the c:\Tools folder. So you might actually want to change to this if that's what you want.

    Code:
    $computers = gc "C:\Scripts\ServerList.txt"
    $source = "C:\Tools"
    $dest = "c$"
    foreach ($computer in $computers) {
        if (test-Connection -Cn $computer -quiet) {
            Copy-Item $source -Destination \\$computer\$dest -Recurse -Force -Verbose
        } else {
            "$computer is not online"
    		   }
    		   }
    I haven't tested yet though.
    Last edited by wullieb1; 6th August 2013, 22:14.

    Comment


    • #3
      Re: Copy_Item Command does not overwrite files if they exist in the destination folde

      Right I've done some testing and based on what I think should be happening you need to change your script to the following

      Code:
      $computers = gc "C:\Scripts\ServerList.txt"
      $source = "C:\Tools\*"
      $dest = "c$\Tools"
      foreach ($computer in $computers) {
          if (test-Connection -Cn $computer -quiet) {
              Copy-Item $source -Destination \\$computer\$dest -Recurse -Force -Verbose
          } else {
              "$computer is not online"
      		   }
      		   }
      What your original script does is copy the entire folder tools to the destination, it will create the folder c:\tools on the destination if it doesn't exist.

      When the folder does already exist you are telling it to copy the folder and its contents to the destination.

      You need to add the wildcard to the source so that it copies the contents of the folder.

      You'll get more information here

      http://technet.microsoft.com/en-us/l.../ee176988.aspx

      This is a quick script that I just done that will check if the destination folder exists and create it if not. Then copy the relevant files in the source to the destination. Quick and dirty but gets the job done.

      Code:
      $source = "C:\Tools\*"
      $dest = "C:\Tools2\"
      #Check if the destination folder exists.
      if (Test-Path $dest)
      {
      	#If it does run the copy command
      	"Folder Exists"
         	Copy-Item $source -Destination $dest -Recurse
      }
      else
      {
      	#If it doesn't create it and then run the copy command
          "Folder does not exist"
          New-Item c:\Tools2 -type directory
      	Copy-Item $source -Destination $dest -Recurse
      }

      Comment

      Working...
      X