Sync Folders with Powershell - New and edited FilesDetermine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”Can PowerShell trap errors in GetChildItem and continue looping?How do you comment out code in PowerShell?Powershell - Variables Array's - Finding Folders then Files in FolderPowershell trying to set-content of folders instead of filesPowershell - Skip files that cannot be accessedPowerShell to find folder / file sizePowershell and previous folder versionsMove files of certain type from one folder to another

Incorrect syntax near '+' in stored procedure sql server

Why are Fuji lenses more expensive than others?

EU compensation - fire alarm at the Flight Crew's hotel

Is the name of an interval between two notes unique and absolute?

Paradox regarding phase transitions in relativistic systems

Edge style for certain types of nodes in TikZ-Tree

Can I separate garlic into cloves for storage?

Why is it called a Blood Knot?

Fun time! Guess what I am!

Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?

Delete empty subfolders, keep parent folder

Simulate a 1D Game-of-Life-ish Model

What to do as a player when ranger animal companion dies

How do rulers get rich from war?

As a discovery writer, how to complete unfinished novel (which is highly diverted from original plot ) after a time-gap

Can we store a whole string with the help of a single character pointer? If yes , How?

Quick Kurodoko Puzzle: Threes and Triples

Inquiry answerer

Is it safe to unplug a blinking USB drive after 'safely' ejecting it?

What is the origin of the “clerics can create water” trope?

How to create template of guides in Illustrator which appears with every document I open?

Why did Inosuke called Tanjiro as "Kentaro"?

I feel like most of my characters are the same, what can I do?

Dear Fellow PSE Users,



Sync Folders with Powershell - New and edited Files


Determine installed PowerShell versionPowerShell says “execution of scripts is disabled on this system.”Can PowerShell trap errors in GetChildItem and continue looping?How do you comment out code in PowerShell?Powershell - Variables Array's - Finding Folders then Files in FolderPowershell trying to set-content of folders instead of filesPowershell - Skip files that cannot be accessedPowerShell to find folder / file sizePowershell and previous folder versionsMove files of certain type from one folder to another






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to Sync 2 Folders with Powershell.
Comparing and copying any new Files works just fine. But I want to additionally copy all files that got modified in the reference Foler.



The following Code works and copys all new Files which got created in the reference Folder.



$folderReference = 'C:UsersAdministratorDesktopTestA'
$folderToSync = 'C:UsersAdministratorDesktopTestB'

$referenceFiles = Get-ChildItem -Recurse -Path $folderReference
$FolderSyncFiles = Get-ChildItem -recurse -Path $folderToSync

$fileDiffs = Compare-Object -ReferenceObject $referenceFiles -DifferenceObject $FolderSyncFiles

foreach ($File in $fileDiffs)
try
if ($File.SideIndicator -eq "<=")

$FullSourceObject = $File.InputObject.Fullname
$FullTargetObject = $File.InputObject.Fullname.Replace($folderreference, $folderToSync)

Write-Host "copy File: " $FullSourceObject
copy-Item -Path $FullSourceObject -Destination $FullTargetObject


catch
Write-Error -Message "Something went wrong!" -ErrorAction Stop




Now I also want to copy the modified Files.
I tried -property LastWriteTime after the Compare-Objectbut I get a WriteErrorException when running the code.



Do you guys have some tips on how to get this Code to run properly?
Thanks in advance










share|improve this question



















  • 1





    I'd just use robocopy, it's built specifically for this type of task

    – James C.
    Mar 28 at 14:17











  • +1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

    – Anthony Stringer
    Mar 28 at 14:19

















0















I am trying to Sync 2 Folders with Powershell.
Comparing and copying any new Files works just fine. But I want to additionally copy all files that got modified in the reference Foler.



The following Code works and copys all new Files which got created in the reference Folder.



$folderReference = 'C:UsersAdministratorDesktopTestA'
$folderToSync = 'C:UsersAdministratorDesktopTestB'

$referenceFiles = Get-ChildItem -Recurse -Path $folderReference
$FolderSyncFiles = Get-ChildItem -recurse -Path $folderToSync

$fileDiffs = Compare-Object -ReferenceObject $referenceFiles -DifferenceObject $FolderSyncFiles

foreach ($File in $fileDiffs)
try
if ($File.SideIndicator -eq "<=")

$FullSourceObject = $File.InputObject.Fullname
$FullTargetObject = $File.InputObject.Fullname.Replace($folderreference, $folderToSync)

Write-Host "copy File: " $FullSourceObject
copy-Item -Path $FullSourceObject -Destination $FullTargetObject


catch
Write-Error -Message "Something went wrong!" -ErrorAction Stop




Now I also want to copy the modified Files.
I tried -property LastWriteTime after the Compare-Objectbut I get a WriteErrorException when running the code.



Do you guys have some tips on how to get this Code to run properly?
Thanks in advance










share|improve this question



















  • 1





    I'd just use robocopy, it's built specifically for this type of task

    – James C.
    Mar 28 at 14:17











  • +1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

    – Anthony Stringer
    Mar 28 at 14:19













0












0








0








I am trying to Sync 2 Folders with Powershell.
Comparing and copying any new Files works just fine. But I want to additionally copy all files that got modified in the reference Foler.



The following Code works and copys all new Files which got created in the reference Folder.



$folderReference = 'C:UsersAdministratorDesktopTestA'
$folderToSync = 'C:UsersAdministratorDesktopTestB'

$referenceFiles = Get-ChildItem -Recurse -Path $folderReference
$FolderSyncFiles = Get-ChildItem -recurse -Path $folderToSync

$fileDiffs = Compare-Object -ReferenceObject $referenceFiles -DifferenceObject $FolderSyncFiles

foreach ($File in $fileDiffs)
try
if ($File.SideIndicator -eq "<=")

$FullSourceObject = $File.InputObject.Fullname
$FullTargetObject = $File.InputObject.Fullname.Replace($folderreference, $folderToSync)

Write-Host "copy File: " $FullSourceObject
copy-Item -Path $FullSourceObject -Destination $FullTargetObject


catch
Write-Error -Message "Something went wrong!" -ErrorAction Stop




Now I also want to copy the modified Files.
I tried -property LastWriteTime after the Compare-Objectbut I get a WriteErrorException when running the code.



Do you guys have some tips on how to get this Code to run properly?
Thanks in advance










share|improve this question














I am trying to Sync 2 Folders with Powershell.
Comparing and copying any new Files works just fine. But I want to additionally copy all files that got modified in the reference Foler.



The following Code works and copys all new Files which got created in the reference Folder.



$folderReference = 'C:UsersAdministratorDesktopTestA'
$folderToSync = 'C:UsersAdministratorDesktopTestB'

$referenceFiles = Get-ChildItem -Recurse -Path $folderReference
$FolderSyncFiles = Get-ChildItem -recurse -Path $folderToSync

$fileDiffs = Compare-Object -ReferenceObject $referenceFiles -DifferenceObject $FolderSyncFiles

foreach ($File in $fileDiffs)
try
if ($File.SideIndicator -eq "<=")

$FullSourceObject = $File.InputObject.Fullname
$FullTargetObject = $File.InputObject.Fullname.Replace($folderreference, $folderToSync)

Write-Host "copy File: " $FullSourceObject
copy-Item -Path $FullSourceObject -Destination $FullTargetObject


catch
Write-Error -Message "Something went wrong!" -ErrorAction Stop




Now I also want to copy the modified Files.
I tried -property LastWriteTime after the Compare-Objectbut I get a WriteErrorException when running the code.



Do you guys have some tips on how to get this Code to run properly?
Thanks in advance







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 14:16









KamiRyuKamiRyu

31 bronze badge




31 bronze badge










  • 1





    I'd just use robocopy, it's built specifically for this type of task

    – James C.
    Mar 28 at 14:17











  • +1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

    – Anthony Stringer
    Mar 28 at 14:19












  • 1





    I'd just use robocopy, it's built specifically for this type of task

    – James C.
    Mar 28 at 14:17











  • +1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

    – Anthony Stringer
    Mar 28 at 14:19







1




1





I'd just use robocopy, it's built specifically for this type of task

– James C.
Mar 28 at 14:17





I'd just use robocopy, it's built specifically for this type of task

– James C.
Mar 28 at 14:17













+1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

– Anthony Stringer
Mar 28 at 14:19





+1 for robocopy. can you share the error message you get? maybe put throw $_ in your catch to see what the error is

– Anthony Stringer
Mar 28 at 14:19












1 Answer
1






active

oldest

votes


















0
















I'd just use robocopy, it's built specifically for this type of task and included in most modern versions of windows by default:



robocopy C:Source C:Destination /Z /XA:H /W:5



  • /Z - resumes copy if interrupted


  • /XA:H - ignores hidden files


  • /W:5 - shortens wait for failures to 5 sec (default 30)

Worth taking a look through the documentation as there's many different options for practically every situation you can think of...



For example, add /MIR and it will remove any files from the destination when they are deleted from source.






share|improve this answer

























  • But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

    – KamiRyu
    Mar 28 at 14:29











  • No it won't, Robocopy doesn't copy files if they are identical in source/destination.

    – James C.
    Mar 28 at 14:30











  • Thank you. I got it working way easier than I thought!

    – KamiRyu
    Mar 28 at 14:59










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399822%2fsync-folders-with-powershell-new-and-edited-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0
















I'd just use robocopy, it's built specifically for this type of task and included in most modern versions of windows by default:



robocopy C:Source C:Destination /Z /XA:H /W:5



  • /Z - resumes copy if interrupted


  • /XA:H - ignores hidden files


  • /W:5 - shortens wait for failures to 5 sec (default 30)

Worth taking a look through the documentation as there's many different options for practically every situation you can think of...



For example, add /MIR and it will remove any files from the destination when they are deleted from source.






share|improve this answer

























  • But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

    – KamiRyu
    Mar 28 at 14:29











  • No it won't, Robocopy doesn't copy files if they are identical in source/destination.

    – James C.
    Mar 28 at 14:30











  • Thank you. I got it working way easier than I thought!

    – KamiRyu
    Mar 28 at 14:59















0
















I'd just use robocopy, it's built specifically for this type of task and included in most modern versions of windows by default:



robocopy C:Source C:Destination /Z /XA:H /W:5



  • /Z - resumes copy if interrupted


  • /XA:H - ignores hidden files


  • /W:5 - shortens wait for failures to 5 sec (default 30)

Worth taking a look through the documentation as there's many different options for practically every situation you can think of...



For example, add /MIR and it will remove any files from the destination when they are deleted from source.






share|improve this answer

























  • But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

    – KamiRyu
    Mar 28 at 14:29











  • No it won't, Robocopy doesn't copy files if they are identical in source/destination.

    – James C.
    Mar 28 at 14:30











  • Thank you. I got it working way easier than I thought!

    – KamiRyu
    Mar 28 at 14:59













0














0










0









I'd just use robocopy, it's built specifically for this type of task and included in most modern versions of windows by default:



robocopy C:Source C:Destination /Z /XA:H /W:5



  • /Z - resumes copy if interrupted


  • /XA:H - ignores hidden files


  • /W:5 - shortens wait for failures to 5 sec (default 30)

Worth taking a look through the documentation as there's many different options for practically every situation you can think of...



For example, add /MIR and it will remove any files from the destination when they are deleted from source.






share|improve this answer













I'd just use robocopy, it's built specifically for this type of task and included in most modern versions of windows by default:



robocopy C:Source C:Destination /Z /XA:H /W:5



  • /Z - resumes copy if interrupted


  • /XA:H - ignores hidden files


  • /W:5 - shortens wait for failures to 5 sec (default 30)

Worth taking a look through the documentation as there's many different options for practically every situation you can think of...



For example, add /MIR and it will remove any files from the destination when they are deleted from source.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 14:23









James C.James C.

9,3262 gold badges22 silver badges33 bronze badges




9,3262 gold badges22 silver badges33 bronze badges















  • But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

    – KamiRyu
    Mar 28 at 14:29











  • No it won't, Robocopy doesn't copy files if they are identical in source/destination.

    – James C.
    Mar 28 at 14:30











  • Thank you. I got it working way easier than I thought!

    – KamiRyu
    Mar 28 at 14:59

















  • But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

    – KamiRyu
    Mar 28 at 14:29











  • No it won't, Robocopy doesn't copy files if they are identical in source/destination.

    – James C.
    Mar 28 at 14:30











  • Thank you. I got it working way easier than I thought!

    – KamiRyu
    Mar 28 at 14:59
















But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

– KamiRyu
Mar 28 at 14:29





But this would just copy everything from Source to Destination. My goal is to copy only files which are new or modified in the Source Foler. Would that also be possible?

– KamiRyu
Mar 28 at 14:29













No it won't, Robocopy doesn't copy files if they are identical in source/destination.

– James C.
Mar 28 at 14:30





No it won't, Robocopy doesn't copy files if they are identical in source/destination.

– James C.
Mar 28 at 14:30













Thank you. I got it working way easier than I thought!

– KamiRyu
Mar 28 at 14:59





Thank you. I got it working way easier than I thought!

– KamiRyu
Mar 28 at 14:59








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399822%2fsync-folders-with-powershell-new-and-edited-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해