以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  如何批量修改文件名字  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=193595)

--  作者:listfor
--  发布时间:2024/9/27 21:13:00
--  如何批量修改文件名字
我有一个Excel文件 包含 报名号,姓名,身份证号,证书号码(证书尾4位按照0001 0002 0003排序),等各类信息,数据量几千条,每一条信息对应一个以报名号命名的JPG图片文件,如何批量更名图片名字为证书号码尾4位+姓名+身份证号.jpg
--  作者:listfor
--  发布时间:2024/9/27 21:15:00
--  
或者说如何自定义根据EXCEL文件的列组成的文件 批量更名为另外一个组合的名字。
--  作者:有点蓝
--  发布时间:2024/9/28 8:56:00
--  
类似:http://www.foxtable.com/webhelp/topics/2334.htm,遍历处理即可

Dim Book As New XLS.Book("c:\\test\\xxx.xls")
Dim 
Sheet As XLS.Sheet = Book.Sheets(0)

\'注意下面的循环变量从1开始,而不是从0开始,因为Excel表的第一行是标题
For n As Integer = 1 To Sheet.Rows.Count -1
    dim 文件名称 as string = 
Sheet(n,1).Value & Sheet(n,2).Value & right(Sheet(n,3).Value,4)

--  作者:listfor
--  发布时间:2024/9/29 15:02:00
--  

图片点击可在新窗口打开查看此主题相关图片如下:企业微信截图_17275933354564.png
图片点击可在新窗口打开查看

[此贴子已经被作者于2024/9/29 15:03:46编辑过]

--  作者:listfor
--  发布时间:2024/9/29 15:05:00
--  
Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Windows.Forms
Imports System.Data

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim folderBrowserDialog As New FolderBrowserDialog()

        \' 显示对话框并检查用户是否点击了“确定”按钮
        If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
            \' 将选定的文件夹路径设置为TextBox的Text属性
            TextBox1.Text = folderBrowserDialog.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim openFileDialog As New OpenFileDialog()
        openFileDialog.Filter = "Excel Files|*.xls;*.xlsx"
        If openFileDialog.ShowDialog() = DialogResult.OK Then
            Dim filePath As String = openFileDialog.FileName
            ImportExcelToDataGridView(filePath)
        End If
    End Sub

    Private Sub ImportExcelToDataGridView(filePath As String)
        Dim excelApp As New Excel.Application()
        Dim workbook As Excel.Workbook = excelApp.Workbooks.Open(filePath)
        Dim worksheet As Excel.Worksheet = CType(workbook.Worksheets(1), Excel.Worksheet)
        Dim rowCount As Integer = worksheet.UsedRange.Rows.Count
        Dim colCount As Integer = worksheet.UsedRange.Columns.Count

        DataGridView1.Rows.Clear()
        DataGridView1.Columns.Clear()

        For i As Integer = 1 To colCount
            DataGridView1.Columns.Add(worksheet.Cells(1, i).Value, worksheet.Cells(1, i).Value)
        Next

        For i As Integer = 2 To rowCount
            Dim dataRow As String() = New String(colCount - 1) {}
            For j As Integer = 1 To colCount
                dataRow(j - 1) = worksheet.Cells(i, j).Value
            Next
            DataGridView1.Rows.Add(dataRow)
        Next

        workbook.Close()
        excelApp.Quit()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim folderBrowserDialog As New FolderBrowserDialog()
        If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
            TextBox2.Text = folderBrowserDialog.SelectedPath
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            Dim oldFileName As String = row.Cells("报名号").Value.ToString & ".jpg"
            Dim newFileName As String = row.Cells("证书号码").Value.ToString().Substring(row.Cells("证书号码").Value.ToString().Length - 4) & _
                                          row.Cells("姓名").Value.ToString & _
                                          row.Cells("证件号码").Value.ToString().Substring(row.Cells("证件号码").Value.ToString().Length - 4) & ".jpg"
            Dim oldFilePath As String = Path.Combine(Me.TextBox1.Text, oldFileName)
            Dim newFilePath As String = Path.Combine(Me.TextBox2.Text, newFileName)

            If File.Exists(oldFilePath) Then
                File.Copy(oldFilePath, newFilePath)
            End If
        Next
    End Sub
End Class

--  作者:listfor
--  发布时间:2024/9/29 15:05:00
--  
谁给完善完善