Win 7 批处理文件 - 将子文件夹(和文件)移动到祖父目录

Win7 Batch File - Moving Subfolders(& Files) to Grand-Parent Directory

我有一个有点复杂的问题。我已经使用 Httrack 从 archive.org 下载了一个存档网站,现在我有数千个子文件夹和文件需要合并才能重建它。

我正在尝试编写一个批处理文件来解决这个问题。但是我的搜索结果永远无法接近我想要达到的目标。

我正在尝试制作这些:

D:\Utilities\httrack\SITES\RW\web.archive.org\web110202194232\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110202194331im_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110202194449cs_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110202194453im_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110202194505cs_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110101000000_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110101072153\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web110201061410\http_\www.site.com\*

进入这个:

D:\Utilities\httrack\SITES\RW\web.archive.org\web\http_\www.site.com\*

基本上是尝试将 "http_" 及其子文件夹和文件移动到其祖父目录 ("web") 中。好像我在拖放一样,单击"Yes"合并文件夹,然后单击"Move, but keep both files"。

我也希望它能重命名所有同名文件以避免删除。

IE:

web\http_\www.site.com\index.html
web\http_\www.site.com\index (1).html
web\http_\www.site.com\index (2).html

在此先感谢您的帮助!!!

挑战:接受。如果此功能内置于 robocopyxcopyfso.CopyFile、PowerShell 的 Move-Item 或任何其他实用程序或脚本对象方法中,岂不是很好?

您可能应该在层次结构的副本上对此进行测试。我做了一些最小的测试,它似乎按预期工作,但如果出现不可预见的问题,它将是破坏性的。

@echo off
setlocal

set "root=D:\Utilities\httrack\SITES\RW\web.archive.org\web\"

for /d %%I in ("%root%*") do (
    set /P "=Moving %%~nxI... "<NUL
    pushd "%%~fI"
    for /r %%J in (*) do (
        set "relative=%%~dpJ"
        setlocal enabledelayedexpansion
        call :mv "%%~fJ" "%root%!relative:%%~fI\=!"
        endlocal
    )
    popd
    rd /q /s "%%~fI"
    echo Complete.
)

goto :EOF

:mv <srcfile> <destdir>
setlocal disabledelayedexpansion
if not exist "%~f2" md "%~f2"
set /a seq = 1
set "filename=%~nx1"
:mv_loop
if exist "%~f2\%filename%" (
    set "filename=%~n1 (%seq%)%~x1"
    set /a seq += 1
    goto mv_loop
)
move "%~f1" "%~f2\%filename%" >NUL
endlocal & goto :EOF