-2

I have a huge problem here related to VB.Net coding. I have a folder named Demo located in my C drive. And I have a project which will create DAT files in this folder. Now my problem is. I have to change the file name if file is exists like this way NOUHINHD.DAT -- NOUHHD01.DAT -- NOUHHD02.DAT -- NOUHHD03.DAT ---- NOUHHD10.DAT.

That mean if the file NOUHINHD.DAT exists then I have to rename NOUHINHD.DAT to NOUHHD01.DAT and create a new file name called NOUHINHD.DAT.

Same like if I have file Name called NOUHHD10 then rename it to NOUHHD11 and continuously rename those 10 file with increasing number and always create a new file NOUHINHD.DAT.

I am using this code

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

for creating those file but it create me like this format DEMO.DAT -- DEMO02.DAT -- DEMO04.DAT -- DEMO06.DAT. It is increasing by two times but it should increase just 1 times.

David Wilson
  • 4,369
  • 3
  • 18
  • 31
ZearaeZ
  • 899
  • 8
  • 20

2 Answers2

1

This should work..

At the moment the sub will delete the file NOUHHD10.DAT because when you try to rename NOUHHD09, it will probably produce an error, but you can change this behaviour in the code.

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
  • 4,369
  • 3
  • 18
  • 31
  • Thank you for this help Mr. @david-wilson for this code. It also work for me after some changes. And i also have solve this problem with another code written by myself with your code refrence. Thank you so much. – ZearaeZ Mar 05 '16 at 19:58
-1

For my own question i got my own solution. Thank you Mr. @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
ZearaeZ
  • 899
  • 8
  • 20
  • As it is written, this code will not compile. When the compile errors are fixed, the program will still not work properly as there are logic errors. Also you answer bears little resemblance to the question you posted and will just confuse other users searching for a solution to renaming backups. Finally, you should not use GOTO in VB.net - this command has been deprecated and is only still available to make it easier to migrate old VB6 programs to .net. Oh and in your code, you have not included the declarations for some variables. – David Wilson Mar 06 '16 at 23:22