Page 1 of 1

Powershell | Find all mp4 avi mkv flv videos in a folder and make subfolders for them

Posted: Mon Dec 16, 2024 8:13 pm
by darknkreepy3#
Here's the code for poweshell win10 win11 as admin and allowing scripts like this to run via cmd as administrator with a line of code

Subfolders fore each video file found in a main folder and then move that file into the folder with the same name without the extension in it

Code: Select all

# movie_folders.ps1 v1.0 by kristoffe.brodeur. ©2024 All rights reserved.
# 12/16/2024

# Get the folder where the script is located
$folderPathCurrent = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Write-Host "Script is running in folder: $folderPathCurrent"

# Get all .avi, .flv, .mp4, or .mkv files in the current directory (and subdirectories)
$files = Get-ChildItem -Path "$folderPathCurrent\*" -Recurse -Include *.avi, *.flv, *.mp4, *.mkv -File -Force
Write-Host "Found $($files.Count) video files."

foreach ($file in $files) {
    Write-Host "Processing file: $($file.FullName)"
    # Get the file name without the extension
    $folderName = [System.IO.Path]::GetFileNameWithoutExtension($file.FullName)
    Write-Host "Target folder name: $folderName"

    # Create the target folder path
    $targetFolder = Join-Path -Path $folderPathCurrent -ChildPath $folderName
    Write-Host "Target folder path: $targetFolder"

    # Check if the folder already exists, if not, create it
    if (-not (Test-Path -Path $targetFolder)) {
        Write-Host "Creating folder: $targetFolder"
        New-Item -ItemType Directory -Path $targetFolder | Out-Null
    } else {
        Write-Host "Folder already exists: $targetFolder"
    }

    # Move the file to the new folder
    Write-Host "Moving file to folder: $targetFolder"
    try {
        Move-Item -Path $file.FullName -Destination $targetFolder -ErrorAction Stop
        Write-Host "Successfully moved: $($file.FullName)"
    } catch {
        Write-Host "Error moving file: $($file.FullName). $_"
    }
}

Write-Host "Operation completed successfully."
Allowing Powershell scripts to run

For Windows 11, Windows 10, Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator:

x86 (32 bit)
Open C:\Windows\SysWOW64\cmd.exe
Run the command

Code: Select all

powershell Set-ExecutionPolicy RemoteSigned
x64 (64 bit)
Open C:\Windows\system32\cmd.exe
Run the command

Code: Select all

powershell Set-ExecutionPolicy RemoteSigned
You can check mode using

In CMD: echo %PROCESSOR_ARCHITECTURE%
In Powershell: [Environment]::Is64BitProcess

https://stackoverflow.com/questions/403 ... his-system
by Ralph Willgoss