سؤال

انا بحاجة الى بعض المساعدة في إعادة تسمية بعض الصور في دليل يقع في /images/graphicsLib/.

كل صورة أسماء /graphicsLib/ قد اصطلاح التسمية التي تبدو مثل هذا:400-60947.jpg.نسميه "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# & النظام.IO , ما هو وسيلة مقبولة لإعادة تسمية كل صورة ملفات قاعدة على بادئة اسم الملف?المستخدمين يجب أن تكون قادرة على الدخول في التيار البادئة, رؤية جميع الصور في /graphicsLib/ أن المباراة, ثم أدخل في بادئة كل تلك الملفات تسمية جديدة مع البادئة.فقط البادئة من يحصل على ملف إعادة تسمية بقية اسم الملف يحتاج إلى تغيير.

ما لدي حتى الآن هو:

//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?

 }

}
هل كانت مفيدة؟

المحلول

هل يمكن أن ننظر السلسلة.تقسيم.

حلقة فوق كافة الملفات في الدليل الخاص بك.

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

هذا وسوف تعطيك مجموعة من سلسلتين:

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

استخدام الاسم الأول في القائمة الخاصة بك.

الخاص بك اسم الملف الجديد ثم يصبح:

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

ثم إعادة تسمية الملف:

File.Move(oldFileName, newFileName);

حلقة أكثر من الملفات في الدليل:

foreach (string oldFileName in Directory.GetFiles(pathToFiles, searchPattern))
{
    // Rename logic
}

نصائح أخرى

في الواقع يجب عليك تكرار كل من الملفات في الدليل و إعادة تسمية واحدة

لتحديد اسم الملف الجديد ، يمكنك استخدام شيء من هذا القبيل:

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

لإعادة تسمية ملف, يمكنك استخدام شيء من هذا القبيل:

File.Move(oldFileName, newFileName);

إذا كنت لم تكن مألوفة مع التعابير العادية ، يجب أن تحقق:http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

و تحميل هذا البرنامج على pratice:http://www.radsoftware.com.au/regexdesigner/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top