Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27417

visual studio 2012 - multiple for loops

$
0
0
my question is this. I have a project that moves and renames files based on certain criteria. It will search through all client files or if you type in a single client code it should only search through that clients files and then move out of the for loop.

Here is a sample of my code. It looks at the single clients files but after that goes into the for loop searching all client files. How do I make it skip searching all files ONLY IF THE SINGLE CLIENT is selected?

Code:

  Public Sub btn_SearchMdocs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SearchMdocs.Click
        Try

            Dim quarter As String = tb_QuarterSelected.Text
        If (quarter) = 0 Then
            MsgBox("Please select a quarter before continuing.")
            Exit Sub
        End If
        'defines what to look for in file name
        Dim pattern As String = "*" & quarter & "*"

        'sets file directories to move files from/to
        Dim QtrsourceDir As String = G.StoreroomDirectory
        Dim QtrdestDir As String = G.StagingDirectory
        QtrdestDir = QtrdestDir.Replace("\\", "\\")
            Dim fileCount As Integer = 0
            Dim singlefilecount As Integer = 0
            Dim sw As StreamWriter = New StreamWriter("\\cps1\m3\electronicquarterlyw2logs\CopiedFiles\" & "CopiedFilesLog_" & Date.Today.ToString("MM_dd_yy") & ".txt", True)

        'finds files, numbers them and then copies them if they are not in secureview company set
            For Each f As String In Directory.EnumerateFiles(QtrsourceDir, pattern, SearchOption.AllDirectories)
                Dim fileName As String = Path.GetFileName(f)
                Dim ind As Integer = fileName.IndexOf("_" & quarter)
                Dim code As String = fileName.Substring(0, ind)
                Dim destFile As String = QtrdestDir & code & fileName.Substring(ind)
                If code = tb_singlecocode.Text Then
                    sw.WriteLine(DateTime.Now.ToString & "_" & fileName)
                    File.Copy(f, destFile)
                    singlefilecount = singlefilecount + 1
                End If
            Next
            sw.Close()

            'make sure that the co code is not in secureview co set
            For Each item As Object In lb_M3test.Items
                For Each f As String In Directory.EnumerateFiles(QtrsourceDir, pattern, SearchOption.AllDirectories)
                    Dim fileName As String = Path.GetFileName(f)
                    Dim ind As Integer = fileName.IndexOf("_" & quarter)
                    Dim code As String = fileName.Substring(0, ind)
                    Dim destFile As String = QtrdestDir & code & fileName.Substring(ind)
                    If item.ToString = code Then
                        File.Copy(f, destFile)
                        sw.WriteLine(DateTime.Now.ToString & "_" & fileName)
                        'sw.Close()
                        fileCount = fileCount + 1
                    End If
                Next
            Next


            sw.Close()

            If singlefilecount > 0 Then
                MessageBox.Show(singlefilecount & " files were copied", "Copy Files", MessageBoxButtons.OK)
            Else
                MessageBox.Show(fileCount & " files were copied", "Copy Files", MessageBoxButtons.OK)
            End If
            tb_CountMDOC.Text = fileCount

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

any help would be awesome!! Thank you in advance.

Viewing all articles
Browse latest Browse all 27417

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>