如何创建批处理文件以将文件从子文件夹上移到一级重命名重复项? [英] How to create batch file that move files from sub-folder up one level renaming duplicates?

查看:435
本文介绍了如何创建批处理文件以将文件从子文件夹上移到一级重命名重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个批处理文件,可以将其放入任何 SHOW_NAME 目录中,它将把文件从子文件夹移动到其 SEASON 父目录中.例如:

I already have a batch file that I can drop in any SHOW_NAME directory and it will move files from a sub-folder to its SEASON parent directory. For example:

F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP1\title_episode1.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP2\title_episode2.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP3\title_episode3.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\title_episode3.mkv

将所有文件移动到父文件夹( SEASON1 )时," title_episode3.mkv "是重复的,并覆盖原始文件.如何通过添加数字" title_episode3(1).mkv "来自动重命名?

When it moves all files to the parent folder (SEASON1) the "title_episode3.mkv" is a duplicate and overwrites the original. How can I automatically rename by appending a number "title_episode3 (1).mkv"?

这是我在批处理文件中使用的代码:

Here is the code that I use in a batch file:

@echo off
for /d /r %%f in (*) do (
for /d %%g in ("%%f\*") do (
   for %%h in ("%%~g\*.mkv") do move "%%~h" "%%~f" >nul 2>&1
    )
)

谢谢!

推荐答案

此批处理批处理文件可用于此任务:

This commented batch file can be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Search for any file two directory levels below specified directory
rem and pass to subroutine MoveFile the name of the file with full path.

for /D %%A in ("F:\TV_SHOWS\SHOW_NAME\*") do (
    for /D %%B in ("%%A\*") do (
       for /F "delims=" %%I in ('dir "%%B\*" /A-D /B /S 2^>nul') do call :MoveFile "%%I"
    )
)

endlocal
goto :EOF

:MoveFile
set "FilePath=%~dp1"
set "FileNameOnly=%~n1"
set "FileNameFull=%~1"
set "FileName+Ext=%~nx1"
set "FileExtension=%~x1"

rem For files staring with a dot and not containing one more dot.
if "%FileNameOnly%" == "" set "FileNameOnly=%~x1" & set "FileExtension="

rem Get path to parent folder ending with a backslash.
for /F "delims=" %%J in ("%FilePath:~0,-1%") do set "FileParent=%%~dpJ"

rem Uncomment the line below to see the values of the six File* variables.
rem set File & echo/

rem Does a file with current file name not exist in parent folder?
if not exist "%FileParent%%FileName+Ext%" (
    rem Move the file to parent folder and if this was successful
    rem delete the folder of the moved file if being empty now.
    move "%FileNameFull%" "%FileParent%%FileName+Ext%" >nul
    if not errorlevel 1 rd "%FilePath%" 2>nul
    goto :EOF
)

set "FileNumber=1"
:NextFile
if exist "%FileParent%%FileNameOnly% (%FileNumber%)%FileExtension%" set /A "FileNumber+=1" & goto NextFile

move "%FileNameFull%" "%FileParent%%FileNameOnly% (%FileNumber%)%FileExtension%" >nul
if not errorlevel 1 rd "%FilePath%" 2>nul
goto :EOF

再次在同一目录上运行批处理文件,没有新的子目录,也没有新的文件不会更改任何内容.

Running the batch file a second time on same directory with no new subdirectory and no new file does not change anything.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?
  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅:

  • the Microsoft article about Using Command Redirection Operators;
  • Single line with multiple commands using Windows batch file;
  • Where does GOTO :EOF return to?

这篇关于如何创建批处理文件以将文件从子文件夹上移到一级重命名重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆