I am trying to copy files in one folder to another folder. Files should go in the same folder structure under their parent folders. For example, C:\Users\Boss\One\Two\Alpha\File1.txt should go to C:\Users\Boss\One\Two\Beta\File1.txt
It fails in the last line where I specify the Destination value in a Copy-Item:
$sourcedir = "C:\Users\Boss\One\Two\Alpha"
$localdir = "C:\Users\Boss\One\Two\Beta"
# Assumes folders are established by other processes
$sourceContents = Get-ChildItem $sourcedir -Recurse | Where-Object { -not $_.PsIsContainer }
$localContents = Get-ChildItem $localdir | Where-Object { -not $_.PsIsContainer }
Compare-Object $sourceContents $localContents -Property Name,Length,LastWriteTime -PassThru |
Where-Object {$_.SideIndicator -eq "<="} |
ForEach-Object {
Write-Output $_.FullName, $_.Name;
Write-Output $localdir;
$a = Join-Path $localdir $_.FullName.SubString($sourcedir.Length);
Write-Output $a;
Write-Output $_;
# Write-Outputs are just to see what's going on
# Works as expected up until here
Copy-Item $_ -Destination "$a"
}
I get, in this case, an error saying, "Cannot find path 'C:\Users\Boss\File1.txt because it does not exist."
I have tried a number of things and ended up with the above trying different ways to feed the Destination value.
When I accomplish this, the source needs to become a network share. I thought of using Robocopy early on, but I need to migrate this yet again to using a URL for the source.
Any thoughts would be greatly appreciated. I program in C# .NET but I've always found PowerShell a challenge to learn.
Using Theo's example, I simplified my code to the following. Note that I moved the folders to one outside of the Users and not in any synchronised folder.
$sourcedir = "C:\Unsynchronised\Alpha"
$localdir = "C:\Unsynchronised\Beta"
# I kept the "local" because I would like to use this for server-client some day
Get-ChildItem -Path $sourcedir -Recurse -File | ForEach-Object {
$localDirPath = Join-Path -Path $localdir -ChildPath $_.DirectoryName.Substring($sourcedir.Length)
$localDirFile = Join-Path -Path $localDirPath -ChildPath $_.Name
Write-Output $_.DirectoryName
Write-Output $localDirPath
Write-Output $localDirFile
$_ | Copy-Item -Destination %localDirPath
}
Output gives...
C:\Unsynchronised\Alpha
C:\Unsynchronised\Beta\
C:\Unsynchronised\Beta\TestFile.txt
C:\Unsynchronised\Alpha\Folder1
C:\Unsynchronised\Beta\Folder1
C:\Unsynchronised\Beta\Folder1\Subfile_1_1.txt
Files in Beta remain unchanged despite newer ones of the same name in Alpha.