2014-11-01 130 views
0

我的問題有點複雜。讓我解釋:批量文件命令保留最小的目錄並刪除所有其他文件

我有一個批處理文件,現在將創建4個文件夾 - 它們分別是:

1 
2 
3 
4 

每個4個文件夾都將在他們2個文件。我想要做的是創建一個批處理文件,將保留這4個文件夾中最小的文件夾並刪除其他文件夾。因此,如果文件夾大小如下所示:

1 = 300,000 b 
2 = 325,000 b 
3 = 250,000 b 
4 = 350,000 b 

然後只保留文件夾3,應刪除文件夾1,2和4。

這可以用批處理文件完成嗎?任何幫助,將不勝感激。

謝謝。

回答

0

將文件夾中的這個批處理文件包含4個數據文件夾,然後運行它:

@echo off 
setlocal EnableDelayedExpansion 

rem Get size of all folders 
set smallestSize=9999999999 
for /D %%a in (*) do (
    set size=0 
    for %%b in (%%a\*.*) do set /A size+=%%~Zb 
    if !size! lss !smallestSize! (
     set smallestSize=!size! 
     set smallestName=%%a 
    ) 
) 

echo Folder to keep: "%smallestName%" 
pause 

rem Delete all folders, excepting the smallest one 
for /D %%a in (*) do (
    if "%%a" neq "%smallestName%" rd /S /Q "%%a" 
) 
+0

我測試了該批處理文件並正確識別了最小的文件夾,但在暫停點按Enter後,三個較大的文件夾未被刪除。 – Manny90 2014-11-01 22:43:58

1
@echo off 
    setlocal enableextensions enabledelayedexpansion 

    rem Configure where to start 

    set "root=%cd%" 

    rem Initialize needed variables 

    set "minSize=99999999999999999999" 
    set "minFolder=" 
    set "size=" 
    set "folder=" 
    for %%a in ("%root%") do set "drive=%%~da" 

    rem Retrieve the full list and from it get only the lines that 
    rem contain the folder name and the file count/size 

    for /f "skip=2 delims=" %%z in (' 
     dir /-c /s /a-d "%root%\*" ^| findstr /r /c:"^ [ ]*[0-9]" /c:":\\.*" 
    ') do (

     rem Determine if the current line is a folder name, or folder statistics. 
     rem If we have already read a folder name, the next record should be the sizes 
     rem else we have a folder name or the final statistics for the dir command 

     if defined folder (

      rem Read the size and format as a zero prefixed string to avoid 2^31 limit 
      rem in batch files arithmetic. All the size tests will be a string test. 
      for /f "tokens=3" %%a in ("%%z") do set "size=00000000000000000000%%a" & set "size=!size:~-20!" 

     ) else ( 

      rem Read the folder name and reset the size for this folder. As the colon is used 
      rem to separate the folder name from the rest of the data in the line, the drive 
      rem letter and colon are removed. Append the initially retrieved one. 
      for /f "tokens=2 delims=:" %%a in ("%%z") do set "folder=%drive%%%a" 
      set "size=" 

     ) 

     rem When a size is available, a folder has also being retrieved. 
     rem As we have all the needed information, check if the current folder size 
     rem is less than the previous minimal folder and delete the previous or the 
     rem current one depending on its size 

     if defined size (

      if "!size!" lss "!minSize!" (

       rem Old folder is bigger, remove it and remember current folder info 
       if defined minFolder echo rd /s /q "!minFolder!" 
       set "minSize=!size!" 
       set "minFolder=!folder!" 

      ) else (

       rem Current folder is bigger, remove it 
       echo rd /s /q "!folder!" 
      ) 

      rem In any case, the current folder has been processed. Initialize variables 
      set "size=" 
      set "folder=" 
     ) 
    ) 

大量的代碼,但我想只執行一個dir命令檢索所有所需的信息。

根據您的需要更改root變量。而且,所有的rd命令都只能回顯給控制檯。如果輸出正確,請刪除前綴rd操作的echo命令。

相關問題