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.
When I run the following: $source = "C:\Users\x326269\Documents\Scripts\Source"
$target = "C:\Users\x326269\Documents\Scripts\Destination"
$rfc_dir = "C:\Users\x326269\Documents\Scripts\RFC000001234"
$folder1Files= dir $source -Recurse
$folder2Files= dir $rfc_dir -Recurse
Sorry about that, should have looked at the code a bit closer before replying. I can think of a couple different ways to address this. You could either write a recursive function, or do a little bit of string manipulation to convert between relative and absolute paths; relative for comparing the folder contents, and absolute for the copy operation. Both should work; I went with the latter in this example. Give it a try:
Note: I wrote this to be PowerShell 2.0-compatible, since I wasn’t sure what version you are running. You can get rid of the Where-Object calls and use the -File switch on Get-ChidlItem, if you’re running PowerShell 3.0 and prefer it that way.