Rename a file based on a selection list

Aqib Kaved 1 Reputation point
2022-08-23T09:19:51.433+00:00

I have to rename thousands of image files with a set of name list such as photos of products named as topview, topview1, topview2, etc,... and sideview, sideviews1, ...sideviewn, backview, frontview1234, etc. So I want to automate this work by creating a special folder named as product name and copy all the images of that product into it. I want to rename images in this folder like this: whenever I choose an image and press F2 to rename (thumbnail view of course) it will appear a drop down list of preset name list (or dialog window, etc.) such as topview, sideview, frontview, backview and an option of typing new name if not preset in the list, if topview is taken before then the next picking on topview will create new name topview1 then topview2 topviewn, etc.

I don't know the code that Windows rename a file by pressing F2 so I try another way, I code a Windows application form based on vb.net through Visual Studio but I'm stucking with my code. Please give me some advice to make it work. My code include running code and designer code as follows:

Running code:

Imports System.IO  
Public Class Form1  
  
  
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click  
        If New Global.System.Windows.Forms.OpenFileDialog() With {  
            .Title = "Select a file",  
            .InitialDirectory = "D:\TestRen",  
            .Filter = "All files (*.*)|*.*|All files (*.*)|*.*",  
            .FilterIndex = 2,  
            .RestoreDirectory = True  
        }.ShowDialog() = Global.System.Windows.Forms.DialogResult.OK Then  
            Dim strFilename As String = New OpenFileDialog() With {  
            .Title = "Select a file",  
            .InitialDirectory = "D:\TestRen",  
            .Filter = "All files (*.*)|*.*|All files (*.*)|*.*",  
            .FilterIndex = 2,  
            .RestoreDirectory = True  
        }.FileName  
            'get extension  
            Dim extn As String = Path.GetExtension(strFilename)  
        End If  
    End Sub  
  
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Dim var As String  
        var = ComboBox1.Text  
        'It is stucking at this rename syntax  
        My.Computer.FileSystem.RenameFile(strFilename, var + extn)  
    End Sub  
End Class  
Designer code:  
  
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _  
Partial Class Form1  
    Inherits System.Windows.Forms.Form  
  
    'Form overrides dispose to clean up the component list.  
    <System.Diagnostics.DebuggerNonUserCode()> _  
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)  
        Try  
            If disposing AndAlso components IsNot Nothing Then  
                components.Dispose()  
            End If  
        Finally  
            MyBase.Dispose(disposing)  
        End Try  
    End Sub  
  
    'Required by the Windows Form Designer  
    Private components As System.ComponentModel.IContainer  
  
    'NOTE: The following procedure is required by the Windows Form Designer  
    'It can be modified using the Windows Form Designer.    
    'Do not modify it using the code editor.  
    <System.Diagnostics.DebuggerStepThrough()> _  
    Private Sub InitializeComponent()  
        Me.ComboBox1 = New System.Windows.Forms.ComboBox()  
        Me.Button1 = New System.Windows.Forms.Button()  
        Me.Button2 = New System.Windows.Forms.Button()  
        Me.Button3 = New System.Windows.Forms.Button()  
        Me.Button4 = New System.Windows.Forms.Button()  
        Me.SuspendLayout()  
        '  
        'ComboBox1  
        '  
        Me.ComboBox1.FormattingEnabled = True  
        Me.ComboBox1.Items.AddRange(New Object() {"Front view", "Back view", "Left view", "Right view"})  
        Me.ComboBox1.Location = New System.Drawing.Point(15, 112)  
        Me.ComboBox1.Name = "ComboBox1"  
        Me.ComboBox1.Size = New System.Drawing.Size(309, 21)  
        Me.ComboBox1.TabIndex = 0  
        '  
        'Button1  
        '  
        Me.Button1.Location = New System.Drawing.Point(370, 112)  
        Me.Button1.Name = "Button1"  
        Me.Button1.Size = New System.Drawing.Size(75, 23)  
        Me.Button1.TabIndex = 1  
        Me.Button1.Text = "Rename"  
        Me.Button1.UseVisualStyleBackColor = True  
        '  
        'Button2  
        '  
        Me.Button2.Location = New System.Drawing.Point(370, 166)  
        Me.Button2.Name = "Button2"  
        Me.Button2.Size = New System.Drawing.Size(75, 23)  
        Me.Button2.TabIndex = 2  
        Me.Button2.Text = "Next"  
        Me.Button2.UseVisualStyleBackColor = True  
        '  
        'Button3  
        '  
        Me.Button3.Location = New System.Drawing.Point(370, 239)  
        Me.Button3.Name = "Button3"  
        Me.Button3.Size = New System.Drawing.Size(75, 23)  
        Me.Button3.TabIndex = 3  
        Me.Button3.Text = "Cancel"  
        Me.Button3.UseVisualStyleBackColor = True  
        '  
        'Button4  
        '  
        Me.Button4.Location = New System.Drawing.Point(370, 57)  
        Me.Button4.Name = "Button4"  
        Me.Button4.Size = New System.Drawing.Size(75, 23)  
        Me.Button4.TabIndex = 6  
        Me.Button4.Text = "Select a file"  
        Me.Button4.UseVisualStyleBackColor = True  
        '  
        'Form1  
        '  
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)  
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font  
        Me.ClientSize = New System.Drawing.Size(488, 312)  
        Me.Controls.Add(Me.Button4)  
        Me.Controls.Add(Me.Button3)  
        Me.Controls.Add(Me.Button2)  
        Me.Controls.Add(Me.Button1)  
        Me.Controls.Add(Me.ComboBox1)  
        Me.Name = "Form1"  
        Me.Text = "Select a name"  
        Me.ResumeLayout(False)  
  
    End Sub  
  
    Friend WithEvents ComboBox1 As ComboBox  
    Friend WithEvents Button1 As Button  
    Friend WithEvents Button2 As Button  
    Friend WithEvents Button3 As Button  
    Friend WithEvents Button4 As Button  
End Class  

My design is like this: click button4 (Select a file) to pick a file name in a folder to get input of oldname and the combobox for newname then click button1 (Rename) to rename file, after a successful process the Next button will do the loop to get next input, the Cancel button to exit application when all done. It need more many codes to get my all targets but I want to understand the key process (rename in vb.net) to go further.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,099 questions
0 comments No comments
{count} votes