I have vb.net winform application which create folder rename the files inside folder but when folder path goes long for example "E:\WF_2324\WF_2324_The Shadow Of The Sycamores\WF_2324_The Shadow Of The Sycamores_Part Four_1935 1943\WF_2324_The Shadow Of The Sycamores_Part Four_1935 1943_Chapter Thirty Five_Null Name" and file name look like "WF_2324_The Shadow Of The Sycamores_Part Four_1935 1943_Chapter Thirty Five_Null Name.pdf" even i tired to change group policy for long name and registery edit for long name but cannot worked. I need a manual method to resolve this issue code for rename files below help me resolve. The window is 10 and 11 .net framework is 4.7.2 help me to resolve as technically it is not working suggest me programtically apporach for this a manual method
Private Sub BtnRenameFile_Click(sender As Object, e As EventArgs) Handles BtnRenameFile.Click
Try
If BgWorkerRenameFiles.IsBusy Then
MessageBox.Show("Renaming is already in progress. Please wait.")
Exit Sub
End If
' Validation
If Not Directory.Exists(TxtFolderPathM.Text.Trim()) Then
MessageBox.Show("Folder does not exist.")
Exit Sub
End If
If Not Integer.TryParse(TxtStartPage.Text.Trim(), Nothing) OrElse Not Integer.TryParse(TxtEndPage.Text.Trim(), Nothing) Then
MessageBox.Show("Invalid page numbers.")
Exit Sub
End If
Dim args As New RenameFileArgs With {
.folderPath = TxtFolderPathM.Text.Trim(),
.startPage = Integer.Parse(TxtStartPage.Text.Trim()),
.endPage = Integer.Parse(TxtEndPage.Text.Trim())
}
ProgressBar1.Value = 0
BgWorkerRenameFiles.RunWorkerAsync(args)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Found !!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End Sub
Private Sub BgWorkerRenameFiles_DoWork(sender As Object, e As DoWorkEventArgs) Handles BgWorkerRenameFiles.DoWork
Try
Dim args As RenameFileArgs = CType(e.Argument, RenameFileArgs)
Dim folderPath = args.folderPath
Dim startPage = args.startPage
Dim endPage = args.endPage
Dim pageCount As Integer = endPage - startPage + 1
Dim cCode As String = GlobalSettings.ClientCode
Dim folderName As String = New DirectoryInfo(folderPath).Name
Dim tmpfilename As String = folderName.Substring(folderName.IndexOf("_"c) + 6).Trim()
Dim finalFileName As String = $"{cCode}_{tmpfilename}"
Dim pdfFiles As List(Of String) = Directory.GetFiles(folderPath, "*.pdf").OrderBy(Function(f) f).ToList()
Dim tempFiles As New List(Of String)
If pdfFiles.Count <> pageCount Then
Throw New Exception("Mismatch in file count and page range.")
End If
' Step 1: Temporary Rename
For i As Integer = 0 To pdfFiles.Count - 1
Dim oldPath As String = pdfFiles(i)
Dim tempPath As String = Path.Combine(folderPath, $"_tempfile_{i}.pdf")
File.Move(oldPath, tempPath)
tempFiles.Add(tempPath)
Dim percent = CInt(((i + 1) / pdfFiles.Count) * 50)
BgWorkerRenameFiles.ReportProgress(percent)
Next
' Step 2: Final Rename
For i As Integer = 0 To tempFiles.Count - 1
Dim pageNumber As Integer = startPage + i
Dim newName As String = $"{finalFileName}_Page {pageNumber}.pdf"
Dim newPath As String = Path.Combine(folderPath, newName)
File.Move(tempFiles(i), newPath)
Dim percent = 50 + CInt(((i + 1) / tempFiles.Count) * 50)
BgWorkerRenameFiles.ReportProgress(percent)
Next
BgWorkerRenameFiles.ReportProgress(100)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Renaming Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End Sub
Private Sub BgWorkerRenameFiles_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BgWorkerRenameFiles.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BgWorkerRenameFiles_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BgWorkerRenameFiles.RunWorkerCompleted
MessageBox.Show("Files renamed successfully!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
ProgressBar1.Value = 0
End Sub