Checking files presence using UNC

ChuckDrago

Active Member
Joined
Sep 7, 2007
Messages
470
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone,

I am fighting a syntax battle. I have an application that needs to verify whether a specific file is present in a specific folder.
Google this and you get a zillion answers. Unfortunately, all I found refer to the folder path as a mapped designation, whereas I need to use the UNC approach.
The actual example is I need to locate if filename "9999Test.pdf" is present in folder "\\bms-files01\CustOrd\".
When using a Dir method, the string Dir(\\bms-files01\ etc. ), it does not recognize the search item at all and I get zilch. Likewise, using an FSO method leads to oblivion.
Therefore, I will appreciate any suggestion as to what is the right syntax to combine the UNC address and filename in a successful search.
Thanks a bunch, as always.
Chuck
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What FSO code did you try that didn't work?
 
Last edited:
Upvote 0
What FSO code did you try that didn't work?

Hi shg,
Guess what? I let the code run and after about 8 minutes it detected the presence of the test file. The folder in question has about 95K records and my test file is probably at the bottom (typical filenames in there start with a number). So I stand corrected: the FSO method worked, but the time of execution is ridiculous. Therefore, my request for help now changes into: Is there a faster method to locate a specific file in a specific folder using the UNC address?
Here is the code I used

<code/>Sub CheckScanDocsCont()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim FName As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
FName = "Test123.pdf"
Set objFolder = objFSO.GetFolder("\\bms-files01\CustOrd")
For Each objFile In objFolder.Files
If objFile.Name = FName Then
MsgBox "File Found!", vbInformation + vbOKOnly
Exit Sub
End If
Next objFile
End Sub
</code>
 
Upvote 0
This works for me
Code:
Sub chk()

   Dim fname As String
   fname = Dir("\\AFolder\MrExcel\297test.xlsx")
   If fname = "" Then
      MsgBox "no"
   Else
      MsgBox "fname"
   End If
End Sub
 
Upvote 0
With FSO:

Code:
Sub CD()
  Const sFold       As String = "\\bms-files01\CustOrd\"
  Const sFile       As String = "Test123.pdf"

  Dim oFSO          As Scripting.FileSystemObject

  Set oFSO = New Scripting.FileSystemObject
  MsgBox sFold & sFile & " exists: " & oFSO.FileExists(sFold & sFile)
End Sub

I'd use Fluff's suggestion, though, if it works for you.
 
Upvote 0
As a function,

Code:
Sub CD()
  If FileExists("\\bms-files01\CustOrd\Test123.pdf") Then
    ' do something useful
  End If
End Sub

Function FileExists(sFile As String) As Boolean
  Static oFSO       As Scripting.FileSystemObject

  If oFSO Is Nothing Then Set oFSO = New Scripting.FileSystemObject
  FileExists = oFSO.FileExists(sFile)
End Function
 
Upvote 0
Shg & Fluff: a great appreciation for your help. Used Fluff's Dir method and the response was instantaneous and accurate. Problem solved. (I kept wandering what had I done before, when attempting it and failed... go figure).
Thank you,
Chuck
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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
Back
Top