Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param (
- [Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
- [string[]]$InputFiles
- )
- # Get full paths of dropped files
- $files = $InputFiles | Where-Object { Test-Path $_ -and -not (Test-Path $_ -PathType Container) }
- if ($files.Count -eq 0) {
- Write-Host "No files provided."
- Read-Host -Prompt "Press Enter to exit"
- exit
- }
- # Extract just the file names
- $fileNames = $files | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension($_) }
- # Find common prefix
- function Get-CommonPrefix {
- param ([string[]]$names)
- if ($names.Count -eq 0) { return "" }
- $prefix = $names[0]
- foreach ($name in $names) {
- $i = 0
- while ($i -lt $prefix.Length -and $i -lt $name.Length -and $prefix[$i] -eq $name[$i]) {
- $i++
- }
- $prefix = $prefix.Substring(0, $i)
- if ($prefix -eq "") { break }
- }
- return $prefix.Trim("_- ")
- }
- $commonPrefix = Get-CommonPrefix -names $fileNames
- if ([string]::IsNullOrWhiteSpace($commonPrefix)) {
- Write-Host "No common prefix found."
- Read-Host -Prompt "Press Enter to exit"
- exit
- }
- # Use folder of the first file as base
- $baseFolder = Split-Path -Path $files[0] -Parent
- $targetFolder = Join-Path $baseFolder $commonPrefix
- # Create target folder if needed
- if (!(Test-Path $targetFolder)) {
- New-Item -ItemType Directory -Path $targetFolder | Out-Null
- }
- # Move files
- foreach ($file in $files) {
- Move-Item -Path $file -Destination $targetFolder
- }
- Write-Host "Moved $($files.Count) files into '$targetFolder'"
- Read-Host -Prompt "Press Enter to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement