Hi /r/PowerShell,
I’m sure I’m making a meal out of this problem but I could do with some help.
I have written a PS script that will run daily as a scheduled task. Its job is to copy any new files from one folder location (G:BackupsPartner which is on the local server) to a network share (kslnas01BackupsPartner Backups which is on another server).
At the moment it works fine for copying files that live in that folder, but fails to copy ones that live in the subfolders. What I want is it to find all the files, even in subfolders (which it does), and then copy them to the new location. if the file is in a subfolder that doesn’t exist, I want it to create a matching subfolder on the new location and copy the file there.
I know why this isn’t working (because I haven’t figured out how to capture the subfolder name i want to create and then actually create it) but don’t know how to do it.
Can anyone help please?
This is the script:
$sourcefiles = "" $testpath = "" $file = "" $source = "G:BackupsPartner" $destination = "\kslnas01BackupsPartner Backups" $sourcefiles = get-childitem $source -Recurse foreach ($file in $sourcefiles) { $path = "$destination$file" $testpath = test-path $destination$file if(!$testpath) { copy-item -Path $source$file -Destination $destination } }
submitted by /u/danblank000
[link] [comments]
The post Help with copy-item script appeared first on How to Code .NET.