Answered by:
Copy only new and Modified Files

Question
-
Hi,
i am using Xcopy soucre destination /E/Y to Copy only new and modifides folder/files to destination server.
How can i use the same in powershell and i want to capture errors if there are any.
Any idea?
-------- VinodFriday, July 29, 2011 11:06 AM
Answers
-
Yes, robocopy is better. You can invoke the robocopy via powershell. Below is one simple example.
$source = "c:\folder1"
$destination = "c:\folder2"
$copyoptions = "/MIR"
$command = "robocopy `"$($source)`" $($destination) $copyOptions"
$output = Invoke-Expression $command
Here you can use $LASTEXITCODE to determine the return code of above execution and $output will have the output that robocopy returns.
Hope this helps
Thanks,Sitaram Pamarthi
Blog : http://techibee.com
This posting is provided AS IS with no warranties or gurentees,and confers no rights
You don't even need to use Invoke-Expression for robocopy. PowerShell does a great job by itself in running the command with the switches. In fact, Invoke-Expression really isn't a best practice to use in a situation like this (See link below)http://blogs.msdn.com/b/powershell/archive/2011/06/03/invoke-expression-considered-harmful.aspx
Just doing this would work just as well:
robocopy "C:\folder1" "C:\folder2" /MIR
Alternately, if you do not want to hold the output in a variable, you can use the /LOG:"log.log" to save the output.
$LASTEXITCODE will still show the same result code that will allow you to determine the next action to take if it fails.
- Marked as answer by VinodKumar Pottal Friday, July 29, 2011 5:05 PM
Friday, July 29, 2011 1:51 PM -
Yes, robocopy is better. You can invoke the robocopy via powershell. Below is one simple example.
$source = "c:\folder1"
$destination = "c:\folder2"
$copyoptions = "/MIR"
$command = "robocopy `"$($source)`" $($destination) $copyOptions"
$output = Invoke-Expression $command
Here you can use $LASTEXITCODE to determine the return code of above execution and $output will have the output that robocopy returns.
Hope this helps
Thanks,Sitaram Pamarthi
Blog : http://techibee.com
This posting is provided AS IS with no warranties or gurentees,and confers no rights
- Proposed as answer by pamarths Friday, July 29, 2011 1:15 PM
- Marked as answer by VinodKumar Pottal Friday, July 29, 2011 5:06 PM
Friday, July 29, 2011 12:08 PM
All replies
-
I think is better used robocopy utility with the /mir.
Friday, July 29, 2011 11:24 AM -
Agree with Kazun. You can call robocopy from inside your Powershell script if you want, but robocopy was designed for this kind of work. Duplicating that in Powershell would involve doing a test-path and/or retrieving and comparing lastmodifiedtime on all the files in the directories. Robocopy can do that a lot more efficiently than just using native Powershell cmdlets.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "Friday, July 29, 2011 11:46 AM -
Yes, robocopy is better. You can invoke the robocopy via powershell. Below is one simple example.
$source = "c:\folder1"
$destination = "c:\folder2"
$copyoptions = "/MIR"
$command = "robocopy `"$($source)`" $($destination) $copyOptions"
$output = Invoke-Expression $command
Here you can use $LASTEXITCODE to determine the return code of above execution and $output will have the output that robocopy returns.
Hope this helps
Thanks,Sitaram Pamarthi
Blog : http://techibee.com
This posting is provided AS IS with no warranties or gurentees,and confers no rights
- Proposed as answer by pamarths Friday, July 29, 2011 1:15 PM
- Marked as answer by VinodKumar Pottal Friday, July 29, 2011 5:06 PM
Friday, July 29, 2011 12:08 PM -
http://robopowercopy.codeplex.com/
was featured on the scripting guy blog not long ago. havent tested it, but seems pretty neat.
Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Friday, July 29, 2011 1:18 PM -
Yes, robocopy is better. You can invoke the robocopy via powershell. Below is one simple example.
$source = "c:\folder1"
$destination = "c:\folder2"
$copyoptions = "/MIR"
$command = "robocopy `"$($source)`" $($destination) $copyOptions"
$output = Invoke-Expression $command
Here you can use $LASTEXITCODE to determine the return code of above execution and $output will have the output that robocopy returns.
Hope this helps
Thanks,Sitaram Pamarthi
Blog : http://techibee.com
This posting is provided AS IS with no warranties or gurentees,and confers no rights
You don't even need to use Invoke-Expression for robocopy. PowerShell does a great job by itself in running the command with the switches. In fact, Invoke-Expression really isn't a best practice to use in a situation like this (See link below)http://blogs.msdn.com/b/powershell/archive/2011/06/03/invoke-expression-considered-harmful.aspx
Just doing this would work just as well:
robocopy "C:\folder1" "C:\folder2" /MIR
Alternately, if you do not want to hold the output in a variable, you can use the /LOG:"log.log" to save the output.
$LASTEXITCODE will still show the same result code that will allow you to determine the next action to take if it fails.
- Marked as answer by VinodKumar Pottal Friday, July 29, 2011 5:05 PM
Friday, July 29, 2011 1:51 PM -
Hi All,
Thanks a ton for a great help. It was really usefull.
Finally, i have done something like this and it's working fine. Do let me know if there is any better way to do this?
$s = "C:\Test\Source"
$d = "C:\Test\Destination"
$o = "/E"
Robocopy $s $d $o $LASTEXITCODE
IF ($LASTEXITCODE -eq 1)
{
Write-host copying complted successfully
}
Else
{
Write-host Error Copying File
}
-------- VinodFriday, July 29, 2011 5:05 PM -
Leave out the $LASTEXITCODE from the robocopy parameters. This is an automatic variable that tells whether a command ran correctly or not. And a 0 means that it completed successfully, not a 1.
$s = "C:\Test\Source"
$d = "C:\Test\Destination"
$o = "/E"
Robocopy $s $d $oIF ($LASTEXITCODE -eq 0)
{
Write-host copying complted successfully
}
Else
{
Write-host Error Copying File
}- Proposed as answer by ajsongyjr Thursday, March 24, 2016 7:16 PM
Friday, July 29, 2011 5:49 PM -
Hi there,
I have published in another thread a script to compare binary files from within two directories. My script will go through all source and destination files and if both files exists it will call a CompareFileVersion function. If you replace that function with something else (comparison of FileAge and FileSize) you may get the right results.
If you want to get smart, you can use my "hash" table to store FileInfo objects instead of "FullName" value. With FileInfo value you have much more information in hand.
Threadname: Comparing version of 2 Dlls or exe
Wednesday, August 3, 2011 3:05 PM