使用批处理计算的特定文件夹中的文件数 [英] Using batch to count the number of specific files in folder
问题描述
我有一大堆的文件夹他们的名字都像a1.txt,a6.txt,a8.txt,在文件的..我需要算来我试过这个批处理文件,但它不承认*作为方法来解释所有数字和不返回正确的答案。
I have a bunch of files in a folder that their names are like a1.txt, a6.txt, a8.txt,..and I need to count them I tried this batch file but it does not recognize * as a way to account for all numbers and does not return the correct answer.
set /a count=0
for /F "tokens=* delims= " %%i in ('dir/s/b/a-d "C:\Users\xyz\desktop\Project\a.*"') do (set /a count=count+1)
你能看见什么,我做错了什么?感谢您的帮助提前。
Can you see what I am doing wrong? Thanks for your help in advance.
推荐答案
有类似的问题面前,我preFER使用的伎俩,我从雷蒙德陈了解到,至极是的使用找到
作为替代 WC -l </ code>
。
Faced with a similiar problem, I prefer to use a trick I learned from Raymond Chen, wich is to use find
as a replacement for wc -l
.
所以下面scrit设置批次变量计数
来匹配 patttern
的文件的数量。就像你的原始脚本,目录被排除在计数。
So the following scrit sets the batch variable count
to the number of files that match patttern
. Like your original script, directories are excluded from the count.
@echo off
setlocal
set count=0
set pattern=a?.txt
use dir /b %pattern% ^| find /c /v ""
for /f %%i in ('dir /b /a-d %pattern_you_are_looking_for% ^| find /c /v ""') do @call set count=%%i
echo %count%
endlocal
这篇关于使用批处理计算的特定文件夹中的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!