File List skipping First File

DMROD

New Member
Joined
Jul 23, 2010
Messages
6
The following code return a list of files & file path in the directory except for the frist file in directory, but it adds to the end of the list one file path without file. Total lines in list equals number of Files in directory. There is a sub directory in the directory which I do not need in the list nor it contents. What I am missing?

Thank you
David


strDir = ThisWorkbook.Path

Let strName = Dir(strDir & "\*.*")
Do While strName <> vbNullString
Let j = j + 1
' Let strArr(i, 1) = strDir & "\" & strName
Let strName = Dir()
'Sheets("List").Cells(j, 1) = strArr
Sheets("List").Cells(j, 1) = strDir & "\" & strName
Loop
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,298
Office Version
  1. 365
Platform
  1. Windows
David

Are you sure the index of the file list starts at 1?

Perhaps you should move j=j+1?
 

DMROD

New Member
Joined
Jul 23, 2010
Messages
6
Norie

I inserted code before the loop to set j = 0 prior to starting the loop. And tried moving the j = j+1 line within the loop. With no success. Any other suggestions.

Thank you
David

Code:
strDir = ThisWorkbook.Path
    Let j = 0
    Let strName = Dir$(strDir & "\*.*")
    Do While strName <> vbNullString
        Let j = j + 1
 
        Let strName = Dir$()
 
        Sheets("List").Cells(j, 1) = strDir & "\" & strName
        Loop
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,298
Office Version
  1. 365
Platform
  1. Windows
Oops, my bad - I misread your code.

It thought you were using another method to get the fiile list, not just Dir.

I think what you need to do is move the line of code that gets the next filename after the line that puts the current filename on the worksheet.

Your very first Dir should get the first filename.

Code:
    strDir = ThisWorkbook.Path
    
    strName = Dir$(strDir & "\*.*") ' get first filename - if there is one    
    
    Do While strName <> vbNullString
 
        j = j + 1
 
        Sheets("List").Cells(j, 1) = strDir & "\" & strName
 
        strName = Dir$() ' get next filename
        
    Loop
 

Watch MrExcel Video

Forum statistics

Threads
1,133,272
Messages
5,657,770
Members
418,412
Latest member
fehr56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top