1

I am trying to read file names from source directory and then read a separate file to rename and move files to target directory. Below code reads the file names but the problem is it only reading the contents of app.ini file only once i.e. for first file name. Code is not looping app.ini as soon as for loops switches to second file name.

 Dim di As New IO.DirectoryInfo("D:\Transcend")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo
        If (di.GetFiles.Count > 0) Then
            Dim a As Integer = 1
            Dim b As Integer = 1
            For Each dra In diar1
                ComboBox1.Items.Add(dra.FullName.ToString)
                Using reader2 As New IO.StreamReader("D:\Transcend\test\app.ini")
                    Do While reader2.Peek() >= 0
                        Dim line2 = reader2.ReadLine
                        Do Until line2 Is Nothing
                            'line2 = reader2.ReadLine()
                            'ComboBox1.Items.Add(line2.ToString)
                            'Label1.Text = line2
                            If line2 <> Nothing Then
                                If line2.Contains("filename" + a.ToString) Then
                                    Dim values() As String = line2.Split(CChar(":")).ToArray
                                    Dim values2() As String = values(1).Split(CChar(";")).ToArray() 'full filename
                                    Dim values3() As String = values(2).Split(CChar(";")).ToArray() 'keyword to be replaced in filename
                                    Dim values4() As String = values(3).Split(CChar(";")).ToArray() 'fullname in place of keyword
                                    Dim values5() As String = values(4).Split(CChar(";")).ToArray 'destination drive letter
                                    Dim values6() As String = values(5).Split(CChar(";")).ToArray 'destination path after drive letter
                                    ComboBox1.Items.Add(values2(0))
                                    ComboBox1.Items.Add(values3(0))
                                    ComboBox1.Items.Add(values4(0))
                                    ComboBox1.Items.Add(values5(0) + ":" + values6(0))
                                    'Label1.Text = dra.Name.ToString
                                    If dra.Name.ToString.Contains(values2(0)) Then
                                        Dim n As String = dra.Name.Replace(values3(0), values4(0))
                                        File.Copy(dra.FullName, values5(0) + ":" + values6(0) + n)
                                    End If
                                End If
                            End If
                            Exit Do
                        Loop
                        a = a + 1
                    Loop
                    reader2.Close()
                End Using
                b = b + 1
            Next
            Label1.Text = b
        Else
            MsgBox("No files!")
            End
        End If

ouput image:

enter image description here

Above image is to show the output and error, first line is the filename1 and the next 8 lines are the output of the app.ini file. As you can see as soon as the filename1 changes to the next file name i.e. Autorun.inf in the 9th line of the above image the same 8 lines of app.ini(line 2nd to 9th in the above image) should be reiterated after Autorun.inf file name but app.ini is not getting to read after file name increments to Autorun.inf and then to FreeSoftware(JF).htm.

1 Answer 1

1

The only difference between the first and the second file are the a and b values.

On the first run a will start from 1 and it will be incremented for each line in the app.ini file. After reading 8 lines, the final value of a will be 9.

For the second file, the value a isn't reset so it's value will still be 9. This means that the following condition will never be true because the first run only found value from 1 to 8 *.

If line2.Contains("filename" + a.ToString) Then

To fix your issue, you must set the a variable value back to 1 between each file:

Using reader2 As New IO.StreamReader("D:\Transcend\test\app.ini")
    a = 1
    Do While reader2.Peek() >= 0

* I'm assuming that the filename in your .ini file are sorted (i.e. line containing filename9 isn't listed before filename2) and that no external process changed the content of your .ini file between the first and the second file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.