首页 > 解决方案 > 使用批处理在目录下查找大小为 0 的特定文件夹

问题描述

有人可以帮助如何使用批处理在目录下查找大小为 0 的特定文件夹吗?谢谢你。

父文件夹中有很多文件夹,其中一些文件夹里面有文件,一些文件夹是空的。我想使用批处理文件找出空文件夹的名称。有人可以帮忙吗?谢谢

标签: batch-file

解决方案


此脚本扫描目标文件夹并返回目标文件夹中所有空子文件夹的 UNC 路径。

@echo off
setlocal EnableDelayedExpansion
REM This script scans the target folder and returns the UNC paths of all empty subfolders within the target folder.

title Looking for empties, please wait!
cd "%~dp0"
cls

if "%~1" == "" (
    echo ERROR: No folder was specified!  You gotta tell me where to look for empties!
    echo      Please drag-and-drop a folder onto the icon for this script.
    pause
    exit /b
)

if not exist "%~dpn1" (
    echo ERROR: The folder you told me to scan doesn't seem to exist!
    pause
    exit /b
)

set "target=%~dpn1"
echo Scanning: %target%
echo.

REM Grab each subfolder for checking.
for /f "tokens=*" %%a in ('dir "%~dpn1" /s /b /a:d') do (call :checkForBlanks "%%a")

echo.
echo Done.
title Done.
pause
exit /b

:checkForBlanks
    REM We've been given a folder.  We must check if it's empty.
    set "folder=%~1"
    set "scanned=!folder:%target%=!"
    title Looking for empties: %scanned:&=^&%
    set exist=
    for /f %%a in ('dir "%~1" /b') do (set "exist=%%a" & goto :found)
    :found
    if "%exist%" == "" echo EMPTY: %scanned:&=^&%
goto :eof

推荐阅读