cunhaigo23

New Member
Joined
Jul 7, 2021
Messages
1
Greetings, I need a Script that separates files According to destination folder name. Example if in the destination folder there is a folder named Paulo, it searches in the source folder where the files are located to separate files that have the name Paulo.

Origin

Paulo24567.mp4

Destiny

Paulo folder

Thank you very much
 


Solution
Powershell version. Supports sub directories on the source and can handle duplicate names on destination.

Code:
$SourceDirectory = ".\SOURCE"
$DestinationDirectory = ".\DEST"

$Directories = Get-ChildItem -Path $DestinationDirectory -Directory
$Files = Get-ChildItem -Path $SourceDirectory -File -Recurse
foreach($Dir in $Directories) {
    foreach($File in $Files) {
        if($File.Name -like "*$($Dir.Name)*") {
            if(Test-Path "$($Dir.FullName)\$($File.Name)") {
                $Items = Get-ChildItem -Path "$($Dir.FullName)" -File
                if($Items) {
                    $NewFileName = "$($File.Name.Split('.')[0])$($Items.Count)$($File.Extension)"
                    Move-Item -Path "$($File.FullName)" -Destination...
Does not work on subfolders, but this one liner should do the trick.

for /D %G in ("*") DO move %~nxG* %~nxG
 


Powershell version. Supports sub directories on the source and can handle duplicate names on destination.

Code:
$SourceDirectory = ".\SOURCE"
$DestinationDirectory = ".\DEST"

$Directories = Get-ChildItem -Path $DestinationDirectory -Directory
$Files = Get-ChildItem -Path $SourceDirectory -File -Recurse
foreach($Dir in $Directories) {
    foreach($File in $Files) {
        if($File.Name -like "*$($Dir.Name)*") {
            if(Test-Path "$($Dir.FullName)\$($File.Name)") {
                $Items = Get-ChildItem -Path "$($Dir.FullName)" -File
                if($Items) {
                    $NewFileName = "$($File.Name.Split('.')[0])$($Items.Count)$($File.Extension)"
                    Move-Item -Path "$($File.FullName)" -Destination "$($Dir.FullName)\$NewFileName"
                }
            } else {
                Move-Item -Path "$($File.FullName)" -Destination "$($Dir.FullName)"
            }       
        }
    }
}
 


Solution
Back
Top