954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie question here

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.

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 
Hi Guys,How's your day?

I'd rather be fishing.

Unhnd_Exception
Posting Pro
571 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

thanks codeorder, i'll try it now.

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

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

codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Hi codeorder,

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

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 
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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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.
renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

>>...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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

thanks codeorder, you're simply the best.

renzlo
Junior Poster
107 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: