views:

227

answers:

2

I need some help in renaming some images in a directory located at /images/graphicsLib/.

All image names in /graphicsLib/ have naming convention that looks like this: 400-60947.jpg. We call the "400" part of the file the prefix and we call the "60957" part the suffix. The entire file name we call the sku.

So if you saw the contents of /graphicLib/ it would look like:
400-60957.jpg
400-60960.jpg
400-60967.jpg
400-60968.jpg
402-60988.jpg
402-60700.jpg
500-60725.jpg
500-60733.jpg
etc...

Using C# & System.IO , what is an acceptable way to rename all image files base on the prefix of the file name? Users need to be able to enter in the current prefix, see all images in the /graphicsLib/ that match, then enter in the new prefix to have all those files renamed with the new prefix. Only the prefix of the file gets renamed, the rest of the file name needs to be unchanged.

What I have so far is:

//enter in current prefix to see what images will be affected by
// the rename process,
// bind results to a bulleted list.
// Also there is a textbox called oldSkuTextBox and button
// called searchButton in .aspx


private void searchButton_Click(object sender, EventArgs e)

{

string skuPrefix = oldSkuTextBox.Text;


string pathToFiles = "e:\\sites\\oursite\\siteroot\\images\graphicsLib\\";  

string searchPattern = skuPrefix + "*";

skuBulletedList.DataSource = Directory.GetFiles(pathToFiles, searchPattern);

skuBulletedList.DataBind();

}



//enter in new prefix for the file rename
//there is a textbox called newSkuTextBox and
//button called newSkuButton in .aspx

private void newSkuButton_Click(object sender, EventArgs e)

{

//Should I loop through the Items in my List,
// or loop through the files found in the /graphicsLib/ directory?

//assuming a loop through the list:

foreach(ListItem imageFile in skuBulletedList.Items)

{

string newPrefix  = newSkuTextBox.Text;

//need to do a string split here?
//Then concatenate the new prefix with the split
//of the string that will remain changed?

 }

}
+1  A: 

Actually you should iterate each of the files in the directory and rename one by one

To determine the new file name, you may use something like:

String newFileName = Regex.Replace("400-60957.jpg", @"^(\d)+\-(\d)+", x=> "NewPrefix" + "-" + x.Groups[2].Value);

To rename the file, you may use something like:

File.Move(oldFileName, newFileName);

If you are not familiar with regular expressions, you should check: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

And download this software to pratice: http://www.radsoftware.com.au/regexdesigner/

Ciwee
Thanks Andre - What is your reasoning using RegEx over String.split? I could get either of these suggestions to work, but like to know more.... Regards, Doug
Doug
Split works fine, but Regex would give you more flexibility. In your case it's easy to split, but let's say you want to replace using a more complex pattern like: 012-123112-167-ab-128-bb.jpg. Let's say you want to replace the first group of letters, that may be anywhere?what do you do? Regex fits better this case.
Ciwee
Good point. Which means I'll have to go back to the end users (company managers) to see what they foresee for future needs. Regards,
Doug
+1  A: 

You could look at string.Split.

Loop over all files in your directory.

string[] fileParts = oldFileName.Split('-');

This will give you an array of two strings:

fileParts[0] -> "400"
fileParts[1] -> "60957.jpg"

using the first name in your list.

Your new filename then becomes:

if (fileParts[0].Equals(oldPrefix))
{
    newFileName = string.Format("(0)-(1)", newPrefix, fileParts[1]);
}

Then to rename the file:

File.Move(oldFileName, newFileName);

To loop over the files in the directory:

foreach (string oldFileName in Directory.GetFiles(pathToFiles, searchPattern))
{
    // Rename logic
}
ChrisF
Thanks Chris. What would my "foreach" statement look like if looping through the directory instead of the bulletedList. I've got code block in translating the logic from a List to the directory object. Regards,
Doug
Although Andre's answer is just as relevant, using string.split is more simple for this particular situation. Chris gets the accepted answer. Thanks to both of you guys. Regards,
Doug