Available filename in the same folder
LP-700-0220-Q.PDF
LP-700-0220-Q-R0_data.txt
I want to rename PDF file Like
LP-700-0220-Q-R0.PDF
I have Doubts on Files Renaming Using C# Code
Please advise me with simple c# code
.PDF , .data.txt Formats are Fixed

Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 25, 2024, 4:51 AM EDT
Hi Raja, You can take refrence from my code:
using System; using System.IO; class Program { static void Main() { // Define the directory path where the files are located string directoryPath = @"C:\YourFolderPath"; // Get all .data.txt files in the directory string[] dataFiles = Directory.GetFiles(directoryPath, "*.data.txt"); foreach (string dataFilePath in dataFiles) { // Extract the filename without extension string baseFileName = Path.GetFileNameWithoutExtension(dataFilePath); // e.g., LP-700-0220-Q-R0_data // Remove the '_data' suffix to get the new base name for the PDF string newPdfBaseName = baseFileName.Replace("_data", ""); // e.g., LP-700-0220-Q-R0 // Find the corresponding PDF file string pdfFilePath = Path.Combine(directoryPath, baseFileName.Replace("_data", "") + ".PDF"); if (File.Exists(pdfFilePath)) { // Define the new PDF file path string newPdfFilePath = Path.Combine(directoryPath, newPdfBaseName + ".PDF"); // Rename the PDF file File.Move(pdfFilePath, newPdfFilePath); Console.WriteLine($"Renamed '{pdfFilePath}' to '{newPdfFilePath}'"); } else { Console.WriteLine($"No matching PDF file found for '{dataFilePath}'"); } } } }Rajadurai127 reputationPosted Jul 25, 2024, 5:54 AM EDT
Hi Nikunj,
The Code Not working, Please check and revert
Thanks
Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 25, 2024, 6:01 AM EDT
HI Raja,
Can you give some details? what error you getting and from where you getting exectly?
Can you provide some detailed information or your code snisipt? So i can help you batter.
Rajadurai127 reputationPosted Jul 25, 2024, 6:23 AM EDT
After Run this Code there is no action in the specified folder