Win7批处理文件 - 将子文件夹(& Files)移动到Grand-Parent目录

时间:2015-06-16 21:21:03

标签: batch-file cmd merge move subdirectory

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

我正在尝试编写批处理文件来解决问题。但我的搜索结果从未接近我想要实现的目标。

我试图制作这些:

D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110202194232\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110202194331im_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110202194449cs_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110202194453im_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110202194505cs_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110101000000_\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110101072153\http_\www.site.com\*
D:\Utilities\httrack\SITES\RW\web.archive.org\web\20110201061410\http_\www.site.com\*

进入这个:

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

基本上试图移动" http _"进入其祖父目录(" web"),以及它的子文件夹和文件。好像我在拖放,点击"是"合并文件夹,然后单击"移动,但保留两个文件"。

我也希望重命名任何具有相同名称的文件以避免删除。

IE:

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

先谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:0)

挑战:接受。如果此功能内置于robocopyxcopyfso.CopyFile,PowerShell' Move-Item或任何其他实用程序或脚本对象,那么这样做会不会很好方法

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

@echo off
setlocal

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

for /d %%I in ("%root%\2*") 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