Batch Cria pastas baseadas em nomes de arquivos, excluindo parênteses e colchetes

0

Eu tenho milhares de arquivos com a seguinte estrutura:

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

Estou tentando criar pastas em lote com base no nome do arquivo, excluindo os colchetes e parênteses, e movo todos os arquivos com nomes semelhantes para essa pasta da seguinte forma:

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

Alguma idéia de como fazer isso? Qualquer ajuda será apreciada.

Atualização:

@beatcracker Consegui fazê-lo funcionar com os nomes dos arquivos de exemplo, mas aqui estão os nomes dos arquivos que estou usando (o que não funcionou):

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
    
por Jonathan 01.09.2015 / 22:16

1 resposta

0

Aqui está a versão do PowerShell. Funciona para a pasta única, usa o regex para corresponder aos nomes dos arquivos. Apenas edite $OrigFolder / $NewFolder de acordo:

# 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
        }
}

Salvar como MoveFiles.ps1 e executar no console do PowerShell da seguinte forma: .\MoveFiles.ps1

    
por 02.09.2015 / 05:07