File Renamer

To rename some files in a folder

Page 1 of 1

5 Replies - 4490 Views - Last Post: 13 November 2008 - 09:29 AM Rate Topic: -----

#1 rwaldron   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-March 08

File Renamer

Post icon  Posted 13 November 2008 - 03:17 AM

Hi all,

I have some files in a folder c:\todos in the form ORDyymmddNNN

EG: ORD081031001 , ORD081031002 etc.

I need some code to rename these files and add an Extension so they are in the form Oyymmdd.NNN

EG: O081031.001 , O081031.002

Could someone please point me in the Right Directtion ( c#) or any language ?

Thx,
Ray..
Is This A Good Question/Topic? 0
  • +

Replies To: File Renamer

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: File Renamer

Posted 13 November 2008 - 07:13 AM

Are they all in one folder? No exception handling, but this works:
static void Main(string[] args)
{
    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("..\\..\\blah");
    foreach (System.IO.FileInfo fi in di.GetFiles("ORD*"))
    {
        string origName = fi.Name;
        string newName = origName.Insert(origName.Length - 3, ".").Replace("RD", "");
        System.IO.File.Move(System.IO.Path.Combine(fi.DirectoryName, origName), 
            System.IO.Path.Combine(fi.DirectoryName, newName));
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 rwaldron   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-March 08

Re: File Renamer

Posted 13 November 2008 - 08:19 AM

Hiya and thx for the reply.

I was trying to compile your code into an exe using visual studio (csc) but I am getting the below errors.Am I doing simething stupid ?

renamer.cs(11,8): error CS1518: Expected class, delegate, enum, interface, or

struct

renamer.cs(11,25): error CS1001: Identifier expected

renamer.cs(11,27): error CS1518: Expected class, delegate, enum, interface, or

struct

renamer.cs(13,38): error CS1518: Expected class, delegate, enum, interface, or

struct

renamer.cs(21,1): error CS1022: Type or namespace definition, or end-of-file

expected
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: File Renamer

Posted 13 November 2008 - 08:40 AM

That's not the whole thing...this is though.
namespace Renamer
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("..\\..\\blah");
            foreach (System.IO.FileInfo fi in di.GetFiles("ORD*"))
            {
                string origName = fi.Name;
                string newName = origName.Insert(origName.Length - 3, ".").Replace("RD", "");
                System.IO.File.Move(System.IO.Path.Combine(fi.DirectoryName, origName), 
                    System.IO.Path.Combine(fi.DirectoryName, newName));
            }
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 rwaldron   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-March 08

Re: File Renamer

Posted 13 November 2008 - 08:55 AM

Thank you so much,
I have it working..

Could I be so bold as to ask you how to do something very similar but opposite ?

Example:

I now have Files cyymmdd.001 , cyymmdd.002

I want to rename them to CNFyymmdd001 , CNFyymmdd002

Thx a Million..

Ray..
Was This Post Helpful? 0
  • +
  • -

#6 rwaldron   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-March 08

Re: File Renamer

Posted 13 November 2008 - 09:29 AM

Hiya,
Thx so much for your help.

I got the opposite to work by just using replace.
namespace Renamer  
{  
	class Program  
	{  
		static void Main(string[] args)  
		{  
			System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("c:\\todos\\inbox\\finished");  
			foreach (System.IO.FileInfo fi in di.GetFiles("CF*"))  
			{  
				string origName = fi.Name;  
				string newName = origName.Replace("CF", "CNF").Replace(".","");  
				System.IO.File.Move(System.IO.Path.Combine(fi.DirectoryName, origName),   
					System.IO.Path.Combine(fi.DirectoryName, newName));  
			}  
		}  
	}  
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1