根據選擇清單重新命名文件

根據選擇清單重新命名文件

我必須使用一組名稱列表重命名數千個圖像文件,例如名為topviewtopview1topview2等的產品照片,以及sideviewsideview1、 ...、、、等。 所以我想透過創建一個特殊的名稱來自動化這項工作以產品名稱命名的資料夾並將該產品的所有映像複製到其中。我想像這樣重命名此資料夾中的圖像:每當我選擇圖像並按重命名(當然是縮圖視圖)時,它都會出現一個預設名稱列表(或對話框視窗等)的下拉列表,例如,,,,如果清單中沒有預設,則可以選擇輸入新名稱,如果之前使用,則下一次選擇將建立新名稱,等等。sideviewnbackviewfrontview1234F2topviewsideviewfrontviewbackviewtopviewtopviewtopview1topview2 topviewn

我不知道 Windows 透過重命名檔案的程式碼F2,所以我嘗試另一種方法,我透過 Visual Studio 編寫了一個基於 vb.net 的 Windows 應用程式表單,但我堅持使用我的程式碼。請給我一些建議以使其發揮作用。我的程式碼包括運行程式碼和設計器程式碼如下:

運行程式碼:

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

設計師程式碼:

<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

我的設計是這樣的:點擊button4(選擇檔案)在資料夾中選擇一個檔案名稱來輸入舊名稱和組合框新名字然後點擊按鈕1(重新命名)來重命名文件,成功處理後,「下一步」按鈕將循環獲取下一個輸入,所有完成後,「取消」按鈕將退出應用程式。它需要更多的程式碼來獲得我的所有目標,但我想了解關鍵過程(在 vb.net 中重命名)以進一步進行。

相關內容