开发者

Determine files of a specific type in a folder without using loop

Is there any way to use the FileSystemObject in VBA to determine the number of files of a specific type, such as .PD开发者_JAVA技巧F, in a folder without using a loop such as

For Each file In folder.Files
    'Check file type and count
Next


You can do this without the FSO dependency while also negating the need to examine the file extension manually;

dim file as String, countOf As Long
file = Dir$("c:\xxxxxx\*.pdf")
Do until file = ""
    countOf = (countOf + 1)
    file = Dir$()
Loop

MsgBox countOf


For Excel, you need to use a loop. The loop really won't take that long, but, if for some reason your detest loops and you must have a one liner, then batch is the way to go.

I didn't test it but create a text file in the directory, call it whatever.bat, right click the file and select edit (or edit with notepad++). Then insert the following code:

set /a counter = 0
for /f %%f in (*.pdf) do set /a count += 1

echo counter: %counter%
pause

Save the file, and then double-click to open and run the program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜