Trying to copy some files from server locally to network share as shown in the images below:
Folder 1
Folder 2
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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With