Kreg
Kreg

Reputation: 43

Move files into alphabetically named folders

Just wondering if it's possible to have a script move film files to a specific folder based on the alphabet?

Eg Scream 4 would get moved to e:\movies\s\ Avatar would get moved to e:\movies\a\

I start a script that looks like this : But the result is not good!The script try to create a directory with files name...

$a = new-object -comobject wscript.shell
$b = Get-Location

    foreach($file in (dir $b -file -recurse)) { 
        New-Item -Path $b -Name (Split-Path $file.fullname -Leaf).Replace($file.extension,"") -ItemType Directory -Confirm
        Move-Item -Path $file.fullname -Destination "$b\$((Split-Path $file.fullname -Leaf).Replace($file.Extension,''))" -Confirm
        }

An idea ? Many thanks! Kreg

Upvotes: 3

Views: 2047

Answers (1)

Shay Levy
Shay Levy

Reputation: 126762

Destination folders must exist before you run the command:

dir $b -file -recurse | Move-Item -Destination {"e:\movies\$($_.Name[0])"} 

This will create the folders at run time:

dir $b -File -Recurse | foreach{
    $folder = Join-Path e:\movies $_.Name[0]
    md $folder -force | Out-Null
    $_ | Move-Item -Destination $folder
}  

Upvotes: 6

Related Questions