2013-05-08 39 views
0

我正在清理和組織我的NAS文件夾。Windows批處理 - 將x數量的文件夾+內容移動到另一個文件夾

但是,我有超過1800+文件夾在其中的文件夾,我需要將其分成18個文件夾,每個文件夾包含最多100個文件夾。他們被移動到的文件夾的名稱是不相關的,但在頂層應該是順序的。所有文件夾內容都應該一起移動。

此外,我需要一個批處理文件,這也反轉了這一點。

這是需要的原因是由於我用來索引和組織我的文件夾和文件夾內容的程序無法輕鬆處理超過100個文件夾的文件夾。

我已經嘗試過自己,但最終完全消失了。

+0

你應該顯示一些自己的研究。 – Endoro 2013-05-08 22:42:00

回答

0

這樣的事情?

@ECHO OFF 

SET destination=c:\temp\ 
SET source=c:\windows\system32\ 
SET dcount=0 
SET fcount=0 
SETLOCAL ENABLEDELAYEDEXPANSION 

MKDIR %destination%dir%dcount% 


FOR /f "tokens=*" %%f in ('DIR /b /s /a:d "%source%*"') do (
    SET str=%%f 
    SET str=!str:%source%=! 
    MKDIR %destination%dir!dcount!\!str! 
    COPY %%f %destination%dir!dcount!\!str! 1> NUL 
    SET /a fcount=!fcount!+1 
    IF !fcount! EQU 100 (
    SET fcount=0 
    SET /a dcount=!dcount!+1 
    MKDIR %destination%dir!dcount! 
) 
) 

ECHO DONE. 

顯然,源和目的將需要改變,並沒有試圖健全性檢查或從錯誤中優雅地失敗......

編輯:

第一個版本遞歸下降所有的子目錄並將它們移動到新的結構中 - 基本上破壞了它們的內部嵌套和組織。這第二個保留了嵌套在源目錄的直接子目錄內的子目錄的內部結構......做出選擇。

@ECHO OFF 

SET destination=c:\temp\ 
SET source=c:\windows\system32\ 
SET dcount=0 
SET fcount=0 
SETLOCAL ENABLEDELAYEDEXPANSION 

MKDIR %destination%dir%dcount% 


FOR /f "tokens=*" %%f in ('DIR /b /a:d "%source%*"') do (
    MKDIR %destination%dir!dcount!\%%f 
    XCOPY %source%%%f %destination%dir!dcount!\%%f /e /y 
    SET /a fcount=!fcount!+1 
    IF !fcount! EQU 100 (
    SET fcount=0 
    SET /a dcount=!dcount!+1 
    MKDIR %destination%dir!dcount! 
) 
) 

ECHO DONE. 
0

這適用於我的有限測試,並創建UNDO.BAT以將文件夾恢復到原來的位置。自己測試一下,確保它能做到你想要的。

num是您在每個頂層文件夾

f被用來創建拿着文件夾需要文件夾的數量 - 001,002,003等

的所有啓動它的文件夾中子目錄被移動。

@echo off 
setlocal enabledelayedexpansion 
set num=100 
set c=0 
set f=1001 
set folder=%f:~-3% 
del undo.bat 2>nul 
for /f "delims=" %%a in ('dir /a:d /o:n /b') do (
set /a c=c+1 
    md !folder! 2>nul 
    move "%%a" !folder! 
    echo move "!folder!\%%a" "%cd%" ^& rd !folder! 2^>nul >>undo.bat 
    if !c! EQU %num% (
     set c=0 
     set /a f=f+1 
     set folder=!f:~-3! 
    ) 
) 
相關問題