I have a folder tree with a root1\yyyy\mm\dd\hh\uniqueid structure and need to move the uniqueid sub folders into a root2\yyyy\mm\dd\hh tree based on content within a specific json file in each uniqueid folder
After several searches I've arrived at the code below which is correctly identifying the folders I need to move, and creating the root2\yyyy\mm\dd\hh folders to hold the moved uniqueid folders, but it gives an access denied error trying to execute the move cmdlet
$root1 = My-Current-Root-Folder
$root2 = My-New-Root-Folder
(Get-ChildItem -Literalpath $root1 -Recurse -Filter *.json) | Select-String -Pattern content-I-am-looking-for | ForEach-Object {
$folderToMove = (Split-Path -Parent $_.Path)
$destinationFolder = $folderToMove.Substring(0, $folderToMove.Length - 33).Replace($root1 , $root2)
write-host $folderToMove #This correctly displays the source folders I need to move
write-host $destinationFolder #This correctly displays the root2 folder structure to hold the moved folders
If(!(Test-Path $destinationFolder)){
New-Item -Path $destinationFolder -ItemType "directory" #This correctly created the root2 folders for the move
}
move-item -path $folderToMove -destination $destinationFolder -Force
}
The error is like this:
move-item : Access to the path '$root1\2023\10\19\01\uniqueid' is denied.
But if I execute a move-item command explicitly specifying one of the uniqueid folders and the associated root2\yyyy\mm\dd\hh destination, the folder is moved without any issue.
All advice greatly appreciated
Tried wrapping the source and destination in double quotes in case that was needed and also specifying [string] on the source and destinations, but that just gives a different error (cannot find a drive F)
0 comments:
Post a Comment
Thanks