文件夹列表和它的尺寸 [英] Folder list and its sizes

查看:118
本文介绍了文件夹列表和它的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含100多个子文件夹父文件夹。我想其中包含子文件夹和大小的文本文件。能否请你帮我建立批处理程序?


解决方案

虽然你没有表现出任何努力去尝试你自己,我决定帮你,因为任务可能不是特别琐碎完成..

你见过 DIR / S的输出(该命令列出指定目录中的文件及其所有子目录)?它总结文件的总数在最后,还包括整体尺寸:


  D:\\ TEMP> DIR / S / -C
 在驱动器D体积数据
 卷序列号是XXXX-XXXX 的D目录:\\ TEMP2016年5月12日12:00< D​​IR> 。
2016年5月12日12:00< D​​IR> ..
               0文件(S)0字节     总文件中列出:
               0文件(S)0字节
               2目录(S)??????????空闲字节


因此​​,我们可以在给定位置为每个目录做了 DIR / S 命令,使用捕捉它的输出 FOR / F 循环,检索行的最后一前提取大小值。

以下纯批处理文件脚本 - 让我们把它叫做 folder_sizes.bat - 不正是这些步骤:

关闭@echo
SETLOCAL ENABLEEXTENSIONS DisableDelayedExpansionREM //获取提供的参数:
将文件夹=%〜1
设置LOG =%〜2REM //检查提供的参数:
如果没有定义日志集LOG = CON
SETLOCAL EnableDelayedExpansion
>和2(
    如果没有定义的文件夹(
        回声(错误:没有指定目录!
        出口/ B 1
    )否则,如果不存在!文件夹!\\(
        REM / *(尾随\\来检查目录)* /
        回声(错误:目录不存在!
        出口/ B 1
    )
)REM //处理指定目录下的所有子目录:
> !日志! (
    在/ D %% D(!文件夹!\\ *)做(
        FOR / F在跳过= 10令牌=​​ 3%% F('
            DIR / S / -C%%〜FD
        ')做(
            设置字节=!值!
            设置VALUE = %% F
        )
        SETLOCAL DisableDelayedExpansion
        集ITEM = %%〜NXD
        SETLOCAL EnableDelayedExpansion
        REM //返回名称和子目录的大小:
        回声(!ITEM!字节!
        ENDLOCAL
        ENDLOCAL
    )

ENDLOCALENDLOCAL
出口/ B

要在某一目录下运行此脚本 - 例如 D:\\ TEMP - 和日志数据写入到文件 folder_sizes .LOG 在当前目录,请使用以下命令行:

  folder_sizes.bat。D:\\ TEMP\\ folder_sizes.log

在脚本中,延迟扩展是为了避免有问题的切换(子)含exclamantion标记目录()在他们的名字。

请注意, DIR输出/ S 命令依赖于语言,因此脚本可能无法在非英语之外的其他系统提供了预期的效果。在这种情况下,选项字符串跳过= 10代币= 3 FOR / F 循环,特别是标记选项,需要进行相应的调整。

I have a parent folder which contains more than 100 sub-folders. I want a text file which contain sub-folders and their size. Could you please help me to build the batch program?

解决方案

Although you did not show any effort to try it on your own, I decided to help you, because the task might not be that particularly trivial to accomplish...

Have you ever seen the output of dir /S (the command to list files in a specified directory and all its sub-directories)? It summarises the total count of files at the end, including also the overall size:

D:\TEMP>dir /S /-C
 Volume in drive D is DATA
 Volume Serial Number is XXXX-XXXX

 Directory of D:\TEMP

2016/05/12  12:00    <DIR>          .
2016/05/12  12:00    <DIR>          ..
               0 File(s)              0 bytes

     Total Files Listed:
               0 File(s)              0 bytes
               2 Dir(s)      ?????????? bytes free

So we can do a dir /S command for every directory at the given location, capture its output using a for /F loop, retrieve the line before the last one and extract the size value.

The following pure script -- let us call it folder_sizes.bat -- does exactly these steps:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Get provided arguments:
set "FOLDER=%~1"
set "LOG=%~2"

rem // Check provided arguments:
if not defined LOG set "LOG=con"
setlocal EnableDelayedExpansion
>&2 (
    if not defined FOLDER (
        echo(ERROR: no directory specified!
        exit /B 1
    ) else if not exist "!FOLDER!\" (
        rem /* (trailing "\" to check for directory) */
        echo(ERROR: directory not found!
        exit /B 1
    )
)

rem // Process all sub-directories of given directory:
> "!LOG!" (
    for /D %%D in ("!FOLDER!\*") do (
        for /F "skip=10 tokens=3" %%F in ('
            dir /S /-C "%%~fD"
        ') do (
            set "BYTES=!VALUE!"
            set "VALUE=%%F"
        )
        setlocal DisableDelayedExpansion
        set "ITEM=%%~nxD"
        setlocal EnableDelayedExpansion
        rem // Return name and size of sub-directory:
        echo(!ITEM!   !BYTES!
        endlocal
        endlocal
    )
)
endlocal

endlocal
exit /B

To run this script on a certain directory -- for instance D:\TEMP -- and to write the log data to the file folder_sizes.log in the current directory, use the following command line:

folder_sizes.bat "D:\TEMP" ".\folder_sizes.log"

In the script, delayed expansion is toggled in order to avoid problems with (sub-)directories containing exclamantion marks (!) in their names.

Notice that the output of the dir /S command is language-dependent, so the script might not provide the expected results on systems other than English ones. In such situations, the option string "skip=10 tokens=3" of the for /F loop, particularly the token option, needs to be adapted accordingly.

这篇关于文件夹列表和它的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆