使用批处理文件处理时修复移动和合并文件夹

问题描述 投票:0回答:2

我有此代码

@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

我以这种方式使用命令提示符来创建文件夹并将其中的文件从Folder1移至Folder2

cd /D "C:\Users\Administrator\Downloads\"

"C:\Users\Administrator\Downloads\test1\build-folder-hierarchy.bat" "C:\Users\Administrator\Downloads\test2" "*.mkv"

什么问题?https://i.imgur.com/5ZY6RbB.png

但是我想从移动的文件中获取文件夹合并,而不是从文件中生成相同数量的文件夹

The.Race.Corsa.Mortale.2019.S1E02.Episodio2.HDTV.AAC.iTA.X264-ARSENAL.mkv
The.Race.Corsa.Mortale.2019.S1E01.Episodio1.HDTV.AAC.iTA.X264-ARSENAL.mkv
The.Feed.1x05.Episodio.5.ITA.DLMux.x264-UBi.mkv
The.Feed.1x04.Episodio.4.ITA.DLMux.x264-UBi.mkv
The.Feed.1x03.Episodio.3.ITA.DLMux.x264-UBi.mkv
The.Feed.1x02.Episodio.2.ITA.DLMux.x264-UBi.mkv
The.Feed.1x01.Episodio.1.ITA.DLMux.x264-UBi.mkv
Swamp.Thing.1x10.La.Resa.Dei.Conti.ITA.DLMux.x264-UBi.mkv
Volevo.Fare.La.Rockstar.1x11.Confusione.ITA.WEBRip.x264-UBi.mkv
Volevo.Fare.La.Rockstar.1x07.Tabu.ITA.WEBRip.x264-UBi.mkv
Volevo.Fare.La.Rockstar.1x01.Buon.Compleanno.Olly.ITA.WEBRip.x264-UBi.mkv
Volevo Fare La Rockstar 1x12 La Tribu Ita Webrip x264-Ubi.mkv
Virgin.River.1x10.Finali.inattesi.720p.iTA.AAC.DLRip.x265.-.T7.mkv
Virgin.River.1x07.A.dire.il.vero.720p.iTA.AAC.DLRip.x265.-.T7.mkv
Virgin.River.1x04.Un.Cuore.Ferito.iTA.AC3.WEBMux.x264-ADE.CreW.mkv
Virgin.River.1x01.La.Vita.Continua.iTA.AC3.WEBMux.x264-ADE.CreW.mkv
Tre.Giorni.Di.Natale.1x03.Episodio.3.iTA.AC3.WEBMux.x264-ADE.CreW.mkv
Tre.Giorni.Di.Natale.1x01.Episodio.1.iTA.AC3.WEBMux.x264-ADE.CreW.mkv

但是我想以这种方式获取文件夹并移动文件

├─The Race Corsa Mortale [folder]
│ ├─The.Feed.1x05.Episodio.5.ITA.DLMux.x264-UBi [file]
│ ├─The.Feed.1x04.Episodio.4.ITA.DLMux.x264-UBi [file]
  └─ ....
├─Virgin River [folder]
│ └─Virgin.River.1x07.A.dire.il.vero.720p.iTA.AAC.DLRip.x265 [file]
:

enter image description here

我也尝试使用此批处理脚本,但是它不起作用:我通过资源管理器单击,但是就像已停用(我使用Windows Server 2012)

@echo off
setlocal EnableDelayedExpansion

rem Change current directory to the one where this .bat file is located
cd "%~P0"

set "digits=0123456789"

rem Process all *.mkv files
for %%f in (*.mkv) do (

   rem Get the folder name of this file
   call :getFolder "%%f"

   rem If this file have a properly formatted name: "headS##Etail"
   if defined folder (
      rem Move the file to such folder
      if not exist "!folder!" md "!folder!"
      move "%%f" "!folder!"
   )

)
goto :EOF


:getFolder file

set "folder="
set "file=%~1"
set "head="
set "tail=%file%"

:next
   for /F "delims=%digits%" %%a in ("%tail%") do set "head=%head%%%a"
   set "tail=!file:*%head%=!"
   if not defined tail exit /B
   if /I "%head:~-1%" equ "S" goto found
   :digit
      if "!digits:%tail:~0,1%=!" equ "%digits%" goto noDigit
      set "head=%head%%tail:~0,1%"
      set "tail=%tail:~1%"
   goto digit
   :noDigit
goto next

:found
for /F "delims=Ee" %%a in ("%tail%") do set "folder=%head%%%a"
exit /B

我也接受Powershell解决方案

编辑:我需要的文件名的一部分是S#E###x## .#x##.#x#.#x##和类似名称的>]

我有此代码@echo off setlocal EnableExtensions DisableDelayedExpansion rem //在这里定义常量:set“ SPLITCHAR =-”&rem //(用一个字符分割文件名)set“ SEARCHSTR = ...

powershell batch-file
2个回答
0
投票

假设我们可以在文件名中分割数字的文件名,这应该很有用。

我稍作停顿,检查输出,并确保在单击Enter之前它为您提供了正确的信息。


0
投票

我可能会使用以下脚本(请查阅所有解释性的rem备注:]

© www.soinside.com 2019 - 2024. All rights reserved.