changing file names throughout directory???

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
I have created (with some help :) I think it was Joshdbr) a long time ago a directory rename program. However, I need help changing this to a file rename program. Our work directory will have a root folder, subfolder and .tif images:

Root
subfolder
22_38839283_939393939.tif
33_23343423_432232434.tif

subfolder
34_34342343_234245252.tif

etc......

There may be hundreds or thousands of images within the above folder convention (along with hundreds of subfolders)

The name convention of the images will always be xx_xxxxxxxx_xxxxxxxxx.tif

Would it be easy to change the below into a file changer instead of having the directory?

VB.NET:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        txtDirectory.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If txtDirectory.Text = "" Then MsgBox("Please enter a valid directory to continue.") : Exit Sub
        If Not Directory.Exists(txtDirectory.Text) Then MsgBox("The directory path entered does not exist!") : Exit Sub
        If txtPrefix.Text = "" Then MsgBox("Please enter a valid prefix to continue.") : Exit Sub
        If chkAdd.Checked = True Then
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            txtDirectory.Text = renameFolders(txtDirectory.Text, txtPrefix.Text)
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        ElseIf chkRemove.Checked = True Then
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            txtDirectory.Text = removePrefix(txtDirectory.Text, txtPrefix.Text)
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        ElseIf RadioButton1.Checked = True Then
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            txtDirectory.Text = renameFiles(txtDirectory.Text, txtPrefix.Text)
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End If
    End Sub
    Private Sub renameFiles(ByVal TopDir As String, ByVal strPrefix As String)
        For Each sFile As String In Directory.GetFiles(TopDir, SearchOption.AllDirectories)
            Try
                My.Computer.FileSystem.RenameFile(sFile, strPrefix & IO.Path.GetFileName(sFile))
            Catch ex As Exception
            End Try
        Next
    End Sub
    Private Function renameFolders(ByVal strRoot As String, ByVal prefixNumber As String)
        Dim subFolders() As String = Directory.GetDirectories(strRoot)
        Dim newname As String = ""
        For Each direct As String In subFolders
            newname = prefixNumber & direct.Split("\").GetValue(direct.Split("\").Length - 1)
            My.Computer.FileSystem.RenameDirectory(direct, newname)
        Next
        newname = prefixNumber & strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1)
        My.Computer.FileSystem.RenameDirectory(strRoot, newname)
        Return (strRoot.Replace(strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1), "") & prefixNumber & strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1))
    End Function

    Private Function removePrefix(ByVal strRoot As String, ByVal strPrefix As String)
        Dim newname As String = ""
        Try
            For Each strFolder As String In Directory.GetDirectories(strRoot)
                newname = strFolder.Split("\").GetValue(strFolder.Split("\").Length - 1).Replace(strPrefix, "")
                My.Computer.FileSystem.RenameDirectory(strFolder, newname)
            Next
            newname = strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1).Replace(strPrefix, "")
            My.Computer.FileSystem.RenameDirectory(strRoot, newname)
        Catch ex As Exception
            MsgBox(ex.Message)
            Return strRoot
        End Try
        Return (strRoot.Replace(strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1), "") & strRoot.Split("\").GetValue(strRoot.Split("\").Length - 1).Replace(strPrefix, ""))
    End Function
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.Dir = txtDirectory.Text
        My.Settings.Pre = txtPrefix.Text
        My.Settings.AddSelected = chkAdd.Checked
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtDirectory.Text = My.Settings.Dir
        txtPrefix.Text = My.Settings.Pre
        If My.Settings.AddSelected = True Then
            chkAdd.Checked = True
        Else
            chkRemove.Checked = True
        End If
    End Sub
End Class

Thanks in advanced

daveofgv
 
Last edited:
I forgot to mention that I need the file format to be as follows:

original:
xx_xxxxxxxx_xxxxxxxxx.tif

need to change to only last 9 digits
xxxxxxxxx.tif
 
Back
Top