1. 程式人生 > >windows cmd 開啟相對路徑下的網頁檔案

windows cmd 開啟相對路徑下的網頁檔案

需求

給專案用 Sphix 做了文件,但是最終編譯出來的文件入口 index.html 需要進入很多層資料夾後才能找到,如果建立快捷方式,只能在自己的電腦上有用,到其他人電腦上相應的路徑就不對了,而 Windows 目前並不能在快捷方式中寫相對路徑。

所以想到用命令列來開啟相應的檔案以方便開啟文件進行閱讀。

環境

  • Microsoft Windows 10 企業版 10.0.14393 版本 14393

實現

將以下程式碼儲存為一個 run.bat 檔案

chcp 65001
start "" ".\客戶端設計\_build\html\index.html"

也就是使用 start "" "目標檔案路徑"的形式。注意目標檔案路徑要寫成與 run.bat 相對的路徑。

這樣雙擊 run.bat 就可以達到實際雙擊 index.html 這個檔案的效果了。而且支援路徑中有中文。

參考資料

  1. windows 控制檯cmd亂碼的解決辦法
  2. Relative shortcuts for Windows
  3. Using relative paths for Windows shortcuts
  4. Echo UTF-8 characters in windows batch
  5. How to open an HTML file from a batch file in default browser with a specific destination anchor?
  6. How to easily open files and URLs from the command line
  7. Making a Windows shortcut start relative to where the folder is?