重命名服务器目录图像文件 [英] Rename image files on server directory

查看:180
本文介绍了重命名服务器目录图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重命名的一些图像位于/图片/ graphicsLib /.

目录需要一些帮助

所有图像名字/ graphicsLib /有命名约定如下: 400-60947.jpg。我们称该文件的preFIX的400的一部分,我们所说的60957的一部分的后缀。整个文件名我们所说的SKU。

如果你看到/ graphicLib的内容,以便/它看起来像:
400-60957.jpg
400-60960.jpg
400-60967.jpg
400-60968.jpg
402-60988.jpg
402-60700.jpg
500-60725.jpg
500-60733.jpg
等等...

使用的 C#&放大器; System.IO,什么是重命名所有的图像文件的基础上的文件名的preFIX一种可接受的方式?用户需要能够在目前的preFIX进入,看到在/ graphicsLib /那场比赛的所有图片,然后在新的preFIX输入将所有这些重命名新的preFIX文件。只有该文件的preFIX被更名,文件名的其他部分需要被改变。

我至今是:

  //在当前preFIX进入,看看图像会受
//重命名过程,
//绑定结果项目符号列表。
//也有一个叫oldSkuTextBox和按钮文本
//中的.aspx称为searchButton


私人无效searchButton_Click(对象发件人,EventArgs的)

{

字符串SKU preFIX = oldSkuTextBox.Text;


字符串pathToFiles =E:\\网站\\ oursite \\ siteroot \\影像\ graphicsLib \\;

字符串是searchPattern = SKU preFIX +*;

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

skuBulletedList.DataBind();

}



//在新的preFIX输入文件重命名
//有一个叫newSkuTextBox文本框和
//按钮的.aspx称为newSkuButton

私人无效newSkuButton_Click(对象发件人,EventArgs的)

{

//应通过我的列表中的项目我循环,
//或通过/ graphicsLib /目录下的文件循环?

//假设通过列表循环:

的foreach(列表项镜像文件中skuBulletedList.Items)

{

串新的preFIX = newSkuTextBox.Text;

//需要在这里做一个字符串分割?
//然后串联新的preFIX与分裂
//字符串,将继续改变?

 }

}
 

解决方案

您可以看看的 string.Split

遍历目录中的所有文件。

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

这会给你两个字符串数组:

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

在列表中使用的第一个名字。

您新的文件名就变成了:

 如果(fileParts [0] .Equals(旧preFIX))
{
    newFileName =的String.Format((0) - (1),新的preFIX,fileParts [1]);
}
 

然后重命名文件:

  File.Move(oldFileName,newFileName);
 

要循环遍历目录中的文件:

 的foreach(字符串oldFileName在Directory.GetFiles(pathToFiles,是searchPattern))
{
    //重命名逻辑
}
 

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?

 }

}

解决方案

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
}

这篇关于重命名服务器目录图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆