You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

用于将文件上移一级并重命名重复项的批处理文件制作需求

Solution for Moving .EXT Files Up and Handling Duplicates

Got it, here's a batch script tailored exactly to your needs—save it directly in your C:\Main Folder, run it, and it'll automatically move all .EXT files from subfolders under Folder2 and Folder3 up to their respective parent folders, with smart duplicate filename handling.

The Batch Script

@echo off
setlocal enabledelayedexpansion

:: Set base directories (uses the script's location as Main Folder)
set "mainDir=%~dp0"
set "folder2Target=%mainDir%Folder2"
set "folder3Target=%mainDir%Folder3"

:: Process all .EXT files under Folder2's subfolders
echo === Processing Folder2 ===
for /r "%folder2Target%" %%f in (*.EXT) do (
    :: Extract filename and extension
    set "fileBase=%%~nf"
    set "fileExt=%%~xf"
    set "targetFile=%folder2Target%\!fileBase!!fileExt!"
    set "copyCounter=1"

    :: Handle duplicate filenames
    if exist "!targetFile!" (
        :loopFolder2
        set "newFileName=!fileBase!(^!copyCounter^!)!fileExt!"
        set "newTarget=%folder2Target%\!newFileName!"
        if exist "!newTarget!" (
            set /a copyCounter+=1
            goto loopFolder2
        )
        move "%%f" "!newTarget!" >nul
        echo Moved: "%%~f" → "!newFileName!"
    ) else (
        move "%%f" "!targetFile!" >nul
        echo Moved: "%%~f" → "!fileBase!!fileExt!"
    )
)

:: Process all .EXT files under Folder3's subfolders
echo.
echo === Processing Folder3 ===
for /r "%folder3Target%" %%f in (*.EXT) do (
    :: Extract filename and extension
    set "fileBase=%%~nf"
    set "fileExt=%%~xf"
    set "targetFile=%folder3Target%\!fileBase!!fileExt!"
    set "copyCounter=1"

    :: Handle duplicate filenames
    if exist "!targetFile!" (
        :loopFolder3
        set "newFileName=!fileBase!(^!copyCounter^!)!fileExt!"
        set "newTarget=%folder3Target%\!newFileName!"
        if exist "!newTarget!" (
            set /a copyCounter+=1
            goto loopFolder3
        )
        move "%%f" "!newTarget!" >nul
        echo Moved: "%%~f" → "!newFileName!"
    ) else (
        move "%%f" "!targetFile!" >nul
        echo Moved: "%%~f" → "!fileBase!!fileExt!"
    )
)

:: Optional: Clean up empty subfolders after moving files
echo.
echo === Cleaning up empty subfolders ===
for /d /r "%folder2Target%" %%d in (*) do (
    dir /b "%%d" | findstr "^" >nul || rd "%%d"
    if not exist "%%d" echo Deleted empty folder: "%%d"
)
for /d /r "%folder3Target%" %%d in (*) do (
    dir /b "%%d" | findstr "^" >nul || rd "%%d"
    if not exist "%%d" echo Deleted empty folder: "%%d"
)

echo.
echo All tasks completed!
pause

How It Works

  • Auto-Detects Main Folder: Uses %~dp0 to get the script's directory (so it works even if you rename or move Main Folder later).
  • Recursive File Search: The for /r command scans every subfolder under Folder2 and Folder3 for .EXT files.
  • Duplicate Handling: If a file with the same name already exists in the target folder, it appends a counter (like File1(1).EXT, File1(2).EXT) until it finds a unique filename.
  • Optional Cleanup: The final section deletes any empty subfolders left after moving files—you can remove this part if you want to keep empty folders.

Usage Steps

  1. Copy the code above into a text editor (like Notepad).
  2. Save the file as MoveEXTFilesUp.bat (make sure the "Save as type" is set to "All Files" to avoid adding a .txt extension).
  3. Place the .bat file directly into C:\Main Folder.
  4. Double-click the batch file to run it—it will show you each file being moved and any empty folders being deleted.

内容的提问来源于stack exchange,提问作者user2826193

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:22:39