Advertisement
GazaIan

Untitled

Jun 13th, 2025
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param (
  2.     [Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
  3.     [string[]]$InputFiles
  4. )
  5.  
  6. # Get full paths of dropped files
  7. $files = $InputFiles | Where-Object { Test-Path $_ -and -not (Test-Path $_ -PathType Container) }
  8.  
  9. if ($files.Count -eq 0) {
  10.     Write-Host "No files provided."
  11.     Read-Host -Prompt "Press Enter to exit"
  12.     exit
  13. }
  14.  
  15. # Extract just the file names
  16. $fileNames = $files | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension($_) }
  17.  
  18. # Find common prefix
  19. function Get-CommonPrefix {
  20.     param ([string[]]$names)
  21.     if ($names.Count -eq 0) { return "" }
  22.     $prefix = $names[0]
  23.     foreach ($name in $names) {
  24.         $i = 0
  25.         while ($i -lt $prefix.Length -and $i -lt $name.Length -and $prefix[$i] -eq $name[$i]) {
  26.             $i++
  27.         }
  28.         $prefix = $prefix.Substring(0, $i)
  29.         if ($prefix -eq "") { break }
  30.     }
  31.     return $prefix.Trim("_- ")
  32. }
  33.  
  34. $commonPrefix = Get-CommonPrefix -names $fileNames
  35.  
  36. if ([string]::IsNullOrWhiteSpace($commonPrefix)) {
  37.     Write-Host "No common prefix found."
  38.     Read-Host -Prompt "Press Enter to exit"
  39.     exit
  40. }
  41.  
  42. # Use folder of the first file as base
  43. $baseFolder = Split-Path -Path $files[0] -Parent
  44. $targetFolder = Join-Path $baseFolder $commonPrefix
  45.  
  46. # Create target folder if needed
  47. if (!(Test-Path $targetFolder)) {
  48.     New-Item -ItemType Directory -Path $targetFolder | Out-Null
  49. }
  50.  
  51. # Move files
  52. foreach ($file in $files) {
  53.     Move-Item -Path $file -Destination $targetFolder
  54. }
  55.  
  56. Write-Host "Moved $($files.Count) files into '$targetFolder'"
  57. Read-Host -Prompt "Press Enter to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement