473,231 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Renaming files in a directory

I am a novice in C# and need help. I want to write a simple program to read
a bunch of files from a specified directory and rename those files in a
sequential fashion., changing a bunch of image file names.

For example, changing:
a011.jpg
a 011.1.jpg
i234.jpg

to

img001.jpg
img002.jpg
img003.jpg

There are some 600 files, so doing it manually would be a pain. I don't know
my C# or .NET framwork well enough yet to do this by myself quickly. I
think I should be able to do it with a simple console program. I am hoping
somebody could help me.

Dec 30 '05 #1
3 31354
Well, the way I would do it is that I would get all the files in the
directory using the GetFiles method on the DirectoryInfo instance which
represents the directory. At this point, you can sort the files according
to your criteria (if not, use the GetFiles method on the Directory class,
since it should be faster, since it only returns names of the files, and not
file info classes).

Once you have that, you can cycle through the array, and call the static
Move method on the File class to rename the file.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<nu**@null.com> wrote in message
news:em*************@TK2MSFTNGP14.phx.gbl...
I am a novice in C# and need help. I want to write a simple program to
read a bunch of files from a specified directory and rename those files in
a sequential fashion., changing a bunch of image file names.

For example, changing:
a011.jpg
a 011.1.jpg
i234.jpg

to

img001.jpg
img002.jpg
img003.jpg

There are some 600 files, so doing it manually would be a pain. I don't
know my C# or .NET framwork well enough yet to do this by myself quickly.
I think I should be able to do it with a simple console program. I am
hoping somebody could help me.

Dec 30 '05 #2
<nu**@null.com> wrote:
I am a novice in C# and need help. I want to write a simple program to read
a bunch of files from a specified directory and rename those files in a
sequential fashion., changing a bunch of image file names.

For example, changing:
a011.jpg
a 011.1.jpg
i234.jpg

to

img001.jpg
img002.jpg
img003.jpg

There are some 600 files, so doing it manually would be a pain. I don't know
my C# or .NET framwork well enough yet to do this by myself quickly. I
think I should be able to do it with a simple console program. I am hoping
somebody could help me.


Sure - look at Directory.GetFiles and File.Move.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #3
Thanks for you help. This is what I came up with:
using System;

using System.IO;

namespace FileRename

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

try

{

DirectoryInfo di = new DirectoryInfo(@"G:\Pictures\Target");

// Get a reference to each file in that directory.

FileInfo[] fiArr = di.GetFiles("*.jpg");

// Define an Integer Counter

int i = 0;

string path;

// Display the names of the files.

foreach (FileInfo fri in fiArr)

{

//Console.WriteLine(fri.Name);

//Change the prefix to something that is not already there

path = @"G:\Pictures\Target\" + "pic" + i.ToString("D3")+".jpg";

fri.MoveTo(path);

i++;

}
Console.WriteLine("Done");

}
catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

}

This is actually slightly modified from what I ran, but if follows the
guidance I got. I got my pictures sorted and renamed.

<nu**@null.com> wrote in message
news:em*************@TK2MSFTNGP14.phx.gbl...
I am a novice in C# and need help. I want to write a simple program to
read a bunch of files from a specified directory and rename those files in
a sequential fashion., changing a bunch of image file names.

For example, changing:
a011.jpg
a 011.1.jpg
i234.jpg

to

img001.jpg
img002.jpg
img003.jpg

There are some 600 files, so doing it manually would be a pain. I don't
know my C# or .NET framwork well enough yet to do this by myself quickly.
I think I should be able to do it with a simple console program. I am
hoping somebody could help me.

Dec 31 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
5
by: Zachariah | last post by:
I have code that renames files in a folder. Here it is: Dim strPath As String Dim objFSO As New Scripting.FileSystemObject Dim objFolder Dim objFile Dim strOldPath As String Dim strNewPath As...
1
by: boops boops | last post by:
Perhaps this is obvious to everyone but me, but how do you rename an existing project when using the .NET IDE? It allows you to save the current form as something else, but not the whole project....
5
by: Robizzle | last post by:
I'm trying to write a simple console app that will rename all the files in a directory according to any rules supplied by a user during runtime. For example rename all *.jpg to *.jpeg. My problem...
3
by: Daves | last post by:
why the heck can't I use Directory.Move() to rename a directory (containing files) to a entry 2 levels below e.g. uploads/tempfiles -> uploads/tempfiles/48/003files if I do this in one line...
4
by: Martin Simard | last post by:
Hi all, Is there a way to rename the App_Code.dll generated from the code that belongs to the App_Code folder? We are developping several web applications that we are merging in a single...
1
by: GeoDW | last post by:
Hell All, I have looked around and not found the solution I am looking for within the old threads. Here is my problem: I have a directory full of .img and .rrd files that have long filenames...
0
by: brendan | last post by:
I'm trying to write a code that batch renames photos. In the end each photo should be named: AABBB1111YYYYMM222 where AABBB1111 defines a given place. (i.e. CAMTL = montreal and four digits...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.