Add string after filename in c#PKpraveen kumar11yAsked Nov 4, 2015, 10:10 PM EST4 answersShareLinkedInXCopy linkSave Hi, I have to rename the file name with both prefix and sufix. kindly help me out Thank sin advance
Add string after filename in c#PKpraveen kumar11yAsked Nov 4, 2015, 10:10 PM EST4 answersShareLinkedInXCopy linkSave Hi, I have to rename the file name with both prefix and sufix. kindly help me out Thank sin advance
Nishant MittalRank #692 · 3.5K reputation11y agoPosted Nov 4, 2015, 10:34 PM EST0Hi, Please provide example.0
PKpraveen kumar185 reputation11y agoPosted Nov 4, 2015, 10:39 PM EST0 Hi Nishant Mittal For example my failename is y.doc, i want to change this as xyz.doc0
Nishant MittalRank #692 · 3.5K reputation11y agoPosted Nov 4, 2015, 10:50 PM EST0Hi .. try this tep 1 : as a first step identify wether the file exists or not before copying the file.using File.Exists() methodStep 2: if the file already exists with same name then delete the existing file using File.Delete()methodStep 3: now copy the File into the new Location using File.Copy() method.Step 4: Rename the newly copied file.Try This: string NewFilePath = Path.Combine(NewFileLocation, fileName); if(File.Exists(NewFilePath)) { File.Delete(NewFilePath); } //Now copy the file first File.Copy(FileLocation, NewFileLocation, true); //Now Rename the File File.Move(NewFilePath, Path.Combine(NewFileLocation, "File.txt")); 0Rajeesh MenothRank #118 · 18.4K reputation11y agoPosted Nov 4, 2015, 11:19 PM EST0Hi, Try this Example : C#Copyvar files = Directory.GetFiles(@"C:\yourFolder", "*.pdf"); string prefix = "SomePrefix"; foreach (var file in files) { string newFileName = Path.Combine(Path.GetDirectoryName(file), (prefix + Path.GetFileName(file))); File.Move(file, newFileName); }Reference : http://stackoverflow.com/questions/29418252/add-prefix-to-all-file-names-in-a-folder-with-a-certain-extension-using-c-sharp 0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeesh MenothRank #118 · 18.4K reputation11y agoPosted Nov 4, 2015, 11:19 PM EST0Hi, Try this Example : C#Copyvar files = Directory.GetFiles(@"C:\yourFolder", "*.pdf"); string prefix = "SomePrefix"; foreach (var file in files) { string newFileName = Path.Combine(Path.GetDirectoryName(file), (prefix + Path.GetFileName(file))); File.Move(file, newFileName); }Reference : http://stackoverflow.com/questions/29418252/add-prefix-to-all-file-names-in-a-folder-with-a-certain-extension-using-c-sharp 0
Nishant MittalRank #692 · 3.5K reputationPosted Nov 4, 2015, 10:34 PM EST
praveen kumar185 reputationPosted Nov 4, 2015, 10:39 PM EST
Nishant MittalRank #692 · 3.5K reputationPosted Nov 4, 2015, 10:50 PM EST
tep 1 : as a first step identify wether the file exists or not before copying the file.
using
File.Exists()methodStep 2: if the file already exists with same name then delete the existing file using
File.Delete()methodStep 3: now copy the File into the new Location using
File.Copy()method.Step 4: Rename the newly copied file.
Try This:
Rajeesh MenothRank #118 · 18.4K reputationPosted Nov 4, 2015, 11:19 PM EST
var files = Directory.GetFiles(@"C:\yourFolder", "*.pdf"); string prefix = "SomePrefix"; foreach (var file in files) { string newFileName = Path.Combine(Path.GetDirectoryName(file), (prefix + Path.GetFileName(file))); File.Move(file, newFileName); }Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts