1

I'm scratching my head on this one and haven't found anything that gets it working.

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

1 Answer 1

0

I adapted the answer from this post.

$sourceDir = 'My-Current-Root-Folder'
$targetDir = 'My-New-Root-Folder'

Get-ChildItem $sourceDir -filter "*" -recurse | ForEach-Object {
    $targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length)

    If ( $_.PSIsContainer -eq $True ) {
        If (-not( Test-Path $targetFile )) {
            New-Item -ItemType Directory -Path $targetFile -Force
        }
    } else {
        Move-Item $_.FullName -destination $targetFile
    }
}

It's really slow but it works.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that but I need to keep the select-string logic in the get-childitem line because the folder move is needed for files with specific content in the JSON files I'm checking. When I add that in, I find that using $_.FullName or similar objects inside the forEach loop returns null values and can't be used. That's why I ended up using the Split-Path. I'm also trying to move the parent folder of the files I'm iterating. I'll keep working on this and try some variations on your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.