-
February 14th, 2010, 10:19 PM
#1
Bulk rename files program in c#?
Is it possible to make a program that takes files and can take something out of their original name? I.E. the file 1.txt, that file 2.txt, this file 3.txt, changed to take "this" out and only say the 1.txt, that 2.txt, and this 3.txt ?
If it's possible, how would I go about doing that?
-
February 14th, 2010, 10:32 PM
#2
Re: Bulk rename files program in c#?
Yes.
Code:
File.Move( oldFileName, newFileName );
You may want to catch exceptions as some files could be locked.
Use:
Code:
String[] oldFileNames = Directory.GetFiles( folderName, "*file*.txt");
To get a list of all of the text files that contain the word "file" as in your example. Then for each of the filenames in that loop:
Code:
String newFileName = oldFileName.Replace("file", String.Empty);
Be careful that the word "file" is not in the path to the file. I'm sure with some experimenting, you'll work it out from what I have said.
Last edited by rliq; February 14th, 2010 at 10:37 PM.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 14th, 2010, 11:46 PM
#3
Re: Bulk rename files program in c#?
I'm getting the error
Code:
Error 1 'System.Array' does not contain a definition for 'Replace' and no extension method 'Replace' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
for the line
Code:
String newFileName = oldFileName.Replace("file", String.Empty);
Am I suppose to turn oldFileName into a string instead of keeping it as an array? Or turn newFileName into an array?
-
February 14th, 2010, 11:49 PM
#4
Re: Bulk rename files program in c#?
oldFileName is just ONE of the items in the oldFileNames (notice the S on the end) array.
so...
Code:
foreach (String fileName in fileNames)
{
...
}
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 14th, 2010, 11:57 PM
#5
Re: Bulk rename files program in c#?
I have the following now and it shows no errors, will this code work? (I'm afraid of running it without knowing because I don't want to crash my whole computer if I messed it up)
Code:
String[] oldFileNames = Directory.GetFiles(@"C:\Users\comp\Desktop\file - Copy\", "*File*.mp3");
try
{
foreach (string s in oldFileNames)
{
String newFileName = s.Replace("File", String.Empty);
File.Move(s, newFileName);
}
}
catch
{
}
-
February 15th, 2010, 12:00 AM
#6
Re: Bulk rename files program in c#?
That should work.
Why not create a copy of the directory beforehand whilst you are testing, then you're sure not to lose anything. Ahh, silly me, by the look of the folder name, seems you already have
Also, try it on a small directory with one or two mp3 files in it first...
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 15th, 2010, 12:07 AM
#7
Re: Bulk rename files program in c#?
It works perfectly! Thanks for the help!
-
February 15th, 2010, 12:09 AM
#8
Re: Bulk rename files program in c#?
No worries, glad to see you worked it out. It reminds me... I need to rename all of my MP3's too!!
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
July 22nd, 2014, 07:28 AM
#9
Re: Bulk rename files program in c#?
To batch rename files I suggest to try KrojamSoft BatchRenameFiles program
-
July 22nd, 2014, 12:12 PM
#10
Re: Bulk rename files program in c#?
Originally Posted by frankos
To batch rename files I suggest to try KrojamSoft BatchRenameFiles program
Please don't open old threads with spam.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|