vb 文件夹内文件计数_按文件类型计数和总大小

本文介绍了一种使用AutoHotkey脚本改进的方法,用于统计文件夹及其子文件夹中每种文件类型的数量和总大小。脚本允许用户输入路径,包括或排除子文件夹,并生成CSV结果文件,方便在Excel中进一步分析。此外,脚本还处理了Windows文件名中可能存在的无效AutoHotkey变量名字符问题。
摘要由CSDN通过智能技术生成

vb 文件夹内文件计数

Article Update 13-March-2020: I removed the full source code and the code snippets. The article that remains should act as a "design roadmap" for members who want to write the code in the programming language of your choice. If you are interested in discussing the program further, please contact me via the EE message system. 文章更新2020年3月13日:我删除了完整的源代码和代码片段。 对于希望用您选择的编程语言编写代码的成员,剩下的文章应作为“设计路线图”。 如果您有兴趣进一步讨论该程序,请通过EE消息系统与我联系。 INTRODUCTION 介绍

This article was inspired by a recent question here at Experts Exchange. In response to that, I wrote a "quick-and-dirty" script that performs the function requested by the Original Poster, but it has numerous shortcomings. This article describes a major revision of that script, which addresses the shortcomings in the original one.

本文的灵感来自于Experts Exchange上一个最近的问题 。 对此,我编写了一个“快速且肮脏”的脚本,该脚本执行了原始海报所要求的功能,但是它有许多缺点。 本文介绍了该脚本的主要修订版,该修订版解决了原始脚本中的缺点。

PROBLEM DESCRIPTION 问题描述

The objective is to report a count of the files, for each file type (file extension), in a folder and all of its subfolders. In addition, the report should contain the total size of all files for each file type. Example:

目的是报告文件夹及其所有子文件夹中每种文件类型(文件扩展名)的文件计数。 此外,报告应包含每种文件类型的所有文件的总大小。 例:

SOLUTION

I wrote the original "quick-and-dirty" script in AutoHotkey, an excellent (free!) programming/scripting language. The quick explanation for installing AutoHotkey is to visit its website. A more comprehensive explanation is to read my EE article, AutoHotkey - Getting Started. After installation, AutoHotkey will own the AHK file type, supporting the solution discussed in the remainder of this article.

我用AutoHotkey (一种出色的(免费!)编程/脚本语言)编写了原始的“快速处理”脚本。 安装AutoHotkey的快速说明是访问其网站 。 更全面的解释是阅读我的EE文章AutoHotkey-Getting Started 。 安装后,AutoHotkey将拥有AHK文件类型,支持本文其余部分讨论的解决方案。

The new script improves upon the previous one in the following ways:

新脚本在以下方面对上一个脚本进行了改进:

o  The original script hard-codes the name of the source folder. The new script provides a standard Windows "Browse for Folder" dialog that allows the user to navigate to the folder (or type or copy/paste the folder name). o原始脚本对源文件夹的名称进行硬编码。 新脚本提供了标准的Windows“浏览文件夹”对话框,该对话框允许用户导航到文件夹(或键入或复制/粘贴文件夹名称)。 o  The original script saves the results in the same file, so the results are not preserved across multiple executions, unless the user manually saves the file. The new script saves the results in a file with the date and time of execution (including seconds) in the file name (yyyy-MM-dd_HH.mm.ss), so there are never duplicate or overwritten results files. o原始脚本将结果保存在同一文件中,因此除非用户手动保存文件,否则结果不会在多次执行中保留。 新脚本将结果保存到文件名中,文件名(yyyy-MM-dd_HH.mm.ss)中带有执行日期和时间(包括秒),因此不会重复或覆盖结果文件。 o  The original script saves the results in a simple text file. The new script saves the results in a CSV file that may be easily loaded into Excel for additional processing – sorting, formatting, printing, whatever. o原始脚本将结果保存在一个简单的文本文件中。 新脚本将结果保存在CSV文件中,可以轻松将其加载到Excel中以进行其他处理-排序,格式化,打印等。 o  The original script stores the results file in the source base folder. The new script stores the results file in the same folder where the script is located – a better choice. I considered allowing the user to select the folder (and even the file name), but decided not to complicate the process of entering parameters for the run. o原始脚本将结果文件存储在源基本文件夹中。 新脚本将结果文件存储在脚本所在的同一文件夹中–更好的选择。 我考虑过允许用户选择文件夹(甚至文件名),但决定不使输入运行参数的过程复杂化。 o  The original script always includes subfolders. The new script provides an option to include or exclude subfolders. o原始脚本始终包含子文件夹。 新脚本提供了包含或排除子文件夹的选项。 o  The original script does no error checking. The new script does extensive error checking. o原始脚本不进行错误检查。 新脚本进行了广泛的错误检查。 o  The original script terminates with a runtime error if there is a file type that contains a character which is invalid in AutoHotkey variable names (see the o如果某个文件类型包含一个在AutoHotkey变量名称中无效的字符,则原始脚本会因运行时错误而终止(请参见下面的“ GOOD NEWS AND BAD NEWS section below). The new script executes properly when these file types are present. 好消息和坏消息”部分)。 存在这些文件类型时,新脚本将正确执行。 HOW TO RUN THIS SCRIPT 如何运行此脚本

Download the attached file called Counts-and-Sizes.ahk and simply double-click on it in Windows/File Explorer or whatever file manager you use. Since its file type is AHK, AutoHotkey will be launched to process it. If you prefer, the file may be turned into an executable via the AutoHotkey compiler, which is installed during the standard installation of AutoHotkey. If you right-click on an AHK file in Windows Explorer or whatever file manager you use, there will be a context menu pick called Compile Script:

下载名为Counts-and-Sizes.ahk的附件 ,然后在Windows / File Explorer或使用的任何文件管理器中双击即可。 由于其文件类型为AHK,因此将启动AutoHotkey进行处理。 如果愿意,可以通过AutoHotkey编译器将文件转换为可执行文件,该编译器在标准安装AutoHotkey的过程中安装。 如果您在Windows资源管理器或您使用的任何文件管理器中右键单击AHK文件,将有一个名为“编译脚本”的上下文菜单选项:

Select that menu item and it will create an EXE file, which is a stand-alone/no-install executable of the AHK script.

选择该菜单项,它将创建一个EXE文件,该文件是AHK脚本的独立/无需安装的可执行文件。

HOW THE SCRIPT WORKS 脚本的工作方式

For those interested in understanding how the script works, the remainder of this article shows code snippets, with a description of what each snippet does, including screenshots where appropriate (this also acts as a form of documentation for the script).

对于那些对理解脚本的工作原理感兴趣的人,本文的其余部分显示了代码段,并描述了每个代码段的功能,包括适当的屏幕截图(这也作为脚本文档的一种形式)。

Code snippet:

程式码片段:

removed

What it does: The

它的作用:

如果在未初始化或未分配值的情况下读取变量,则 #Warn statement provides a warning when a variable is read without having been initialized or assigned a value. The #Warn语句会提供警告。 SetBatchLines statement sets the script to run at maximum speed, i.e., no "sleeping" will occur in the script. SetBatchLines语句将脚本设置为以最大速度运行,即,脚本中不会发生“睡眠”。

Code snippet:

程式码片段:

removed

What it does: Asks the user to enter the full path of the source folder:

它的作用:要求用户输入源文件夹的完整路径:

It allows the user to navigate/browse to it or type/paste it in. It looks for an ending backslash on the path name and if one was not entered, it appends one (in other words, it works whether or not the user includes the ending backslash in the path). It then checks to see if a source folder was entered, and if so, if the folder exists. If either is not true, it gives the user the opportunity to exit or continue. Note: whether or not the source folder can be reported as null with the Browse For Folder dialog depends on the operating system, so the code checks for it. When the source folder is obtained, it creates a variable with the source files by appending *.* to the source folder.

它允许用户浏览/浏览它或键入/粘贴它。它在路径名上寻找结尾的反斜杠,如果未输入,则附加一个(换句话说,不管用户是否包含,都可以使用)路径中的结尾反斜杠)。 然后,它检查是否已输入源文件夹,如果已输入,则是否存在该文件夹。 如果任何一个都不成立,它将为用户提供退出或继续的机会。 注意:是否可以在“ 浏览文件夹”对话框中将源文件夹报告为null取决于操作系统,因此代码会对其进行检查。 获取源文件夹后,它通过将*。*附加到源文件夹来创建带有源文件的变量。

Code snippet:

程式码片段:

removed

What it does: Asks if the user wants to include subfolders:

功能:询问用户是否要包含子文件夹:

removed

What it does: Creates a file name to store the results. It includes the date and time to the second (ss), so there can never be a duplicate file name. It stores the file in the same folder where the script is located.

作用:创建一个文件名来存储结果。 它包括第二秒(ss)的日期和时间,因此永远不会有重复的文件名。 它将文件存储在脚本所在的同一文件夹中。

Code snippet:

程式码片段:

removed

What it does: Initializes some variables for the first loop through the files. The

它的作用:为通过文件的第一个循环初始化一些变量。 的

BadVarName variables are discussed in the BadVarName变量将在本文后面的“ GOOD NEWS AND BAD NEWS section later in this article. 好消息和坏消息”部分中进行讨论。

Code snippet:

程式码片段:

removed

What it does: The script makes three loops through the folders/files. This is the first. Its purpose is to detect all of the file types and create/initialize a dynamic variable for each file type to store its count and size. This is discussed in detail in the

它的作用:该脚本在文件夹/文件中进行三个循环。 这是第一个。 其目的是检测所有文件类型并为每种文件类型创建/初始化一个动态变量以存储其计数和大小。 这将在

GOOD NEWS AND BAD NEWS section. 好消息和坏消息部分。

Code snippet:

程式码片段:

removed

What it does: This is the second loop through the folders/files. Since a dynamic variable for each file type was created/initialized in the first loop, this loop utilizes those variables by incrementing the count and size for each file found.

它的作用:这是文件夹/文件的第二个循环。 由于每种文件类型的动态变量都是在第一个循环中创建/初始化的,因此该循环通过增加找到的每个文件的计数和大小来利用这些变量。

Code snippet:

程式码片段:

removed

What it does: Initializes some variables for the third loop. Writes out the header row for the Comma Separated Variable (CSV) results file, terminating with a fatal error if the append (write) operation gives a non-zero return code. The header looks like one of these two lines, depending on the choice of including/excluding subfolders:

它的作用:为第三个循环初始化一些变量。 写出逗号分隔变量(CSV)结果文件的标题行,如果附加(写)操作给出的返回码非零,则以致命错误终止。 标题看起来像这两行之一,具体取决于包含/排除子文件夹的选择:

File Type,Count,Size(bytes),Folder=D:\BaseFolder\ (with subfolders) 文件类型,计数,大小(字节),文件夹= D:\ BaseFolder \(带有子文件夹) File Type,Count,Size(bytes),Folder=D:\BaseFolder\ (without subfolders) 文件类型,计数,大小(字节),文件夹= D:\ BaseFolder \(不带子文件夹)

Code snippet:

程式码片段:

removed

What it does: The is the third and final loop through the folders/files. It appends a line to the

它的作用:这是遍历文件夹/文件的第三个也是最后一个循环。 它将一行附加到

每种文件类型的 CSV results file for each file type with its count and size (the OutputFlag variable ensures that only one line is appended for each file type), terminating with a fatal error if the append (write) operation gives a non-zero return code. CSV结果文件及其数量和大小(OutputFlag变量可确保每种文件类型仅附加一行),如果append(写)操作给出的返回码非零,则以致命错误终止。

The CSV lines look like this:

CSV行如下所示:

="jpg",1040,178585752

=“ jpg”,1040,178585752

="rtf",9,6260767

=“ rtf”,9,6260767

="pdf",673,694644678

=“ pdf”,673,694644678

="docx",36,9087570

=“ docx”,36,9087570

="html",249,11401711

=“ html”,249,11401711

="bat",9,8866

=“ bat”,9,8866

*Other*,2,1954

*其他*,2,1954

="class",3391,9249790

=“ class”,3391,9249790

="webpage",1,112

=“网页”,1,112

="0A1",1,17920

=“ 0A1”,1,17920

After loading the CSV results file into Excel, doing some formatting, sorting by the File Type column, and putting in a TOTAL row with SUM formulas for Count and Size, the Excel spreadsheet looks like this:

将CSV结果文件加载到Excel中后,进行一些格式设置,按“文件类型”列进行排序,并在“总计”行中添加“计数”和“大小”的SUM公式,Excel电子表格如下所示:

Code snippet:

程式码片段:

removed 

What it does: Displays a dialog box with the fully qualified file name of the CSV results file:

它的作用:显示一个对话框,其中包含CSV结果文件的标准文件名:

After displaying this dialog, the script exits.

显示此对话框后,脚本退出。

GOOD NEWS AND BAD NEWS 好消息和坏消息

In discussing the last code snippet, we get to the GOOD NEWS AND BAD NEWS section mentioned previously in the article. The good news is that the script does not require the user to specify in advance what file types to process. It takes advantage of a powerful AutoHotkey feature to create dynamically (at runtime) a variable with a name that is based on the contents of another variable. For example, consider the variables named:

在讨论最后一个代码片段时,我们进入了本文前面提到的GOOD NEWS和BAD NEWS部分。 好消息是该脚本不需要用户预先指定要处理的文件类型。 它利用强大的AutoHotkey功能来(在运行时)动态创建一个变量,该变量的名称基于另一个变量的内容。 例如,考虑以下变量:

Count_%FileExt%

Count_%FileExt%

Size_%FileExt%

大小_%FileExt%

If the variable %FileExt% contains the value PDF at runtime, then these variables have the names:

如果变量%FileExt%在运行时包含值PDF ,则这些变量的名称为:

Count_PDF

计数_PDF

Size_PDF

大小_PDF

This technique is used in the script to create dynamically Count and Size variables for every file type encountered during the search, without requiring the user to specify file types in advance.

在脚本中使用此技术可为搜索过程中遇到的每种文件类型动态创建CountSize变量,而无需用户预先指定文件类型。

The bad news is that some characters are valid in Windows file names but invalid in AutoHotkey variable names. The list of these characters may be seen in the code snippet below. For example, if the script encounters a file type of

坏消息是某些字符在Windows文件名中有效,但在AutoHotkey变量名中无效。 这些字符的列表可以在下面的代码片段中看到。 例如,如果脚本遇到文件类型为

ab!

ab!

and attempts to create the variables

并尝试创建变量

Count_ab!

Count_ab!

Size_ab!

大小!

it will terminate with a runtime error – that's what happens with the script I posted in the original question. So this new script checks every character in the file types that it finds and looks for "bad" characters, i.e., characters that would result in an invalid AutoHotkey variable name. When it finds them, it stores the counts and sizes in these variables:

它会因运行时错误而终止–这就是我在原始问题中发布的脚本所发生的情况。 因此,此新脚本检查其找到的文件类型中的每个字符,并查找“错误”字符,即会导致无效的AutoHotkey变量名称的字符。 找到它们后,它将计数和大小存储在以下变量中:

Count_BadVarName

Count_BadVarName

Size_BadVarName

Size_BadVarName

It reports these values in the results file with *Other* as the file type.

它以* Other *作为文件类型在结果文件中报告这些值。

More good news is that these characters are not found in most file types of interest, such as ahk, bat, csv, doc, docx, exe, flv, gif, htm, html, ico, java, kbd, lnk, m4v, nsi, opd, pdf, qt, rtf, sys, tif, tiff, uni, vbs, wma, xls, xlsx, y4m, zip (couldn't resist the A to Z approach).

更好的消息是,在大多数感兴趣的文件类型中都找不到这些字符,例如ahk,bat,csv,doc,docx,exe,flv,gif,htm,html,ico,java,kbd,lnk,m4v,nsi ,opd,pdf,qt,rtf,sys,tif,tiff,uni,vbs,wma,xls,xlsx,y4m,zip(无法抵御A到Z的方法)。

Code snippet:

程式码片段:

removed 

What it does: This is a function that takes a file type as the input parameter and returns TRUE if any character is a "bad" one, i.e., if it contains any character that is invalid in an AutoHotkey variable name. Otherwise, it returns FALSE.

作用:该函数将文件类型作为输入参数,如果任何字符为“不良”字符(即,如果其中包含在AutoHotkey变量名称中无效的任何字符),则返回TRUE 。 否则,它返回FALSE

That's it! I hope this helps the Original Poster as well as other EE members.  If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

而已! 我希望这对原始海报以及其他EE成员有所帮助。 如果您发现本文有帮助,请单击下面的大拇指图标。 这使我知道什么对EE成员有价值,并为以后的文章提供了指导。 非常感谢! 问候乔

翻译自: https://www.experts-exchange.com/articles/12253/Count-and-Total-Size-by-File-Type.html

vb 文件夹内文件计数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值