-5

I'm developing a C# app that needs to rename a group of files that are in the same folder to add an extension (so if there's a file called myFile, it's file name changes into myFile.ext). How can I achieve this in C#?

Hyblocker
  • 101
  • 4
  • 11
  • 1
    Possible duplicate of [Executing Batch File in C#](http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – devRicher Nov 26 '16 at 12:31
  • If you want to rename a file without using the command line utility like REN you should look at http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp – Steve Nov 26 '16 at 12:32
  • @devRicher: I think he is better off coding it in C# instead of running batch file. The mention of batch is just a way to clarify the intent. – Daniel Nov 26 '16 at 12:32
  • @dani The post explains how to run a batch command. – devRicher Nov 26 '16 at 12:33
  • @devRicher I don't want to run a batch file from my c# app, i just want to know how I can achieve the same results with c# – Hyblocker Nov 26 '16 at 12:38
  • Have you even taken a look at the question, or just on the title? Look at the question, the first answer perfectly explains how to run batch commands without creating batch files. @Hyblocker – devRicher Nov 26 '16 at 12:39
  • @Steve Thanks, but what I want is to rename all files in a folder. sorry for not clarifying this in the original post :| – Hyblocker Nov 26 '16 at 12:39
  • @devRicher Sorry. just looked at it. It is good, but I need to run it from a specific folder, like: C:\Users\Owner\AppData\Local\Temp\myfolder – Hyblocker Nov 26 '16 at 12:43
  • 1
    You just need a call to DirectoryGetFiles then loop – Steve Nov 26 '16 at 12:43
  • @devRicher ??? How? – Hyblocker Nov 26 '16 at 12:44
  • If its so hard for you, why don't you just create a batch file, run it, then delete it? It's only 3 lines and perfect if you have no idea what to do here. @Hyblocker – devRicher Nov 26 '16 at 12:45
  • @dev Richer Managed – Hyblocker Nov 26 '16 at 13:04
  • You can do all this by starting a cmd process http://stackoverflow.com/questions/1255909/execute-cmd-command-from-code – Vinod Srivastav Nov 28 '16 at 12:25

1 Answers1

0

I manged to find a way. But for those who need help:

foreach (var srcPath in Directory.GetFiles(tmppath))
{
  //To customize for yourself:
  //replace "tmppath" with what ever you want
  File.Move(srcPath, srcPath+".jpg");
}
Hyblocker
  • 101
  • 4
  • 11