473,396 Members | 1,966 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,396 software developers and data experts.

Renaming files in C# based old file name

1
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 that need to be trimmed down because the program I am using to open these files lists them not only by the name, but by how many characters are in the name. Here is an example of a filename and how I want it to look afterward:

34_1311_0813_Pitt_nc04_WILMAR_NW_2004_08_04.img
454_0254_0583_Rockingham_nc05_MAYODAN_SW_20050624. img

1331_0812_2004_08_04.img
0254_0583_2005_06_24.img

As you can see I only want to keep a couple sets of numbers and the date. Everything else is useless to me. A potential problem is that the names are not consistent in length or how they denote the date at the end.

I'm trying to use C# to do this because I have other programs that need to be built in this language as well. I've read a little and think I know how to read filenames and move files, but I don't have a clue as to the method for renaming the files based on the old filenames.

Thanks for your help!
Apr 27 '07 #1
1 7168
SammyB
807 Expert 512MB
You use System.IO.File.Move to rename files. Here is an example that does most of what you want. It doesn't do anything fancy to the filename: just keeps the first group, so 2007_12_3_important.img will be renamed to 2007.img. Also, I have using System.IO at the top. Warning -- I think move will wipe out any existing file with the same name, so you will want to do a File.Exists on the new filename. Be sure to test it a lot and backup the files before trying it on real data. I made many mistakes on just these few lines of code. :) Sam
Expand|Select|Wrap|Line Numbers
  1. string[] sFiles = Directory.GetFiles (Environment.GetFolderPath (Environment.SpecialFolder.MyPictures));
  2. foreach (string sOld in sFiles)
  3. {
  4.     FileInfo fi = new FileInfo(sOld);
  5.     if (fi.Extension == ".img")
  6.     {
  7.         string s = fi.Name.Split("_".ToCharArray())[0];
  8.         File.Move(sOld, fi.Directory + "\\" + s + fi.Extension);
  9.     }
  10. }
Apr 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
16
by: dudufigueiredo | last post by:
I have one folder containing mp3 files, the folder is: C:\My Shared Folder\Rubber Soul And the files are: 01 drive my car.mp3 02 norwegian wood.mp3 03 you won't see me.mp3 04 nowhere man.mp3...
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...
1
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
1
by: Chris | last post by:
Hi, I create a new VC++ project, then want to rename the default filename Form1 (used for .h, .cpp, .resx) to something else. If I rename it from within Windows explorer the project becomes...
8
by: BillCo | last post by:
I'm updating a legacy app with table naming that makes baby jesus cry. It's a bit of a spider web though... no telling when and where the tables will be called by name. So I wrote this for renaming...
1
by: farseer | last post by:
HI, i have created a DataSet using the DataSet Designer. The default name is DataSet1. I then renamed this to "MyDataSet" and proceed to add my TableAdapters using the Visual Designer. After i...
1
by: Jack Maxwell | last post by:
Hope someone can help here. My brother has asked me if I can assist in renaming about 1500 midi files which are associated with a programm called Cubase SE. With that software installed he can...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.