Robinsper
Robinsper

Reputation: 79

Powershell - Move files to existing folders with partially matching names

I have a folder with images that multiple people will be contributing, and I have multiple folders that other people will be creating with additional content, the folder names will be similar but not identical to the file names (see example below). I have tried writing a Powershell script (I'm still a bit green with it) that moves each image to its respective folder but I hit a wall, I'm getting the "Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Destination'. Specified method is not supported." I tried converting the destination to a string but had trouble looping through the list of folders. I'm adding the code here in hopes someone will spot the error I made in the code and will point me in the right direction. Thanks in advance for taking the time to help.

$sourceFolder = Get-ChildItem 'C:\Example_Files_Folder' -Recurse -Filter *.png
$targetFolder = Get-ChildItem -Path 'C:\Example_Target_Folder' -Recurse -Directory

foreach ($file in $sourceFolder) {
        Where-Object { $targetFolder.Name -cin $sourceFolder.BaseName }
        move-Item -Path $file.FullName -Destination $targetFolder.FullName -WhatIf
        Write-Output "...copying $file" 
}


<# ### Folder Structure
▼sourceFolder #Root Folder containing files
├───►looking inside this folder.png
├───►you should be able to find.png
├───►quite a long list of files.png
├───►matching many folders names.png
└───►that exist inside other folders.png


▼targetFolder #Root Folder
├───▼subfolder01
│   └───►looking inside #this is a folder
├───▼subfolder02
|   └───▼subfolder002
|       └───▼subfolder0002
|           └───►you should be #also a folder
├───▼subfolder03
|   └───►quite a long #not a file, just a folder
├───▼subfolder04
|   └───►matching many folders #folder
├───▼subfolder05
|   └───►sometimes with no matching file name #yep, a folder
├───▼subfolder06
|   └─────►that exist within many folders #you get it
└───►subfolder07
### #>

Upvotes: 0

Views: 937

Answers (1)

postanote
postanote

Reputation: 16076

Continuing from my comment ... You can simplify this to say something like this:

# Target folder tree
$targetFolders = Get-ChildItem -Path 'D:\Temp\Target' -Recurse -Directory
# Results
<#
    Directory: D:\Temp\Target


Mode          LastWriteTime Length Name    
----          ------------- ------ ----    
d-----  17-Feb-21     10:22        Book    
d-----  17-Feb-21     10:10        TextData


    Directory: D:\Temp\Target\Book


Mode          LastWriteTime Length Name
----          ------------- ------ ----
d-----  17-Feb-21     10:10        Date
#>

# Source folder files to move
Get-ChildItem 'D:\Temp' -Recurse -Filter '*.txt' | 
ForEach {
    ForEach ($targetFolder in $targetFolders)
    {
        If ($PSItem.BaseName -match $targetFolder.Name)
        {Move-Item -Path $PSItem.FullName -Destination $targetFolder.FullName -WhatIf}
    }
}
# Results
<#
What if: Performing the operation "Move File" on target "Item: D:\Temp\Book- Copy (2).txt Destination: D:\Temp\Target\Book\Book- Copy (2).txt".
....
What if: Performing the operation "Move File" on target "Item: D:\Temp\DateData.txt Destination: D:\Temp\Target\Book\Date\DateData.txt".
...
What if: Performing the operation "Move File" on target "Item: D:\Temp\TextData.txt Destination: D:\Temp\Target\TextData\TextData.txt".
#>

Upvotes: 1

Related Questions