เปลี่ยนชื่อไฟล์ล่าสุดใน Download แล้วย้ายไปที่ specific folder ด้วย Power Automate Desktop

by

สมมุติว่าเราใช้ Power Automate Desktop เพื่อ download ไฟล์จาก Internet แล้วต้องการตั้งชื่อไฟล์ที่เพิ่ง download ให้มีวันที่และเวลาในชื่อไฟล์

Get files in folder

ใช้คำสั่ง Get files in folder แล้วตั้งค่าตามรูป เพื่อดึงไฟล์ทุกประเภทในโฟลเดอร์ Download แล้วเรียงตาม Creation time แบบ Descending

คำสั่งนี้จะได้ output เป็น list ของไฟล์ เก็บอยู่ในตัวแปรชื่อ Files โดยที่ไฟล์ล่าสุดถูกเก็บอยู่ใน index 0

Get files in folder
Folder เลือก Folder ที่ใช้เก็บไฟล์ที่ดาวน์โหลด, File filter ใส่ * จะดึงไฟล์มาทุกประเภท
Get files in folder
ที่ Advanced เลือก Sort by Creation time และเปิด toggle descending

Display message

ใช้คำสั่ง Display message เพื่อเช็คค่า Index 0 ในตัวแปร Files

Display message
ใช้เพื่อตรวจสอบค่าที่ได้ในตัวแปร Files ตอนใช้งานจริงสามารถกด disable action ได้

Get current date and time

ลากคำสั่ง Get current date and time มาวางแล้ว Save จะได้วันที่และเวลาเก็บในตัวแปร CurrentDateTime

Get current date and time
ที่ Time zone ถ้าใช้ System time zone จะได้เวลาตาม time zone ที่ตั้งใน Control Panel

Convert datetime to text

ลากคำสั่ง Convert datetime to text มาวางแล้ว ตั้งค่า parameter ตามด้านล่าง

  • Datetime to convert: ตัวแปร CurrentDateTime ที่ได้จากคำสั่ง Get current date and time
  • Format to use: Custom
  • Custom Format: ใส่รูปแบบวันที่เวลาที่ต้องการ เช่น yyyy-MM-dd-hhmmsstt เมื่อเราพิมพ์ format จะมีตัวอย่างแสดงขึ้นมาที่ Sample (สามารถใช้ Format แบบอื่นได้ตามต้องการแต่ต้องไม่มี Illegal file name character เช่น \ / : * ? ” < > |)

คำสั่งนี้จะได้ output เป็นวันที่และเวลาที่ถูกแปลงเป็นข้อความเก็บในตัวแปร FormattedDateTime

Convert datetime to text
Custom format สามารถปรับได้ตามรูปแบบที่ต้องการ แต่ต้องไม่มี Illegal filename character

Rename file(s)

ลากคำสั่ง Rename file(s) มาวางแล้วตั้งค่า parameter ตามด้านล่าง เพื่อ rename ไฟล์ที่เพิ่งสร้างล่าสุด โดยการเติมสัญลักษณ์ _ ตามด้วยวันที่และเวลา ต่อท้ายชื่อเดิมของไฟล์นั้น

ชื่อไฟล์ที่เปลี่ยนจะถูกเก็บในตัวแปร list ชื่อ RenamedFiles

Rename file(s)
สามารถเลือก Rename scheme เป็นรูปแบบอื่นๆ ได้

Display message

ใช้คำสั่ง Display message เพื่อเช็คค่า Index 0 ในตัวแปร RenamedFiles และเลือก properties Name โดยพิมพ์ %RenamedFiles[0].Name% ที่ Message to display (ถ้าใช้ properties Name จะแสดงเฉพาะชื่อไฟล์ ถ้าใช้ properties Fullname จะแสดง File path)

Display message
ตั้งค่าตามรูปเพื่อเช็คชื่อไฟล์ที่เปลี่ยน

Move file(s)

ย้ายไฟล์ที่ renamed ไปที่โฟลเดอร์ที่ต้องการด้วยคำสั่ง Move file(s) และเลือก properties Fullname โดยพิมพ์ %RenamedFiles[0].Fullname% ที่ File(s) to move

Move file(s)
Destination folder ใส่เป็น Folder อื่นที่ต้องการได้

Completed Flow

Completed Flow

Check Result

ไปที่ Dowload folder แล้วสร้างไฟล์ใหม่ขึ้นมา ตั้งชื่อว่า TestRename.txt

ไฟล์ล่าสุดใน Download Folder

ไปที่ Power Automate Desktop กด Save และกด Run ที่ Display message บรรทัด 2 จะได้ C:\Users\User\Downloads\TestRename.txt

ผลลัพธ์จากการใช้ Get files in folder แบบ Sort Descending

ที่ Display message บรรทัด 6 จะได้ Name ของไฟล์ที่เปลี่ยนชื่อ คือ TestRename_2024-03-23-105851AM.txt

ผลลัพธ์จากการ Rename file แสดงด้วย Name properties

ไปที่ Destination folder (ในที่นี่คือ Desktop) จะพบไฟล์ที่สั่งให้โรบอท Renamed

ผลลัพธ์จากการใช้ Move file(s)

Edited Flow for Ready to Use

สามารถ copy code นี้ไปลองใช้ได้ที่ github

คำอธิบายก่อนจะเป็นโฟลว์ใน github แก้ไขเพิ่มเติมดังนี้

  • บรรทัด 1 ใช้คำสั่ง Get Special folder เพื่อหา Desktop และชื่อคอมพิวเตอร์
  • บรรทัด 2 แทนที่คำว่า Desktop ด้วย Downloads โดยใช้คำสั่ง Replace text เพราะในคำสั่ง Get special folder ไม่มี Downloads ให้เลือก
  • บรรทัด 3 ใช้ตัวแปรแทนการพิมพ์ Downloads Path ตรงๆ เพราะคอมพิวเตอร์แต่ละคนใช้ชื่อไม่เหมือนกัน
  • บรรทัด 4 ให้ใส่ Destination Path ที่ต้องการย้ายไฟล์ลงไป
  • บรรทีด 5 และ 9 สามารถลบทิ้งได้
Edited Flow before delete Display message action
Edited Flow after delete Display message action
ใส่ Destination Path ลงไป กด Save กด Run

Did You Enjoy this Article?

Subscription to get new articles sent to your inbox each week for FREE

Question?

If you have any questions or feedback about this article please leave a message in the comments section below.

Subscribe to get new articles sent to your inbox each week for FREE