批处理文件 - 计算文件夹中的文件数并存储在变量中 [英] batch file - counting number of files in folder and storing in a variable

查看:18
本文介绍了批处理文件 - 计算文件夹中的文件数并存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生.请帮帮我

I am very new to this. Please help me

我试图编写一个批处理文件程序来计算文件夹中的文件数并将其分配给一个变量并显示它以验证它已被存储请帮我解决语法问题,

I was trying to write a batch file program to count number of files in a folder and assign that to a variable and display it to verify that it has been stored please help me with the syntax,

先谢谢你-VK

推荐答案

我假设您不想计算隐藏文件或系统文件.

I'm going to assume you do not want to count hidden or system files.

有很多方法可以做到这一点.我将展示的所有方法都涉及某种形式的 FOR 命令.FOR 命令有许多变体,它们看起来几乎相同,但它们的行为却大不相同.初学者可能会感到困惑.

There are many ways to do this. All of the methods that I will show involve some form of the FOR command. There are many variations of the FOR command that look almost the same, but they behave very differently. It can be confusing for a beginner.

您可以通过从命令行输入 HELP FORFOR/? 来获得帮助.但是如果你不习惯阅读它,那么这个帮助有点神秘.

You can get help by typing HELP FOR or FOR /? from the command line. But that help is a bit cryptic if you are not used to reading it.

1) DIR 命令列出目录中的文件数.您可以将 DIR 的结果通过管道传送到 FIND 以获取相关行,然后使用 FOR/F 从该行解析所需的值.这种技术的问题是您搜索的字符串必须根据操作系统使用的语言进行更改.

1) The DIR command lists the number of files in the directory. You can pipe the results of DIR to FIND to get the relevant line and then use FOR /F to parse the desired value from the line. The problem with this technique is the string you search for has to change depending on the language used by the operating system.

@echo off
for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
echo File count = %cnt%

2) 您可以使用 DIR/B/ADHS 列出没有其他信息的非隐藏/非系统文件,将结果通过管道传送到 FIND 以计算文件数,并使用 FOR/F 读取结果.

2) You can use DIR /B /A-D-H-S to list the non-hidden/non-system files without other info, pipe the result to FIND to count the number of files, and use FOR /F to read the result.

@echo off
for /f %%A in ('dir /a-d-s-h /b ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%

3) 您可以使用简单的 FOR 来枚举所有文件,并使用 SET/A 为找到的每个文件增加一个计数器.

3) You can use a simple FOR to enumerate all the files and SET /A to increment a counter for each file found.

@echo off
set cnt=0
for %%A in (*) do set /a cnt+=1
echo File count = %cnt%

这篇关于批处理文件 - 计算文件夹中的文件数并存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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