Rename image files on server directory

回眸只為那壹抹淺笑 提交于 2019-12-11 06:13:16

问题


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:


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
}



回答2:


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/



来源:https://stackoverflow.com/questions/1603795/rename-image-files-on-server-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!