We help IT Professionals succeed at work.

Weird problem reading files in directory

rutledgj asked
I have an application that reads in .doc files from a directory, processes them and moves them to an archive directory. My problem is that occassionally the application will leave a couple of files. It is like it just doesn't even see them.  I'm using this code to get a list of all the files:

 files = Directory.GetFiles(sourcePath, "*.doc")
 For Each f As String In files ...

If I go into the dir and open the files and close them, the app picks them up.  The files get ftp'd to the directory. Most files get processed but there is always a couple not processed.

Any ideas what might cause this or how to fix it?
Watch Question

U sure they aint .docx?

Can you post a larger portion of code?


Regardless, according to the MSDN documentation it should pick up both, but to answer your question, I can see the files and they are indeed .doc files.

 They application opens the word doc using the Aspose component and saves it as a .txt file. Then it moves the .doc file to the archive folder. I have checked and the files do not already exist in archive. The remaining code operates only on the .txt file so it is irrelevant.

'look for .doc files
            'loop through the files and convert to txt file.
            files = Directory.GetFiles(sourcePath, "*.doc")

            For Each f As String In files
                fileName = Path.GetFileNameWithoutExtension(f)
                    Dim aw = New Aspose.Words.Document(f)
                   'save word document as txt file
                    aw.Save(sourcePath & "\" & fileName & ".txt", SaveFormat.Text)
                    aw = Nothing
'Move .doc file to archive folder
File.Move(sourcePath & "\" & tmpF, MyProfile.ArchiveFilePath & "\" & tmpF)

Open in new window

Where is the issue occuring?
By that i mean, are they not being moved? or not uploaded?

I've attached a snippet for you to try
it replaces lines 3 to 13
it copies the file to the new location then verifies its existance before deleting the original

Dim di As New DirectoryInfo(sourcePath)

        For Each fi As FileInfo In di.GetFiles("*.doc")
                Dim aw = New Aspose.Words.Document(fi.FullName)
                'save word document as txt file
                aw.Save(sourcePath & "\" & fi.Name.Replace(fi.Extension, "") & ".txt", SaveFormat.Text)
                aw = Nothing
                'Move .doc file to archive folder
                Dim nfi As FileInfo = fi.CopyTo(MyProfile.ArchiveFilePath & "\" & tmpF, True)
                If nfi.Exists Then fi.Delete()

Open in new window


The process fails on the files = Directory.GetFiles(sourcePath, "*.doc") code. It just doesn't return them into the files variable.
Is the "sourcePath" a local folder or a unc path?

have you tried the snippet i sent? its a cleaner way to use and access files and folders

it may also throw an error that may shed a little more light on the issue


It is a unc path.  I'll give the code a try but since I can't predict when the problem will occur (it doesn't happen each time) I'm not sure when I can tell that it helped.
If its a unc path then there is a few reasons why it could be failing

Is it a DFS share or just a shared folder? is the source machine on the local network?

I know i ask a lot of questions but seeing the full picture allows you to pick out the details better


The paths look like this:  \\FSSAN\EDIFiles\Lab\CNC-Lyons\newdata

I guess it is a DFS Share but not sure (how do I tell?). It is on the company network
if you right click on the share root " \\FSSAN\EDI" and call the properties it should have a tab named "DFS", if it hasnt then its probably not

DFS shares generally only cause problems if one of the servers that host the DFS are offline, they will be listed on the DFS tab

it looks like connectivity is the problem,

If it was me i would add a textfile to the folder and rename it "dummy.dat"
then when your process runs try to read that file first as you know it should be there!
if it isnt then either try again later or add code to handle the error


THere is no DFS tab. I don't think connectivity is the issue. It will pick up other documents in the folder without problem.  I had 2 files unprocessed this morning. I ran the application and it didn't pick them up. I then opened the files with Word and closed them and reran the application and it processed them.
Where are the documents coming from?
With you saying that you opened and closed the files i'm wondering if the lock file could be causing the problem

on your loop do you have any code in the catch portion of the try statement?

it may be worth adding something to write any captured error to a text file


Actually in that catch I didn't have any code. Had plenty in the inner try catch codes. I'll add something there and see what happens.  (There is a try before the files = Directory.GetFiles(sourcePath, "*.doc") code.). I'll also try your code snippet. Thanks.
Happy to Help :)