So I am having this situation... I have many files in my Folder which will look like this
Iron.Man.2008.1440p.UHD.US.BluRay.x265.HDR.DD5.1-Pahe.in
Iron Man 2008.7
Since you also tagged this PowerShell, here's a solution:
$sourceFolder = 'D:\Test' # where the files are now
$destination = 'D:\Movies' # where the subfolders storing the files should be created
$regex = '^(\D+[.\s]\d{4})\..*' # see explanation below
(Get-ChildItem -Path 'D:\Test' -File | Where-Object { $_.BaseName -match $regex }) |
ForEach-Object {
$subFolder = ([regex]$regex).Match($_.BaseName).Groups[1].Value -replace '\s', '.'
$targetDir = Join-Path -Path $destination -ChildPath $subFolder
# create the subfolder if it does not yet exist
if (!(Test-Path -Path $targetDir -PathType Container)) {
$null = New-Item -Path $targetDir -ItemType Directory
}
$_ | Copy-Item -Destination $targetDir
}
Regex details:
^ # Assert position at the beginning of a line (at beginning of the string or after a line break character)
( # Match the regular expression below and capture its match into backreference number 1
\D # Match a single character that is not a digit 0..9
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
[.\s] # Match a single character present in the list below
# The character “.”
# A whitespace character (spaces, tabs, line breaks, etc.)
\d # Match a single digit 0..9
{4} # Exactly 4 times
)
\. # Match the character “.” literally
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)