批量查找子文件夹中的文件夹,将文件向上移动一级并删除该文件夹

时间:2015-11-10 13:45:46

标签: batch-file cmd

我有这个结构:

    ..
    ..\FolderA\FolderX\File1.txt
    ..\FolderB\FolderX\File2.txt
    ..\FolderC\FolderD\FolderE\FolderX\File3.txt

我需要批量查找所有" FolderX"在子文件夹中移动所有文件" FolderX"升级到一级并删除" FolderX"

    ..
    ..\FolderA\File1.txt
    ..\FolderB\File2.txt
    ..\FolderC\FolderD\FolderE\File3.txt

怎么写一批?我试过这个,但是不完整,代码找不到文件夹:

    @Echo Off
    Set _Source=%~dp0
    Set _FindDir=FolderX
    Set _Path=%_Source%\%_FindDir%
    If Exist "%_Path%" (
    Move /-Y "%_Path%\*.*" "%_Source%"
    For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Path%"') Do Move "%_Path%\%%I" "%_Source%"
    RD /S /Q "%_Path%"
    ) Else (
    Echo There is no %_FindDir% folder in %_Source%
    )

1 个答案:

答案 0 :(得分:-1)

解决:

    @echo off & setlocal enabledelayedexpansion
    for /d /r %~dp0 %%a in (*) do (
    if /i "%%~nxa"=="FolderX" (
    set "folderpath=%%a" (
    move /y !folderpath!\* !folderpath:~,-8!
    rmdir !folderpath!
    )
    )
    )