重命名演示文件夹下方最多 10 代的历史文件

rename up to 10 generations of the history file just below Demo Folder

我这里有一个与 VB.Net 编码相关的大问题。 我的 C 盘中有一个名为 Demo 的文件夹。我有一个项目将在此文件夹中创建 DAT 文件。现在我的问题是。如果文件存在,我必须更改文件名 NOUHINHD.DAT -- NOUHHD01.DAT -- NOUHHD02.DAT -- NOUHHD03.DAT ---- NOUHHD10.DAT.

这意味着如果文件 NOUHINHD.DAT 存在,那么我必须将 NOUHINHD.DAT 重命名为 NOUHHD01.DAT 并创建一个名为 NOUHINHD.DAT.

的新文件名

如果我有一个名为 NOUHHD10 的文件名,然后将其重命名为 NOUHHD11 并连续重命名这 10 个文件并增加编号并始终创建一个新文件 NOUHINHD.DAT.

我正在使用这个代码

Dim path As String = "C:\DEMO\"
no = 10
For Each filename As String In System.IO.Directory.GetFiles(path)
    Dim name As String = (System.IO.Path.GetFileNameWithoutExtension(filename))
    Dim number As String = (Mid(name, 7))
    If number = "HD" Then
        My.Computer.FileSystem.RenameFile(path + name + ".DAT", "NOUHHD01.DAT")
    ElseIf number >= 1 Then
        'For i As Integer = 0 To 0
        '    ArrayName(10) = number
        'Next
        Try
            My.Computer.FileSystem.RenameFile(path + name + ".DAT", "NOUHHD0" + (Convert.ToInt32(number) + 1).ToString + ".DAT")
        Catch ex As Exception
            ErrorMessage(ex.ToString)
        End Try
    Else
        Return Nothing
    End If
    Next
If Not File.Exists(path + "NOUHINHD.DAT") Then
    IBR_SJK_C050.CsvUtil.WriteCSVFile(dt, path + "NOUHINHD.DAT")
End If

用于创建这些文件,但它创建了我这样的格式 DEMO.DAT -- DEMO02.DAT -- DEMO04.DAT -- DEMO06.DAT。它增加了两倍,但应该只增加 1 倍。

这应该有效..

目前 sub 将删除文件 NOUHHD10.DAT 因为当您尝试重命名 NOUHHD09 时,它可能会产生错误,但您可以在代码中更改此行为。

Private Sub RenameDats(path As String)
    'checks if path ends with a backslash and adds it if necessary
    If Not path.Last = "\" Then
        path = path & "\"
    End If
    'array to hold list of files to rename
    Dim filesToRename() As String
    'get list of files to rename matching the pattern
    filesToRename = Directory.GetFiles(path, "NOUHHD*.*")
    'sorts them into order
    System.Array.Sort(Of String)(filesToRename)
    'Check if NOUHHD10.DAT already exists. If so, delete it
    If File.Exists(path & "NOUHHD10.DAT") Then
        File.Delete(path & "NOUHHD10.DAT")
    End If

    'iterates through the array from highest numbered file to lowest
    For i As Integer = filesToRename.GetUpperBound(0) To 0 Step -1
        'gets filname
        Dim name As String = IO.Path.GetFileName(filesToRename(i))
        'gets number of file
        Dim strNum As String = (Mid(name, 7, 2))
        'tries to rename the file
        Try
            'extra check to make sure file number is a valid number
            If IsNumeric(strNum) Then
                'converts filenumber from string to integer
                Dim fileNumber As Integer = CInt(strNum)
                'adds 1 to it and recreates new filename
                Dim newFileNumber As String = Format(fileNumber + 1, "00")
                path = IO.Path.GetDirectoryName(filesToRename(i))
                Dim newFilename As String = "NOUHHD" & newFileNumber.ToString & ".DAT"
                'renames file - note that you don't need the full path in the new filename
                My.Computer.FileSystem.RenameFile(filesToRename(i), newFilename)
            End If
            'catch if there's an error
        Catch ex As Exception
            ErrorMessage(ex.ToString)
        End Try
    Next
    ' if NOUHINHD exists, rename it. If it doesn't exist, create it
    If File.Exists(path & "NOUHINHD.DAT") Then
        Rename(path & "NOUHINHD.DAT", "NOIHHD001.DAT")
    Else
        IBR_SJK_C050.CsvUtil.WriteCSVFile(dt, path + "NOUHINHD.DAT")
    End If
End Sub

对于我自己的问题,我有自己的解决方案。谢谢 @david-wilson 先生

            'file name access
        For Each filename As String In System.IO.Directory.GetFiles(path)
            'remove extensition and location name
            Dim name As String = (System.IO.Path.GetFileNameWithoutExtension(filename))
            'number means filename last 2 digit
            Dim number As String = (Mid(name, 7))
            If number = "BD" Then
                'if last digit was BD then rename it
                My.Computer.FileSystem.RenameFile(path + name + ".DAT", "NOUHBD01.DAT")
            ElseIf number = "HD" Then
                'if last digit was HD then check weather file exist
                If Not File.Exists(path + "NOUHINBD.DAT") Then
                    'if file not exist than create a new file
                    IBR_SJK_C050.CsvUtil.WriteCSVFile(dt, path + "NOUHINBD.DAT")
                End If
                'if number is greater then or equal to num2 (Dim num2 As Integer = 1)
            ElseIf number >= num2 Then
                Dim checkname As String = Mid(name, 5, 2)
                'ckeckname is from Filename it will just take fifth and sixth name and check it weather BD or not
                If checkname = "BD" Then
                    'check weather file exists or not
                    If Not File.Exists(path + "NOUHBD0" + (Convert.ToInt32(number) + 1).ToString + ".DAT") Then
                        'if file doesnot exists than rename filename like NOUHBD03 to NOUHBD04
                        My.Computer.FileSystem.RenameFile(path + name + ".DAT", "NOUHBD0" + (Convert.ToInt32(number) + 1).ToString + ".DAT")
                        'cnumber as filename last 2 digit
                        Dim Cnumber As Integer = Convert.ToInt32(number)
                        'loop for rename file if last two digit untill it is not 0
                        While i <= number + 1
                            If File.Exists(path + "NOUHBD0" + (Convert.ToInt32(Cnumber) - 1).ToString + ".DAT") Then
                                'rename the files
                                My.Computer.FileSystem.RenameFile(path + "NOUHBD0" + (Convert.ToInt32(Cnumber) - 1).ToString + ".DAT", "NOUHBD0" + (Convert.ToInt32(Cnumber)).ToString + ".DAT")
                                Cnumber = Cnumber - 1
                            Else
                                GoTo nextstep1
                            End If
                        End While
                    Else
                    End If
                    'ckeck checkname is weather HD or not
                ElseIf checkname = "HD" Then
                Else
                    Return Nothing
                End If

            Else
            End If
            ' nextstep1: (Uncomment it)
        Next
        'if file not exists NOUHINBD.DAT than create a new one
        If Not File.Exists(path + "NOUHINBD.DAT") Then
            IBR_SJK_C050.CsvUtil.WriteCSVFile(dt, path + "NOUHINBD.DAT")
        End If