473,378 Members | 1,122 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,378 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 31368
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.