I need to get a collection of file names from a folder on a remote server using VBA in excel 2010. I have a function that works and in the majority of cases it would do the
I thought that there would be an API which could get me the file names in a directory without looping but couldn't find it. All the code that I know of involves looping either using fso
or dir
.
So is it possible to get the file names without looping. I guess yes... Here is one way I can think of...
When you type the below command in DOS Prompt, the entire file structure is sent to a text file
Dir C:\Temp\*.* > C:\Temp\MyFile.Txt
Doing the above from VBA
Sub Sample()
Dim sPath As String
sPath = "C:\Temp\"
'~~> DIR C:\Temp\*.* > C:\Temp\MyFile.txt
retval = Shell("cmd.exe /c Dir " & sPath & "*.* > " & sPath & "MyFile.Txt")
End Sub
For example (This is what is stored in MyFile.Txt)
Volume in drive C is XXXXXXX
Volume Serial Number is XXXXXXXXX
Directory of C:\Temp
10/08/2014 11:28 PM .
10/08/2014 11:28 PM ..
10/08/2014 11:27 PM 832 aaa.txt
10/08/2014 11:28 PM 0 bbb.txt
10/08/2014 11:26 PM 0 New Bitmap Image.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_2.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_2_2.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_3.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_3_2.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_4.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_4_2.bmp
10/08/2014 11:26 PM 0 New Bitmap Image_5.bmp
10 File(s) 832 bytes
2 Dir(s) 424,786,952,192 bytes free
So now all you need to do is copy the text file from the remote folder to your folder and simply parse it to get the file names.