Windows 根据多部分存档文件名中的关键字,将类似文件收集到单独的文件夹中

Windows 根据多部分存档文件名中的关键字,将类似文件收集到单独的文件夹中,windows,powershell,batch-file,batch-rename,Windows,Powershell,Batch File,Batch Rename,我有一个包含许多rar或zip文件的文件夹。我想将类似的文件(如果存在,则基于文件名中的部分词)放入自己的文件夹。默认情况下,父文件夹中没有任何文件夹。也许将来会将文件的另一部分添加到父目录中,所以这次它应该将文件移动到自己的文件夹中,而不是创建新文件夹 例如,假设文件为: Visual_Studio_2015.part1.rar Visual_Studio_2015.part2.rar Visual_Studio_2015.part3.rar SQL-Server-Enterprise-20

我有一个包含许多rar或zip文件的文件夹。我想将类似的文件(如果存在,则基于文件名中的部分词)放入自己的文件夹。默认情况下,父文件夹中没有任何文件夹。也许将来会将文件的另一部分添加到父目录中,所以这次它应该将文件移动到自己的文件夹中,而不是创建新文件夹

例如,假设文件为:

Visual_Studio_2015.part1.rar
Visual_Studio_2015.part2.rar
Visual_Studio_2015.part3.rar

SQL-Server-Enterprise-2016-SP1.part1.rar
SQL-Server-Enterprise-2016-SP1.part2.rar

VSCodeSetup x64 1.29.1.rar

Microsoft.Visual.Studio.Ultimate.2012.update.3.part1.rar
Microsoft.Visual.Studio.Ultimate.2012.update.3.part12.rar
移动后,变为如下所示:

Parent Directory
├───Visual_Studio_2015
│   ├───Visual_Studio_2015.part1.rar
│   ├───Visual_Studio_2015.part2.rar
│   ├───Visual_Studio_2015.part3.rar
├───VSCodeSetup x64 1.29.1
│   ├───VSCodeSetup x64 1.29.1.rar
├───SQL-Server-Enterprise-2016-SP1
│   ├───SQL-Server-Enterprise-2016-SP1.part1.rar
│   ├───SQL-Server-Enterprise-2016-SP1.part2.rar
├───Microsoft.Visual.Studio.Ultimate.2012.update.3
│   ├───Microsoft.Visual.Studio.Ultimate.2012.update.3.part1.rar
│   ├───Microsoft.Visual.Studio.Ultimate.2012.update.3.part2.rar
Get-ChildItem -File | 
  Group-Object { $_.Name -replace '.part.*' } | 
  ForEach-Object {
    $dir = New-Item -Type Directory -Name $_.Name
    $_.Group | Move-Item -Destination $dir
  }
我不能用任何软件或编译的编程语言来解决这个问题。对不起,英语不好

更新: 在powershell中,类似这样的东西:

Parent Directory
├───Visual_Studio_2015
│   ├───Visual_Studio_2015.part1.rar
│   ├───Visual_Studio_2015.part2.rar
│   ├───Visual_Studio_2015.part3.rar
├───VSCodeSetup x64 1.29.1
│   ├───VSCodeSetup x64 1.29.1.rar
├───SQL-Server-Enterprise-2016-SP1
│   ├───SQL-Server-Enterprise-2016-SP1.part1.rar
│   ├───SQL-Server-Enterprise-2016-SP1.part2.rar
├───Microsoft.Visual.Studio.Ultimate.2012.update.3
│   ├───Microsoft.Visual.Studio.Ultimate.2012.update.3.part1.rar
│   ├───Microsoft.Visual.Studio.Ultimate.2012.update.3.part2.rar
Get-ChildItem -File | 
  Group-Object { $_.Name -replace '.part.*' } | 
  ForEach-Object {
    $dir = New-Item -Type Directory -Name $_.Name
    $_.Group | Move-Item -Destination $dir
  }

如果文件名中有部分,但是没有它就不能工作,那么我必须指出,如果文件名是多部分归档文件,那么所有文件名都以.partX结尾(X是一个数字)。

如果所有文件都在一个根文件夹中,并且具有您指定的命名约定,然后,有一种方法可以将它们移动到相应的子文件夹中:

Get-Childitem -path "C:\Test" -File |
    ForEach-Object {
        if($_.Name -match "^(?<folder>.*)\.part\d+|(?<folder>.*)\.rar$") {
            New-Item -Path "$($_.Directory)\$($matches.Folder)" -ItemType Directory -Force | Out-Null

            Move-Item -Path $_.FullName -Destination "$($_.Directory)\$($matches.Folder)\$($_.Name)" -Force
        }
    }
getchilditem-路径“C:\Test”-文件|
ForEach对象{
如果($\.Name-match“^(?..*\.part\d+|(?.*)\.rar$”){
新项目-路径“$($.Directory)\$($matches.Folder)”-ItemType目录-Force | Out Null
移动项目-路径$\.FullName-目标“$($\.Directory)\$($matches.Folder)\$($\.Name)”-强制
}
}

根据需要更改
Get Childitem
中的路径。此外,如果希望将
新项目
移动项目
的路径放置在其他位置,而不是作为根目录的子文件夹,则可以修改它们的路径。

如果所有文件都位于一个根文件夹中,并且具有指定的命名约定,则有一种方法可以将它们移动到相应的子文件夹中:

Get-Childitem -path "C:\Test" -File |
    ForEach-Object {
        if($_.Name -match "^(?<folder>.*)\.part\d+|(?<folder>.*)\.rar$") {
            New-Item -Path "$($_.Directory)\$($matches.Folder)" -ItemType Directory -Force | Out-Null

            Move-Item -Path $_.FullName -Destination "$($_.Directory)\$($matches.Folder)\$($_.Name)" -Force
        }
    }
getchilditem-路径“C:\Test”-文件|
ForEach对象{
如果($\.Name-match“^(?..*\.part\d+|(?.*)\.rar$”){
新项目-路径“$($.Directory)\$($matches.Folder)”-ItemType目录-Force | Out Null
移动项目-路径$\.FullName-目标“$($\.Directory)\$($matches.Folder)\$($\.Name)”-强制
}
}

根据需要更改
Get Childitem
中的路径。此外,如果希望将
新项目
移动项目
的路径放置在其他位置,而不是根目录的子文件夹,则可以修改它们的路径。

另一种方法是:

$parentFolder = '<THE PARENTFOLDER THAT HOLDS ALL .RAR AND .ZIP FILES>'

# Get all files inside the parent folder with extension '.rar' or '.zip'
# Because '-Filter' only accepts a single string, we need to use a 'Where-Object' clause.
# Another way would be to use the '-Include' parameter on Get-Childitem, but for that to work
# you must either also use '-Recurse' or append '\*' to the $parentfolder like this:
#  Get-ChildItem -Path "$parentFolder\*" -File -Include *.rar, *.zip
Get-ChildItem -Path $parentFolder -File | Where-Object { $_.Extension -match '\.(rar|zip)$' } | ForEach-Object {
    # create the name of the subfolder by removing the '.partX' from the basename if that exists
    $subFolder = Join-Path -Path $parentFolder -ChildPath ($_.BaseName -replace '\.part\d+', '')
    # create this subfolder if it does not already exist
    if (!(Test-Path -Path $subFolder -PathType Container)) {
        New-Item -Path $subFolder -ItemType Directory -Force | Out-Null
    }
    # move the file to the subfolder
    $_ | Move-Item -Destination $subFolder
}
$parentFolder=''
#获取父文件夹中扩展名为“.rar”或“.zip”的所有文件
#因为“-Filter”只接受单个字符串,所以我们需要使用“Where Object”子句。
#另一种方法是在Get Childitem上使用“-Include”参数,但要使其正常工作
#还必须使用“-Recurse”或将“\*”附加到$parentfolder,如下所示:
#获取ChildItem-路径“$parentFolder\*”-文件-包括*.rar、*.zip
获取ChildItem-Path$parentFolder-File | Where对象{$\扩展名-match'\(rar | zip)$'}ForEach对象{
#通过从基本名称中删除“.partX”(如果存在)来创建子文件夹的名称
$subFolder=Join Path-Path$parentFolder-ChildPath($\.BaseName-replace'\.part\d+','')
#如果此子文件夹不存在,请创建它
if(!(测试路径-路径$子文件夹-路径类型容器)){
新项目-路径$subFolder-项目类型目录-强制|输出空值
}
#将文件移动到子文件夹
$124;移动项目-目标$子文件夹
}

另一种方法是:

$parentFolder = '<THE PARENTFOLDER THAT HOLDS ALL .RAR AND .ZIP FILES>'

# Get all files inside the parent folder with extension '.rar' or '.zip'
# Because '-Filter' only accepts a single string, we need to use a 'Where-Object' clause.
# Another way would be to use the '-Include' parameter on Get-Childitem, but for that to work
# you must either also use '-Recurse' or append '\*' to the $parentfolder like this:
#  Get-ChildItem -Path "$parentFolder\*" -File -Include *.rar, *.zip
Get-ChildItem -Path $parentFolder -File | Where-Object { $_.Extension -match '\.(rar|zip)$' } | ForEach-Object {
    # create the name of the subfolder by removing the '.partX' from the basename if that exists
    $subFolder = Join-Path -Path $parentFolder -ChildPath ($_.BaseName -replace '\.part\d+', '')
    # create this subfolder if it does not already exist
    if (!(Test-Path -Path $subFolder -PathType Container)) {
        New-Item -Path $subFolder -ItemType Directory -Force | Out-Null
    }
    # move the file to the subfolder
    $_ | Move-Item -Destination $subFolder
}
$parentFolder=''
#获取父文件夹中扩展名为“.rar”或“.zip”的所有文件
#因为“-Filter”只接受单个字符串,所以我们需要使用“Where Object”子句。
#另一种方法是在Get Childitem上使用“-Include”参数,但要使其正常工作
#还必须使用“-Recurse”或将“\*”附加到$parentfolder,如下所示:
#获取ChildItem-路径“$parentFolder\*”-文件-包括*.rar、*.zip
获取ChildItem-Path$parentFolder-File | Where对象{$\扩展名-match'\(rar | zip)$'}ForEach对象{
#通过从基本名称中删除“.partX”(如果存在)来创建子文件夹的名称
$subFolder=Join Path-Path$parentFolder-ChildPath($\.BaseName-replace'\.part\d+','')
#如果此子文件夹不存在,请创建它
if(!(测试路径-路径$子文件夹-路径类型容器)){
新项目-路径$subFolder-项目类型目录-强制|输出空值
}
#将文件移动到子文件夹
$124;移动项目-目标$子文件夹
}

脚本最好的地方是防止覆盖,谢谢脚本最好的地方是防止覆盖,谢谢