2

冷知識 - 刪不掉的 Windows 檔案

 3 years ago
source link: https://blog.darkthread.net/blog/filename-endswith-dot/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

冷知識 - 刪不掉的 Windows 檔案

2021-04-13 09:38 PM 2

同事問了一個鬼問題 - "Windows 檔名可不可以只有一個空白?"

答案是不行。測試過程我搞出一個怎麼都砍不掉的檔案,花了點時間才清掉,學到冷知識。

依據微軟文件,檔案或目錄名稱不能包含以下字元:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (forward slash)
  • \ (backslash)
  • | (vertical bar or pipe)
  • ? (question mark)
  • * (asterisk)

所以空白是合法的字元,像是 "Hello World.txt" 完全沒問題,但文章有提到不要用空白或點做結尾,對檔案系統雖然合法,但 Windows Shell 或檔案總管無法支援,所以如果你想惡搞同事,可以這麼做。在他的資料夾生出三個名為" . "、" .. "、"來刪我呀,笨蛋!. "的空檔:

這三個檔案對檔案總管來說是隱形的,看得到刪不掉:

" . " 跟 " .. " 更玄,會出現被其他應用程式開啟無法刪除的訊息:

學駭客用 DOS 下指令,總能成功吧?不行!

啃,難道要逼我格式化磁碟才能刪掉它們?前面提的微軟文件有答案,有個特殊字首 - "\\?\",加在路徑前方會跳過 Windows Shell 或檔案總管的路徑字串解析或檢核,直接送交 Windows API 執行,可解決路徑超過 MAX_PATH 長度限制或前面所說"."結尾檔名等 GUI 無法處理的特殊狀況。所以,加上"\\?\"就能順利解決這些垃圾囉~

又學到一些冷知識。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK