• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

List and work with files in directory

Hi all,

I have the following problem and i am hoping there is a sollution for. I have a directory with numbered pdf files in like this:
1.pdf
2.pdf
3.pdf
5.pdf

the names are supposed to be  sequential. I need some way to scan the directory and give me a report about what files are missing.

it should tell me 4.pdf is missing

thanks
0
frankie_jnr
Asked:
frankie_jnr
  • 4
  • 4
1 Solution
 
djon2003Commented:
Here is a code that should do the job.
Dim files() as String = IO.Directory.GetFiles(myDir)

Dim matchedFileCount As Integer = 0
Dim missingFiles as New Generic.List(Of String)
Dim testNumber As Integer = 1

While matchedFileCount < files.length
  If files.indexOf(myDir & "\" & testNumber & ".pdf") = -1 then
    missingFiles.Add(myDir & "\" & testNumber & ".pdf")
  Else
    matchedFileCount += 1
  End If

  testNumber += 1
End While

Open in new window

0
 
djon2003Commented:
Small correction.
Dim files() as String = IO.Directory.GetFiles(myDir)

Dim matchedFileCount As Integer = 0
Dim missingFiles as New Generic.List(Of String)
Dim testNumber As Integer = 1

While matchedFileCount < files.length
  If Array.IndexOf(Of String)(files, myDir & "\" & testNumber & ".pdf") = -1 then
    missingFiles.Add(myDir & "\" & testNumber & ".pdf")
  Else
    matchedFileCount += 1
  End If

  testNumber += 1
End While

Open in new window

0
 
frankie_jnrAuthor Commented:
How would i declare myDir?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
djon2003Commented:
As a simple string which specify our directory to explore.
Dim myDir As String = "C:\Temp"

Open in new window

0
 
frankie_jnrAuthor Commented:
it goes into an infinate loop if i do it and the program does not stop. if i break the program testNumber is in the billions
0
 
frankie_jnrAuthor Commented:
Thanks so much!
This fixed the loop for me
While matchedFileCount < files.Length - 1
0
 
djon2003Commented:
Ehm.. The problem happens when there is other files that are not matching the pattern.

So I changed it to detect the file with the maximum number and loop to that one.
Dim myDir As String = "C:\DropBoxFolder\My Dropbox\CI\Factures\Clinique Physiotech"
        Dim files() As String = IO.Directory.GetFiles(myDir, "*.pdf")

        Dim maxNumber As Integer = 0
        Dim missingFiles As New Generic.List(Of String)
        Dim testNumber As Integer = 1
        Dim parseOutput As Integer = 0

        For i As Integer = 0 To files.Length - 1
            Dim fileNameWOExtension As String = files(i).Substring(files(i).LastIndexOf("\") + 1)
            fileNameWOExtension = fileNameWOExtension.Substring(0, fileNameWOExtension.LastIndexOf("."))
            If Integer.TryParse(fileNameWOExtension, parseOutput) Then maxNumber = Math.Max(parseOutput, maxNumber)
        Next

        While testNumber < maxNumber
            If Array.IndexOf(Of String)(files, myDir & "\" & testNumber & ".pdf") = -1 Then
                missingFiles.Add(myDir & "\" & testNumber & ".pdf")
            End If

            testNumber += 1
        End While

Open in new window

0
 
frankie_jnrAuthor Commented:
perfect worked great! thanks so much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now