Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky file rename in C# 1

Status
Not open for further replies.

proximity

Technical User
Sep 19, 2002
132
0
0
GB
Hi,

I have a directory containing numerous files which I want to rename. The files are always 40 characters in length. Example files:

prtx010.prtx010.0199.126291.351.03053254
prtx010.prtx010.0199.336760.335.16530215

This part of the filename is always the same: [highlight #FCAF3E]prtx010.prtx010.0199.[/highlight]

The numbers in bold type (which can range from 000000 to 999999) are what I am interested in. They are always 6 characters in length. The last remaining characters are a Unix timestamp, which can be ignored.

What I want to do is loop through the directory and rename all files that are like the above examples (prtx010.prtx010.0199.*) and rename them as the bit in bold. So, in the above examples, the files would be renamed thus:

126291
336760

So, how would I accomplish this in C#?

Many thanks,

--
SM
 
You're going to need to do some work looking up how to do the following:

Looping through a directory on a file system
Using the String Replace/IndexOf/Substing Functions.

You need to post broken code in order to get a more detailed answer.

Lodlaiden

You've got questions and source code. We want both!
There's a whole lot of Irish in that one.
 
try this
Code:
DirectoryInfo di = new DirectoryInfo(@"C:\files");
string lstrSearchPattern = "prtx010.prtx010.0199.";
foreach (FileInfo fi in di.GetFiles(lstrSearchPattern+"*"))
{
	string newName = fi.Name.Substring(lstrSearchPattern.Length, 6);
	fi.MoveTo(newName);
}
di = null;
 
Thanks very much, ralphtrent. I altered you example a little to produce the desired results and it works brilliantly:
Code:
DirectoryInfo di = new DirectoryInfo(@"\\hs001ftp01-01\ftp\distribution\reports\prod\ADMI");
        DirectoryInfo di2 = new DirectoryInfo(@"\\hs001ftp01-01\ftp\distribution\reports\prod\ADMI\");
        string lstrSearchPattern = "prtx010.prtx010.0199.";
        foreach (FileInfo fi in di.GetFiles(lstrSearchPattern + "*"))
        {
            string newName = fi.Name.Substring(lstrSearchPattern.Length, 6);
            fi.MoveTo(di2 + "EDAD_" + newName);

            ImportRemoteFile();
        }
        di = null;

So, now I end up with files named thus:
EDAD_126291
EDAD_336760

Now all I gotta do is loop through the directory of renamed files and import them one at a time into my database using MySQL Bulk Upload


--
SM

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top