根据文件名(不包括括号和方括号)批量创建文件夹

根据文件名(不包括括号和方括号)批量创建文件夹

我有数千个具有以下结构的文件:

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

我正在尝试根据文件名(不包括括号和圆括号)批量创建文件夹,并将所有名称相似的文件移动到该文件夹​​中,如下所示:

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

有什么想法可以做到这一点吗?任何帮助都将不胜感激。

更新:

@beatcracker我可以让它与示例文件名一起工作,但是这是我正在使用的实际文件名(不起作用):

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

答案1

这是 PowerShell 版本。适用于单个文件夹,使用正则表达式匹配文件名。只需相应地编辑$OrigFolder/$NewFolder即可:

# 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
            if(!$Matches[1]){
                $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
        }
}

将其保存为MoveFiles.ps1并在 PowerShell 控制台中运行,如下所示:.\MoveFiles.ps1

相关内容