Hi Guys,

How's your day? I would like to ask, since I'm a beginner in vb .net, I can't figure this out.

Here's the scenario:

I have a folder with lots of folders inside and inside those folders are .tif files. I want to create a program that will rename those images based from the list from my text file.

Here's the content of my text file:

Filename: woutbar.txt
Code:

1.tif
2.tif
2_001.tif
3.tif
3_001.tif
4.tif
4_001.tif
5.tif
5_001.tif
6.tif
6_001.tif
7.tif
7_001.tif
8.tif
8_001.tif

Filename: withbar.txt

Code:

1.tif
1_001.tif
2.tif
2_001.tif
3.tif
3_001.tif
4.tif
4_001.tif
5.tif
5_001.tif
6.tif
6_001.tif
7.tif
7_001.tif
8.tif
8_001.tif

if the count of tif files is equal to 16, the tif files will be renamed based from the contents of withbar.txt but if it's not 16 the tif files will be renamed based from the contents of woutbar.txt.

I've used for loop but it looped forever. Kindly help me. Thanks in advance.

Recommended Answers

All 10 Replies

See if this helps.
1 ListBox, 1 Button

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            With ListBox1.Items
                .Clear()
                getDirectoryList(fbd.SelectedPath, ListBox1)
                MsgBox("Found.Directory(ies).Count: " & .Count)
            End With
        End If
    End Sub

    Private Sub getDirectoryList(ByVal selCoolFolderPath As String, ByRef selCoolListBox As ListBox)
        For Each foundDirectory In Directory.GetDirectories(selCoolFolderPath)
            With ListBox1
                .Items.Add("file.count: (" & IO.Directory.GetFiles(foundDirectory, "*.*").Length & ") file.path: " & foundDirectory)
                .SelectedIndex = .Items.Count - 1
            End With
            getDirectoryList(foundDirectory, selCoolListBox)
        Next
    End Sub
Member Avatar for Unhnd_Exception

Hi Guys,How's your day?

I'd rather be fishing.

thanks codeorder, i'll try it now.

>>thanks codeorder, i'll try it now.
:)
>>I'd rather be fishing.
I'd rather be skating. xD

Hi codeorder,

i don't see sample of renaming the tif. can you give me some?

Dim myCoolFile As String = "C:\test.txt" '// your original file.
        Dim myRenamedCoolFile As String = "testRenamed.txt" '// only your new file name and extension.  file extension can be changed.

        If IO.File.Exists(myCoolFile) Then
            My.Computer.FileSystem.RenameFile(myCoolFile, myRenamedCoolFile)
            MsgBox("File Renamed.", MsgBoxStyle.Information)
        Else
            MsgBox("File Does Not Exist.", MsgBoxStyle.Critical)
        End If

yes, thanks, i already know that code but I can't implement it based on the above condition:

if the count of tif files is equal to 16, the tif files will be renamed based from the contents of withbar.txt but if it's not 16 the tif files will be renamed based from the contents of woutbar.txt.

>>...I can't implement it based on the above condition:
;)

Imports System.IO

Public Class Form1
    Private arTemp() As String, iT As Integer
    Private myCoolFileExtension As String = "*.tif"
    Private myCool16File As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\withbar.txt"
    Private myOtherFile As String

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            getDirectoryList(fbd.SelectedPath, ListBox1)
            MsgBox("(:.done.:)")
        End If
    End Sub

    Private Sub getDirectoryList(ByVal selCoolFolderPath As String, ByRef selCoolListBox As ListBox)
        For Each foundDirectory In Directory.GetDirectories(selCoolFolderPath)
            Select Case Directory.GetFiles(foundDirectory, myCoolFileExtension).Length '// check File.Count.
                Case 16
                    renameFiles(foundDirectory, myCool16File)
                Case Else
                    '  renameFiles(foundDirectory, myOtherFile)
            End Select
            getDirectoryList(foundDirectory, selCoolListBox)
        Next
    End Sub

    Private Sub renameFiles(ByVal selCoolFolder As String, ByVal selFile As String)
        arTemp = File.ReadAllLines(selFile) : iT = 0
        For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                                    (selCoolFolder, FileIO.SearchOption.SearchTopLevelOnly, myCoolFileExtension)
            If Not Path.GetFileName(myCoolFile) = arTemp(iT) Then
                If Not File.Exists(Path.GetDirectoryName(myCoolFile) & "\" & arTemp(iT)) Then
                    My.Computer.FileSystem.RenameFile(myCoolFile, arTemp(iT))
                Else
                    ' //.error.More files in folder than there are file.names in .txt file.
                    Exit For
                End If
            End If
            If Not iT = arTemp.Length - 1 Then
                iT += 1
            End If
        Next
    End Sub
End Class

thanks codeorder, you're simply the best.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.