Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell to copy files over network

Trying to copy some files from server locally to network share as shown in the images below:

Folder 1

enter image description here

Folder 2

enter image description here

I need to achieve is copy all the contents of ROOT folder under SOURCE to DESTINATION folder. But each time it copies the content, I would like it to create a new folder with same destination folder name but suffixed with some sort of sequential numbering. The number of folders & files within the SOURCE/ROOT folder always stays the same (2 folders & 2 files). I just need all those source content go into a new folder on the destination each time I run the script. Here is an example of a script I have been trying out but it's just not doing what I need:

$date = Get-Date -format MMMM-dd-yyyy
$date2 = Get-Date -format yyyyMMdd

$Source = "C:\SOURCE\ROOT"

$Destination1 = "\\netshare\DESTINATION\DATA_May-26-2014"
$Destination2 = "\\netshare\DESTINATION\DATA_May-26-2014-1st"
$Destination3 = "\\netshare\DESTINATION\DATA_May-26-2014-2nd"
$Destination4 = "\\netshare\DESTINATION\DATA_May-26-2014-3rd"
$Destination5 = "\\netshare\DESTINATION\DATA_May-26-2014-4th"
$Destination6 = "\\netshare\DESTINATION\DATA_May-26-2014-5th"

#Check destination path
if (Test-Path $Destination1)
 {
  #then copy
  robocopy $Source $Destination2 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination2)
 {
  #then copy
  robocopy $Source $Destination3 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination3)
 {
  #then copy
  robocopy $Source $Destination4 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination4)
 {
  #then copy
  robocopy $Source $Destination5 /MIR /Z /E /fft /MAXAGE:$date2
 }
if (Test-Path $Destination5)
 {
  #then copy
  robocopy $Source $Destination6 /MIR /Z /E /fft /MAXAGE:$date2
 }
else
 {
  robocopy $Source $Destination1 /MIR /Z /E /fft /MAXAGE:$date2
 }

I was initially using robocopy only for a similar purpose but this copying project needed the folder to have the month name along with it (as in "May") which was not available with robocopy and so I had to switch to powershell script and combine robocopy.

Any ideas on how I can create a better script?

like image 533
user3679127 Avatar asked May 27 '14 09:05

user3679127


People also ask

How do I copy a directory in multiple computers using PowerShell?

PowerShell Copy File to Multiple Computers When you need to copy a file to multiple computers then you have to use ForEach loop. Which executes the copy command one by one on each computer mentioned in the command.

What is the PowerShell command including parameters to copy that file?

The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it can't copy a file to a certificate drive. This cmdlet doesn't cut or delete the items being copied.


1 Answers

Use Copy Item. Robocopy would be cool if you were doing a true mirror, in my experience using copy item is better for most cases in PowerShell. If you want a unique name create a TimeStamp variable and pass that in at the end, or similar.

$TimeStamp = get-date -f yyyyMMddhhmm
$Destination = "\\netshare\DESTINATION\DATA_" + $TimeStamp
New-Item -ItemType directory -Path $Destination -Force
Copy-Item -Path $Source\*.* -Destination $Destination -Force

Also if you stick with RoboCopy you'll need to check for exit level if running inside of some other tool(like TeamCity).

(ROBOCOPY $Source $Destination /MIR /W:30 /R:10) ^& IF %%ERRORLEVEL%% LEQ 1 exit 0

Creates an actual error code based on error level. Which for RoboCopy, ErrorLevel 1 actually means success, 0 means no change.

like image 177
Jason Hughes Avatar answered Oct 12 '22 11:10

Jason Hughes