Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UPDATE</p> <p>TL;DR -- the script</p> <pre><code>$RFC_Folder = 'c:\scripts\rfc' $SOURCE_Folder = 'c:\scripts\source' $BACKUP_Folder = 'c:\scripts\backup' $rfc = get-ChildItem -File -Recurse -Path $RFC_Folder $source = get-ChildItem -File -Recurse -Path $SOURCE_Folder compare-Object -DifferenceObject $rfc -ReferenceObject $source -ExcludeDifferent -IncludeEqual -Property Name -PassThru | foreach-Object { # copy SOURCE to BACKUP $backup_dest = $_.DirectoryName -replace [regex]::Escape($SOURCE_Folder),$BACKUP_Folder # create directory, including intermediate paths, if necessary if ((test-Path -Path $backup_dest) -eq $false) { new-Item -ItemType Directory -Path $backup_dest | out-Null} copy-Item -Force -Path $_.FullName -Destination $backup_dest #copy RFC to SOURCE $rfc_path = $_.fullname -replace [regex]::Escape($SOURCE_Folder),$RFC_Folder copy-Item -Force -Path $rfc_path -Destination $_.FullName } </code></pre> <p>The explanation:</p> <p>Given the OPs comment below on <code>ROBOCOPY</code> not being preferable I've updated the answer.</p> <p>Your posted script is basically on the right track, however instead of using just <code>$backup</code> you have to get a little fancy with the <code>-Destination</code> parameter. You don't want the <code>-Destination</code> to be the static path <code>c:\scripts\backup</code> you want it to update based on where the source file actually was. For example if the file was in <code>c:\scripts\source\subdir1\subdir2</code>, you'd want <code>-Destination</code> to be <code>c:\scripts\backup\subdir1\subdir2</code>.</p> <p><code>$_.FullName</code> will be the path to the <code>$SOURCE_Folder</code> as it was used as the <code>-ReferenceObject</code>. It's getting manipulated stringwise to create the desired RFC and Backup paths. The <code>[regex]::Escape</code> static method is being used because the <code>-replace</code> operator does a regular expression operation on the strings, and several characters in paths need to be escaped (the slash, mainly). All it's doing is turning <code>c:\scripts\source</code> into a regular expression escaped version: <code>c:\\scripts\\source</code></p> <p>The <code>if</code> construct is used because <code>copy-Item</code> doesn't create intermediate directories, but <code>new-Item</code> does.</p> <p>Depending on your specifics this might work as is, but you may have to alter it. For example if somehow a directory can end up in RFC that wasn't in SOURCE, this wouldn't catch that. It also won't catch any empty directories that are in SOURCE and RFC, if that's important.</p> <hr> <p>It may be that PowerShell isn't the best tool for this job, depending on some other factors. As I understand it you have <code>Source</code>, <code>RFC</code> and <code>Backup</code> folders. The <code>RFC</code> folder will contain changes that need to be committed to <code>Source</code>, but before you do that you want to backup <code>Source</code> to <code>Backup</code>. If the folder structures are all similar between them, then perhaps the command line tool <code>ROBOCOPY</code> could do what you need?</p> <p>If some of my assumptions are correct you'd first mirror the <code>Source</code> folder with the <code>Backup</code> folder. This would contain the pre-changed files. Then you would mirror the <code>RFC</code> folder to <code>Source</code>, this would commit any changed files/folders from <code>RFC</code> to the <code>Source</code> folder. An example (this is a batch file):</p> <pre><code>REM Mirror the Source folder to Backup ROBOCOPY C:\Scripts\Source C:\Scripts\Backup /MIR REM Mirror the RFC folder to Source ROBOCOPY C:\Scripts\RFC C:\Scripts\Source /MIR </code></pre> <p>At the end of all this your <code>Source</code> folder would be an exact replica of whatever the <code>RFC</code> folder looked like. If <code>RFC</code> isn't a full copy of <code>Source</code>, but rather a partial copy, then you wouldn't want to use the mirror switch, <code>/MIR</code>, as it would destroy anything in <code>Source</code> that wasn't in <code>RFC</code>.</p> <p>Browse around <code>ROBOCOPY /?</code> for some of its other switches, it's got a few interesting ones for logging if you want to build in some auditing capability. Also, make extra sure to test this in a test environment. Misuse of ROBOCOPY with a /MIR switch might make you a very sad camper.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload