I have thousands of files with the following structure:

Sample1 (43643) [3235]
Sample1 (32352) [6432]
Sample1 (35346) [4364]

I am trying to batch create folders based on the file name excluding the brackets and parenthesis and move all similarly named files into that folder like this:

Sample1\Sample1 (43643) [3235]
Sample1\Sample1 (32352) [6432]
Sample1\Sample1 (35346) [4364]

Any Ideas how to do this? Any help will be appreciated.


@beatcracker I was able to get it to work with the example filenames but here are the actual files names I am using (which did not work):

1st Division Manager (1992)(Codemasters)[cr NMS].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a10].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a11].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a12].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a2].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a3].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a4].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a5].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a6].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a7].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a8].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a9].adf
1st Division Manager (1992)(Codemasters)[cr NMS][a].adf
1st Division Manager (1992)(Codemasters)[cr NMS][h KTS].adf
3D Construction Kit II r2.01 (1992)(Domark)(Disk 1 of 2)[h Ministry][construction kit].adf
3D Construction Kit II r2.01 (1992)(Domark)(Disk 2 of 2)[construction kit].adf
3D Construction Kit II r2.03 (1992)(Domark)(FR)(Disk 1 of 2)[construction kit].adf
3D Construction Kit II r2.03 (1992)(Domark)(FR)(Disk 2 of 2)[construction kit].adf
  • I would like to add that I am using Windows 10. – Jonathan Sep 2 '15 at 0:20

Here is the PowerShell version. Works for the single folder, uses regex to match file names. Just edit $OrigFolder/$NewFolder accordingly:

# Folder where files are stored
$OrigFolder = 'C:\MyFolder'

# Folder where sorted files will be stored.
# It will be created if not exists.
$NewFolder = 'C:\MyFolder_Sorted'

# Folder, where files with empty part of the name will be stored
# Example: file "(1992)(Domark)(FR)(Disk 2 of 2)[construction kit].adf"
# will be moved to ".\Sorted\Unknown" folder
$UnknownFolder = 'Unknown'

# Regex to match filenames
$Regex = '^\s*(.*?)\s*\('

# Loop through files in directory, skip folders
Get-ChildItem -LiteralPath $OrigFolder | Where-Object {!$_.PsIsContainer} |
    ForEach-Object {
        if($_.BaseName -match $Regex){

            # If first part of the file name is empty,
            # move it to the 'Unknown' folder
                $ChildPath = $UnknownFolder
            } else {
                $ChildPath =  $Matches[1]

            # Generate new folder name
            $FolderName = Join-Path -Path $NewFolder -ChildPath $ChildPath

            # Create folder if not exists
            if(!(Test-Path -LiteralPath $FolderName -PathType Container)){
                $null = New-Item -Path $FolderName -ItemType Directory

            # Basic logging
            Write-Host "$($_.FullName) -> $FolderName"

            # Move file
            Move-Item -LiteralPath $_.FullName -Destination $FolderName

Save it as MoveFiles.ps1 and run in PowerShell console like this: .\MoveFiles.ps1

  • Hello, thanks for the quick response. I followed the instructions carefully, editing the folder paths on lines 1 & 2, then ran the script as .\MoveFiles.ps1 in PowerShell but nothing happened... – Jonathan Sep 2 '15 at 6:14
  • @Jonathan Maybe regex is too strict (script works for me with your example filenames). It will match only the filenames as in your example. Could you post actual filename? – beatcracker Sep 2 '15 at 9:36
  • I was able to get it to work with the example filenames but here are the actual files names I am working with (please see above) – Jonathan Sep 2 '15 at 13:59
  • @Jonathan So, basically, you want folder name to be anything until first (? Example: 1st Division Manager, 3D Construction Kit II r2.03, 3D Construction Kit II r2.01? Then see my updated answer. – beatcracker Sep 2 '15 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.