Welcome to Dream.In.Code
Become a C# Expert!

Join 150,114 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,946 people online right now. Registration is fast and FREE... Join Now!




File Renamer

 
Reply to this topicStart new topic

File Renamer, To rename some files in a folder

rwaldron
13 Nov, 2008 - 02:17 AM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 10

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..
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: File Renamer
13 Nov, 2008 - 06:13 AM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
Are they all in one folder? No exception handling, but this works:
csharp
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));
}
}

User is offlineProfile CardPM
+Quote Post

rwaldron
RE: File Renamer
13 Nov, 2008 - 07:19 AM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 10

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

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: File Renamer
13 Nov, 2008 - 07:40 AM
Post #4

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
That's not the whole thing...this is though.
csharp
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));
}
}
}
}

User is offlineProfile CardPM
+Quote Post

rwaldron
RE: File Renamer
13 Nov, 2008 - 07:55 AM
Post #5

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 10

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..
User is offlineProfile CardPM
+Quote Post

rwaldron
RE: File Renamer
13 Nov, 2008 - 08:29 AM
Post #6

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 10

Hiya,
Thx so much for your help.

I got the opposite to work by just using replace.
CODE

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));  
            }  
        }  
    }  
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month