Charkel
Charkel

Reputation: 338

Move all video files to parent directory

All my movies has a separate folder with a single video file in it. I want all files in a single folder. Accept for the ones with multiple parts (eg. tv series)

I have this powershell script that when $commit=false verboses all the filenames nicely in the list but does not execute the move and delete. I cannot get it to work.

  1. Move videofile from subDir to parentDir
  2. Delete subDir
$commit = $true

$extensions = '.mp4','.avi','.mpg','.mpeg','.mkv','.3gp','.wmv'

$path = 'L:\My Drive\Film\Test'

$folders = gci $path -Directory

foreach($fo in $folders) {
    $file = @($fo | gci -File | ? { $_.Extension -in $extensions })

    if($file.Count -eq 1)
    {
        if($commit)
        {                               
            [void](mi -Path $file.FullName "$($fo.Parent.FullName)\$($file.Name)")

            [void](ri -Path $fo.FullName -Recurse)
        }
        else
        {
            Write-Host "$(Get-Date) | Moving '$($file.FullName)' to '$($fo.Parent.FullName)' and deleting folder '$($fo.FullName)'"
        }
    } 
}

Upvotes: 1

Views: 238

Answers (1)

OwlsSleeping
OwlsSleeping

Reputation: 1570

Copying the documentation for the next reader since the issue was in path vs literalpath.

https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/supporting-wildcard-characters-in-cmdlet-parameters?view=powershell-7.1

Many Windows PowerShell cmdlets support wildcard characters for their parameter values. For example, almost every cmdlet that has a Name or Path parameter supports wildcard characters for these parameters. (Although most cmdlets that have a Path parameter also have a LiteralPath parameter that does not support wildcard characters.) The following command shows how a wildcard character is used to return all the cmdlets in the current session whose name contains the Get verb.

...

Supported Wildcard Characters * ? [ ]

Upvotes: 1

Related Questions