通过根据文件夹名称中存在的关键字对文件夹进行分组来将其移动到其他

通过根据文件夹名称中存在的关键字对文件夹进行分组来将其移动到其他

本文介绍了如何通过根据文件夹名称中存在的关键字对文件夹进行分组来将其移动到其他文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件移到按这样的文件中的键字符串排序的相对文件夹中

To move files into their relative folders ordered by key string in file like this

示例脚本

我使用此脚本

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "SPLITCHAR=-"  & rem // (a single character to split the file names)
set "SEARCHSTR=_"  & rem // (a certain string to be replaced by another)
set "REPLACSTR= "  & rem // (a string to replace all found search strings)
set "OVERWRITE="   & rem // (set to non-empty value to force overwriting)

rem // Get file location and pattern from command line arguments:
set "LOCATION=%~1" & rem // (directory to move the processed files into)
set "PATTERNS=%~2" & rem // (file pattern; match all files if empty)

rem /* Prepare overwrite flag (if defined, set to character forbidden
rem    in file names; this affects later check for file existence): */
if defined OVERWRITE set "OVERWRITE=|"
rem // Continue only if target location is given:
if defined LOCATION (
    rem // Create target location (surpress error if it already exists):
    2> nul md "%LOCATION%"
    rem /* Loop through all files matching the given pattern
    rem    in the current working directory: */
    for /F "eol=| delims=" %%F in ('dir /B "%PATTERNS%"') do (
        rem // Process each file in a sub-routine:
        call :PROCESS "%%F" "%LOCATION%" "%SPLITCHAR%" "%SEARCHSTR%" "%REPLACSTR%"
    )
)

endlocal
exit /B


:PROCESS
rem // Retrieve first argument of sub-routine:
set "FILE=%~1"
rem // Split name at (first) split character and get portion in front:
for /F "delims=%~3" %%E in ("%~1") do (
    rem // Append a split character to partial name:
    set "FOLDER=%%E%~3"
)
setlocal EnableDelayedExpansion
rem // Right-trim partial name:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4%~3=!"
set "FOLDER=!FOLDER:%~3=!"
rem /* Check whether partial name is not empty
rem    (could happen if name began with split character): */
if defined FOLDER (
    rem // Replace every search string with another:
    if not "%~4"=="" set "FOLDER=!FOLDER:%~4=%~5!"
    rem // Create sub-directory (surpress error if it already exists):
    2> nul md "%~2\!FOLDER!"
    rem /* Check if target file already exists; if overwrite flag is
    rem    set (to an invalid character), the target cannot exist: */
    if not exist "%~2\!FOLDER!\!FILE!%OVERWRITE%" (
        rem // Move file finally (surpress `1 file(s) moved.` message):
        1> nul move /Y "!FILE!" "%~2\!FOLDER!"
    )
)
endlocal
exit /B

但是我希望对文件夹而不是文件做类似的事情.例如,我有这些文件夹列表

But i wish to do a similar thing for folders and not for files.For example, I have these folders list

Absolute Moebius - Volume 2 - The Long Tomorrow
Absolute Moebius - Volume 3
Agenzia X - Volume 1 - La Recluta
Agenzia X - Volume 2 - Black Point
Agenzia X - Volume 3 - Soli
Akira - Volume 10
Akira - Volume 20
Akira - Volume 23
Alan Ford - Volume 11 - Il Numero Uno
Alan Ford - Volume 12 - La Triste Storia Di Un Giovane Ricco
Alan Ford - Volume 13 - Golf

我希望将它们移动到这样的文件夹结构中

and i wish to move them into a folder structure like that

Absolute Moebius [folder]
|
|---> Absolute Moebius - Volume 2 - The Long Tomorrow
|---> Absolute Moebius - Volume 3
|
|
Agenzia X  [folder]
|
|---> Agenzia X - Volume 1 - La Recluta
|---> Agenzia X - Volume 2 - Black Point
|---> Agenzia X - Volume 3 - Soli
|
Akira [folder]
|
|---> Akira - Volume 10
|---> Akira - Volume 20
|---> Akira - Volume 23
|
.
.
:

通常,许多文件夹名称包含 Volume volume 或重复的其他一些关键字,这些关键字可以用作判别式

Usually many folder names contains Volume , volume , or some other key word that is repeated and that could be used as a discriminant

问题询问如何移动文件夹而不是文件.例如,上面的脚本在将文件分组到文件夹中时起作用:您可以看到示例 HERE .但是我希望移动 文件夹 ,而不是移动文件,以便使用诸如条件之类的关键字(例如 Volume 词)将文件分组到其他文件夹中

question ask how you can move folders and not files. For example, script above works when you moving files grouping them inside folders: you can see an example HERE. But I wish to move folders and not files for grouping them inside other folders using, for example, a keyword like criteria, for example Volume word

我还添加了Powershell标记,因为对我来说也是一个Powershell脚本

I add also Powershell tag because for me is good also a powershell script

推荐答案

我看到您已选择答案.这是另一种方式.

I see that you have selected an answer. Here is another way.

# Setup test data
$Dirs = @(
    'Absolute Moebius - Volume 2 - The Long Tomorrow'
    ,'Absolute Moebius - Volume 3'
    ,'Agenzia X - Volume 1 - La Recluta'
    ,'Agenzia X - Volume 2 - Black Point'
    ,'Agenzia X - Volume 3 - Soli'
    ,'Akira - Volume 10'
    ,'Akira - Volume 20'
    ,'Akira - Volume 23'
    ,'Alan Ford - Volume 11 - Il Numero Uno'
    ,'Alan Ford - Volume 12 - La Triste Storia Di Un Giovane Ricco'
    ,'Alan Ford - Volume 13 - Golf'
)
foreach ($Dir in $Dirs) {
    if (-not (Test-Path -Path $Dir)) { New-Item -ItemType Directory -Path $Dir | Out-Null }
}

# Code to move files starts here vvvvvvvvvvvvvvvvvvvv
(Get-ChildItem -Directory -Filter '* - Volume*').Name |
    ForEach-Object {
        $NewDirName = $_.Split(' - ')[0]
        if (-not (Test-Path -Path $NewDirName)) { New-Item -ItemType Directory -Path $NewDirName | Out-Null }
        Move-Item -Path $_ -Destination $NewDirName
    }
# Code to move files ends here ^^^^^^^^^^^^^^^^^^^

这篇关于如何通过根据文件夹名称中存在的关键字对文件夹进行分组来将其移动到其他文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:34