Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows

ลบไฟล์ Windows ที่มีอักขระแปลกๆ ไม่ได้ง่ายๆ ด้วยคำสั่ง del แบบ greedy search

ทุกไม่กี่ปี ผมเจอปัญหาเล็กน้อยนี้ พยายามลบไฟล์ใน Windows Explorer มัน Throw error บอกว่า Cannot find (และลบ) ไฟล์ที่ระบุ OK เกิดขึ้น ผมไป Command line ลบที่นั่น แต่คราวนี้เจอ Snag ใหม่ การลดปกติไม่ Complete Command prompt บอก: The system cannot find the file specified

ผมเจอ Issue นี้กับ Lock file ที่สร้างโดย Xerox B215 printer บน Samba share 由于一些奇怪的原因 Printer truncated actual filename แทนที่จะเป็น "Xerox_Scan.pdf" มันชื่อแค่ "Xerox_Scan." ฮา แต่จริงๆ แล้วมันเป็นอย่างนั้นไหม?

ปัญหา

บน Command line เราได้:

del "Xerox_Scan."
The system cannot find the file specified.

จุดนี้ ผม Realize ว่า Filename น่าจะมี Weird trailing character ที่ไม่ Visible ใน Windows Explorer หรือ Command prompt นี่เตือน Trick เล็กๆ ที่ผมและเพื่อนร่วมงานใช้ใน System administration exams สำหรับ Juniors เราสร้าง File ด้วย Special characters ท้าย เช่น Old Alt + 255 trick ใน DOS แล้วดูว่าใคร Figure it out ใน Linux ใช้ Octal dump utility (od) ดู Full set of characters ได้

Solution

ใน Windows Approach คือ Greedy search (หรือ Deletion เพื่อความแม่นยำ) หาก Wonder ว่าเกิดอะไรคิด Regular expressions เรา找 Any file (/s) ใน Subdirectories ใดๆ ใน Path ที่ระบุ และ找 File ที่มี Zero or more trailing whitespace characters Boom จบ ไม่มี Ugliness อีก

del /s \\?\D:\Scans\Xerox_Scan."
Deleted file - \\?\D:\Scans\Xerox_Scan.

สรุป

Technically speaking Undeletable file ไม่ควรเป็น Huge problem แต่เป็น Problem บน OCD level Cosmetically และ Theoretically อาจ Interfere กับ Backup scripts หรือ Scheduled tasks หากไม่ Designed to gracefully ignore files ประเภทนี้ ดังนั้นคุณต้องการ Light maintenance

มี Methods อื่นๆ มาก Process Explorer สามารถ Search files และ Delete ได้ รวมถึง Whitespace characters แต่ผมพบ Simple command line regex-search execution เป็น Most elegant solution อย่างไรก็ตาม Task ของเรา Annoying แต่มี Quick และ Painless solution See you around.

Cheers.