Loop through files using instead of Dir

0 votes

Upon understanding of Dir function: Dir [( path [, attributes ] ) ]
[Path] is optional and if it is not found, the DIR function will return a zero-length string.
I replaced StrFile = Dir with StrFile = "" on the below code, and it just debug.printed only one file on my directory.

So, What is the meaning of StrFile = Dir?

Sub LoopThroughFiles()
    Dim StrFile As String
    StrFile = Dir("c:\testfolder\*")
    Do While Len(StrFile) > 0
        Debug.Print StrFile
        StrFile = ""
    Loop

End Sub
Mar 20, 2023 in Others by Kithuzzz
• 38,000 points
506 views

1 answer to this question.

0 votes

Upon reading the documentation @Dir function (Visual Basic for Applications) it seems initial call to Dir

StrFile = Dir("c:\testfolder\*")

the first file that matches the pathname will be returned. A while loop is started because at least one file exists, and successive calls to Dir without an argument return other filenames that match the filepath. Up to that point, Dir returns "" to the StrFile and the while loop is broken.

I think that when you set

StrFile = ""

It skips the fetching of additional filenames that match filepath and only the first file that matches is printed.

answered Mar 20, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Where can I find the details of installed files?

The details of recently and locally installed ...READ MORE

answered Mar 8, 2019 in Others by Nabarupa
598 views
0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
14,024 views
0 votes
1 answer

VBA Loop To Import Changing File Names

You can use a FOR loop and ...READ MORE

answered Jan 12, 2023 in Others by narikkadan
• 63,600 points
869 views
0 votes
1 answer

Trying to run different macros based on value of cell in a range

This demonstrates one approach to loop through ...READ MORE

answered Jan 19, 2023 in Others by narikkadan
• 63,600 points
448 views
0 votes
1 answer

How to add if this cell = 0 skip and go next

Try this: Option Explicit Sub AutoMakeInv() ...READ MORE

answered Jan 20, 2023 in Others by narikkadan
• 63,600 points
380 views
0 votes
1 answer

Automating production of graphs using VBA

You have to change: chrtname = Cells(i, 2).Value To: chrtname ...READ MORE

answered Jan 23, 2023 in Others by narikkadan
• 63,600 points
808 views
0 votes
1 answer

See cell content instead of formula in formulabox in Excel using VBA

A VBA Lookup: Lookup Headers in an ...READ MORE

answered Mar 27, 2023 in Others by narikkadan
• 63,600 points
543 views
0 votes
1 answer

array of LocalBusinessJsonLd Schema using next-seo

specify the unique keyoverride properties if you ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
1,052 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP