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

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

问题描述

我很新了这一点。请帮我

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

thank you in advance -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.

您可以通过在命令行中键入的帮助 FOR /?帮助。但是,这帮助是有点神秘,如果你不习惯阅读。

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结果找得到培训相关线路,然后用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 relevent 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 来列出非隐藏/非系统文件,而无需其他信息,管结果找到数数文件,并用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枚举所有文件,并设置/ 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天全站免登陆