strcompnice
strcompnice

Reputation: 208

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 .PDF, in a folder without using a loop such as

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

Upvotes: 1

Views: 6152

Answers (2)

murali
murali

Reputation: 1

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.

Upvotes: 0

Alex K.
Alex K.

Reputation: 175768

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

Upvotes: 2

Related Questions