Rename File With LINQ


โปรแกรมหลายๆ โปรแกรม จะรับส่งข้อมูลในรูปของ Text File…

เช่น
ระบบสำนักงานใหญ่ และสาขา…
โดยที่สาขาจะมีการ Export ข้อมูลทรานแซกชั่น ออกมาในรูปของข้อมูล Text File…
แล้ว zip ส่งเข้าสำนักงานใหญ่…

สำนักงานใหญ่จะนำข้อมูล Text file ดังกล่าวมาทำการแตก zip แล้วนำเข้าสู่ระบบ…
ขณะเดียวกัน ทางสำนักงาน บางครั้งก็มีการส่งข้อมูลหลัก(Master data) กลับไปที่สาขา…
เช่น ข้อมูลลูกค้า โปรโมชั่น เป็นต้น….

 

เมื่อวันเวลาผ่านไประยะหนึ่ง…
ด้วยเหตุผลใด… ไม่ทราบ คุณจำเป็นที่จะต้อง นำ Text file เก่า เมื่อต้นปีแต่เอาเฉพาะข้อมูลวันที่ 2011-02-15 ถึง 2011-02-17…
ของทุกสาขา…นำเข้าระบบอีกครั้ง… และด้วยสาขาในระบบคุณมีมากกว่า 800 สาขา
การจะมานั่งเปลี่ยนชื่อไฟล์ที่ละสาขา  และเอาเฉพาะไฟล์ข้อมูลวันที่ 2011-02-15 ถึง 2011-02-17
คุณคงต้องเสียเวลาไปหลายวัน…

 

จึงเป็นที่มาของบทความนี้…
เรามาเขียนโปรแกรมเปลี่ยนชื่อไฟล์ง่ายๆ โดยใช้ LINQ ดังนี้

 

1. เปิดโปรแกรม VS2008 …
     สร้างโปรเจ็กต์ด้วย Console Application (C#)…
     ตั้งชื่อเป็น RenameFileWithLINQ

image

 

2. โปรเจ็กต์ RenameFileWithLINQ

image

 

3.เพิ่มไฟล์ config เพื่อกำหนดพารามิเตอร์ให้โปรแกรม…
    ว่าจะเปลี่ยนชื่อไฟล์อะไรบ้าง …ต้นทาง…ปลายทางอยู่ที่ใด…

image 

 

4. ไฟล์ config  (App.config)

image

 

5. กำหนดไฟล์ config ดังนี้

App.config


<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
  <appSettings>

    <add key=”textFileName” value=”20110215″ />
    <add key=”OldName” value=”old” />
    <add key=”NewName” value=”zip” />

    <add key=”sourceFolder” value=”E:\DATATXT” />
    <add key=”destFolder” value=”E:\DATATXT” />

  </appSettings>
</configuration>


 

6. เพิ่ม dll System.Configuration เข้าไปในโปรแกรม…

Add Reference

image

 

7.แล้วใส่โค้ดลงไปดังนี้

Coding


using System.IO;

using System.Configuration;

….

var _fileName = _textFileName.Split(‘,’).ToArray();

var directory = new DirectoryInfo(sourceFolder);

foreach (var item in _fileName)
           {

directory.GetFiles(“*.” + _OldName)
                  .ToList()
                  .Where(t => t.FullName.Contains(item)) //ดึงชื่อไฟล์ตามที่ระบุในไฟล์ config
                  .ToList()
                  .ForEach(f => f.MoveTo(Path.Combine(destFolder, f.Name.Replace(_OldName, _NewName)))); // เปลี่ยนชื่อไฟล์

}

#region Recursive file search

           //วนหาไฟล์ในโฟล์เดอร์ย่อย

            string[] folders = Directory.GetDirectories(sourceFolder);

            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);

                RENAME(folder, dest);
            }

#endregion


8. รันโปรแกรม

image

 

9. ผลการเปลี่ยนชื่อไฟล์ จาก *.old ไปเป็น *.zip…
    เฉพาะ ชื่อไฟล์ 20110215 ตามที่เรากำหนดในไฟล์ config

image

 

10. การใช้เมธอด MoveTo()…
       ถ้ามีชื่อไฟล์ดังกล่าวอยู่แล้ว จะเกิดข้อผิดพลาด ไม่สามารถเปลี่ยนชื่อไฟล์ได้ ดังนี้…

image

 

11. ฉะนั้นก่อนเปลี่ยนชื่อไฟล์ เราทำการตรวจสอบการมีอยู่ของไฟล์ก่อน…
       ดังนี้

Coding … checking File.Exists


directory.GetFiles(“*.” + _OldName)
                  .ToList()
                  .Where(t => t.FullName.Contains(item))
                  .ToList()
                    //.ForEach(f => f.MoveTo(Path.Combine(destFolder, f.Name.Replace(_OldName, _NewName))));
                  .ForEach(f =>
                   {
                        if (File.Exists(Path.Combine(destFolder, f.Name.Replace(_OldName, _NewName)))) //ถ้ามีไฟล์อยู่แล้ว
                            File.Delete(Path.Combine(destFolder, f.Name.Replace(_OldName, _NewName))); //ลบมันทิ้ง
                        f.MoveTo(Path.Combine(destFolder, f.Name.Replace(_OldName, _NewName))); //ทำการเปลี่ยนชื่อไฟล์
                        Console.WriteLine(“RENAME ” + f.Name);
                   });


 

12.  ลองเปลี่ยน config เปลี่ยนชื่อไฟล์ 20110215,16,17 ให้ทำการระบุพารามิเตอร์ดังนี้:

image

 

13. รันโปรแกรมใหม่…

image

 

14. ชื่อ text file จะเปลี่ยนตามที่ต้องการ…

image

 

 

จะเห็นว่าโปรแกรมง่ายๆ…
แต่ช่วยประหยัดของเรา…
จากที่อาจจะต้องใช้เวลาหลายวันในการเปลี่ยนไฟล์ ทีละไฟล์
เพียงแต่…รันโปรแกรมครั้งเดียว…อาจใช้เวลาไม่ถึง 20 นาที…
ทั้งขึ้นอยู่กับว่าไฟล์เก่า (*.old) ในแต่ละสาขามีมากแค่ใหน…

 

 

แหล่งข้อมูลดาวน์โหลด: http://bit.ly/nxKPXF 

 

https://skydrive.live.com/embedicon.aspx/SourcesCode/2011/2011-10-21_RenameFileWithLINQ.zip?cid=7d608959d854cb28&sc=documents

 


ผู้เขียน

clip_image019

** รู้จักกับผู้สอน อ.นุชิต **
JANAWAT Blog: https://janawat.wordpress.com
Nuchit’s Profile @microsoft
https://mvp.support.microsoft.com/profile/nuchit
Facebook:
https://www.facebook.com/nuchit
Twitter:
http://twitter.com/janawat

เกี่ยวกับ

Seasoned Senior System Analyst & Developer with over a decade of experience in designing, analyzing, and developing highperformance, scalable applications using a wide range of technologies, including Java, Spring Boot, C#, ASP.NET, MVC, React.js, Kubernetes, Microservices, PostgreSQL, MySQL, Oracle, and Microsoft SQL. Notable Achievements: Consistently delivered projects on time and within budget, ensuring client satisfaction and project success. Demonstrated versatility in working both independently and as a valuable team member, contributing to collaborative achievements. Honored with the prestigious "Employee of the Year" award in 2012 for exceptional dedication and outstanding performance. Recognized as a Microsoft Most Valuable Professional (MVP) for significant contributions to the software development community. My Commitment: I am committed to innovation, excellence, and solving complex technical challenges. I take pride in my ability to consistently deliver robust and efficient software solutions that meet and exceed the expectations of clients and stakeholders.

Tagged with:
เขียนใน Windows Application
2 comments on “Rename File With LINQ
  1. Randal Waye พูดว่า:

    Hi. Magnificent job. I did not expect this on a friday night. This is a terrific story. Thanks!

  2. Irving Kapusniak พูดว่า:

    Thanks for taking the time to discuss this. Thanks for all the enthusiasm to extend such helpful information on this post. I love to comment on your blog sir.

ส่งความเห็นที่ Randal Waye ยกเลิกการตอบ

In Archive