Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell moving files but backup the files before replacing

$
0
0

I have three directories: 1. RFC 2. Source 3. Backup

RFC contains files and folders(that contain files) that I need to replace in the source folder but before I replace/move files I need to backup the files I'm replacing from source to the backup folder.

I have wrote the following code, which compares RFC and Source and copies the files to backup, but it doesn't copy sub directories. I want it to move files within the sub directories as well with the same folder structure as Source. And once the copy of the files is done. I want to move files from RFC to Source.

Please any help would be highly appreciated.

 

$source = "C:\Scripts\Source\"
$backup = "C:\Scripts\Destination\"
$rfc_dir = "C:\Scripts\RFC000001234\"

$folder1Files=  dir $source
$folder2Files=  dir $rfc_dir

compare-object $folder1Files $folder2Files -property name -includeEqual -excludeDifferent | ForEach-object {
copy-item "$source\$($_.name)" -Destination "$backup" -Force -recurse
}

Viewing all articles
Browse latest Browse all 6937

Trending Articles