-1

I am coding to watch a XYZ folder on the timer tick interval 30,000. If the XYZ folder have the filecount+1 then I want to copy a new file to process.

I can code the rest but stuck at:

Error 1 'filesindir' is not declared. It may be inaccessible due to its protection level.".

I want to initialize the Filesindir and filesinfo only once. Not on every Tick but have to move/copy new file every time.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Dim processedfiles = My.Computer.FileSystem.GetFiles(destfolderpath, "*.ps")


    If processedfiles.Count = (pfiles + 1) Then

        If flag = False Then

            Dim foldertosearch As New IO.DirectoryInfo(folderpath)
            Dim filesindir As IO.FileInfo() = foldertosearch.GetFiles("*.txt")
            Dim filesinfo As IO.FileInfo

        End If

        For Each filesinfo In filesindir




        Next
    End If

    pfiles = processedfiles.Count
End Sub
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    If you `Dim` the variables inside the If/Then clause, they are out of scope outside that clause. You need to declare them outside the If, or move the `For Each` loop inside the If. – Blackwood Jul 24 '15 at 23:14
  • 4
    consider the `System.IO.FileSystemWatcher` which seems to be what you are recreating – Ňɏssa Pøngjǣrdenlarp Jul 24 '15 at 23:15

1 Answers1

1

You need to understand how Scope works in VB.Net.

If you declare a variable in a block, e.g.

If Not flag Then
    Dim filesindir As IO.FileInfo() = foldertosearch.GetFiles("*.txt")
End If

That filesindir variable is scoped to the If block. The variable will be inaccessible outside the block.

Jodrell
  • 34,946
  • 5
  • 87
  • 124