Batch File Command(s) To Keep Smallest Directory And Delete All Others

萝らか妹 提交于 2019-12-02 17:04:05

问题


My question is a bit complicated. Let me elaborate:

I have a batch file now that will create 4 folders--they are:

1
2
3
4

Each of these 4 folders will have 2 files in them. What I'd like to do is create a batch file that will keep the smallest of these 4 folders and delete the others. So if the folder sizes were as follows:

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

then only folder 3 should be kept and folders 1, 2, and 4 should be deleted.

Can this be done with a batch file? Any help would be appreciated.

Thanks.


回答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="
        )
    )

A lot of code, but i was trying to execute only one dir command to retrieve all the needed information.

Change the root variable to your needs. Also, all the rd commands are only echoed to console. If the output is correct remove the echo commands that prefix the rd operations.




回答2:


Place this batch file in the folder that contain the 4 data folders, and run it:

@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"
)


来源:https://stackoverflow.com/questions/26689398/batch-file-commands-to-keep-smallest-directory-and-delete-all-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!