 |
|
รบกวนช่วยเขียนโค้ด ในการลบตัวเลข ข้างหน้า และ ข้างหลังชื่อไฟล์หน่อยครับ |
|
 |
|
|
 |
 |
|
รายชื่อไฟล์ประมาณนี้ครับ

โค้ดตอนนี้ที่ใช้อยู่
Code (C#)
void SetFileByRegex_(object cri)
{
string _cri = (string)cri;
System.IO.Directory.GetFiles(folSong, "*.mp3").ToList<string>()
.Where(f => new Regex(_cri, RegexOptions.None).IsMatch(Path.GetFileName(f)))
.ToList<string>()
.ForEach(_f =>
{
string __f = folSong + "\\" + new Regex(_cri, RegexOptions.None).Replace(Path.GetFileName(_f), "");
this.Invoke(new Action(() => this.Text = "Check File:" + _f));
if (!System.IO.File.Exists(__f))
{
System.IO.File.Move(_f, __f);
}
else
{
//check file size ....
long a = new FileInfo(_f).Length;
long b = new FileInfo(__f).Length;
if (a >= b - 0.3 * b && a <= b + 0.3 * b)
System.IO.File.Delete(_f);
}
});
}
เวลาเรียกใช้
Code (C#)
SetFileByRegex_(@"(_\d{1,})"); //ลบตัวหลัง
SetFileByRegex_(@"^(\d{1,})[\s\.-]{1,}"); //ลบตัวหน้า
ตัวโค้ดนี้ ใช้ได้ปกติ ครับ ไม่มีปัญหาใดๆ
แต่ ผมอยาก ลบตัวหลัง และ ลบตัวหน้า ที่เดียวเลย
แต่ใส่ AND หรือ OR ใน Where ไม่ได้ครับ
Tag : .NET, Win (Windows App), C#
|
|
 |
 |
 |
 |
Date :
2020-01-08 17:27:40 |
By :
lamaka.tor |
View :
898 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ล่าสุดผมเขียนประมาณนี้ครับ

แต่ติดตรงการเปลี่ยนชื่อไฟล์
|
 |
 |
 |
 |
Date :
2020-01-08 17:59:26 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เรียบร้อยแล้วครับ
Code (C#)
System.IO.Directory.GetFiles(folSong, "*.mp3").ToList<string>()
.Where(f => new Regex(@"(_\d{1,})", RegexOptions.None).IsMatch(Path.GetFileName(f)) ||
new Regex(@"^(\d{1,})[\s\.-]{1,}", RegexOptions.None).IsMatch(Path.GetFileName(f)))
.ToList<string>()
.ForEach(_f =>
{
string __f = folSong + "\\" +
new Regex(@"^(\d{1,})[\s\.-]{1,}", RegexOptions.None).Replace(
new Regex(@"(_\d{1,})", RegexOptions.None).Replace(Path.GetFileName(_f), ""),"");
this.Invoke(new Action(() => this.Text = "Check File:" + _f));
if (!System.IO.File.Exists(__f))
{
System.IO.File.Move(_f, __f);
}
else
{
//check file size ....
long a = new FileInfo(_f).Length;
long b = new FileInfo(__f).Length;
if (a >= b - 0.3 * b && a <= b + 0.3 * b)
System.IO.File.Delete(_f);
}
});
สามารถแก้ชื่อที่มี
_ตัวเลข.mp3
และ
ตัวเลข ด้านหน้าพร้อมกันได้ ครับ
ปิดกระทู้ล่าย 5555
|
 |
 |
 |
 |
Date :
2020-01-08 18:29:12 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|