Keil工具_更改文件只讀屬性


1. 前言

Keil給用戶留了一個插入自定義組件的選項。通過插件用戶可以使用外部的編輯器(如Notepad++、VScode等),也可以添加其他自定義功能。可以參考Keil的幾個插件這篇文章。

為了避免鍵盤誤操作破壞代碼,我習慣於鎖定(只讀)已完成的源文件。但每次都要在資源管理器修改屬性很麻煩。本文介紹的這個批處理可以在Keil軟件內輕松自由切換源文件的只讀狀態。(網上查了許久都找不到,只能自己寫了😆)

插件使用批處理,理論上不限定軟件版本。IAR沒有測試,如果參數一致也是可行的。

2. 效果

先來看看效果

3. 快速開始

3.1 代碼

復制代碼到一個新建的txt文件中,更改后綴名為.bat,放到任意文件夾下。

@echo off
attrib -A %1 /s
for /f %%a in ('attrib %1') do (if "%%a"=="R" (attrib -R %2 /s && echo Unlocked) else (attrib +R %2 /s && echo Locked))
exit
3.2 設置

打開Keil的tools->customize tools menu菜單可以添加用戶自定義的菜單選項。如下圖所示:

Menu Content:鎖定當前文件
Command:C:\Software\Keil_V5\Tools\更改只讀屬性\lock.bat
Arguments:!E !E
Menu Content:鎖定所有文件
Command:C:\Software\Keil_V5\Tools\更改只讀屬性\lock.bat
Arguments:%P
  • 記得勾選Run Minimized項,這樣就不會有批處理的黑窗彈出。

4. 相關說明

4.1 批處理相關知識
4.1.1 顯示或更改文件屬性。
ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [[drive:] [path] filename] [/S [/D]]

+ 設置屬性。

- 清除屬性。

R 只讀文件屬性。

A 存檔文件屬性。

S 系統文件屬性。

H 隱藏文專件屬性。

[drive:][path][filename]指定要處理的文件屬性。

/S 處理當前文件夾及其子文件夾中的匹配文件。

/D 也處理文件夾

4.1.2 給批處理腳本傳遞參數

批處理文件中可引用的參數為%0-%9%0是指批處理文件的本身,也可以說是一個外部命令;%1-%9是批處理參數,也稱形參。我們來新建一個文件,文件命名為test.bat,文件內容如下:

@echo off
echo param[0] = %0
echo param[1] = %1
echo param[2] = %2
echo param[3] = %3
echo param[4] = %4
echo param[5] = %5
echo ...
pause

在控制台運行,並且處傳入參數,運行結果如下:

> test.bat 1 2 3 4
param[0] = test.bat
param[1] = 1
param[2] = 2
param[3] = 3
param[4] = 4
param[5] =
...
請按任意鍵繼續. . .
  • pause>nulpause

    pause就是暫停命令,執行時會在命令行窗口顯示請按任意鍵繼續. . .並等待你按鍵。pause > nul的作用是同樣的,區別是不顯示請按任意鍵繼續. . .這些字,nul相當於空文件,把這些文字隱去了。同樣是等待你按鍵。

    >是重定向符(將輸出的內容重定向到其它的設備),例如echo hello >c:\1.txthello 重定向到c:\1.txt,如果c:\1.txt不存在則新建。nul 是空設備。

  • echo off關閉回顯;@echo off加了@就是連本身也不顯示。

4.1.3 Bat批處理之for詳解
FOR /F ["options"] %variable IN ('command1') DO command2 [command-parameters]
  1. forindo是for語句的關鍵字,它們三個缺一不可;
  2. in之后,do之前的括號不能省略;
  3. 關於%variable要注意:%a%%a 是for循環時用的。%a 是在CMD直接執行時用的,%%a是在BAT腳本中用的。%a%是普通變量。
  4. options可以為:delims=XX(指分隔符集);tokens=x,y,m-n(指每行的哪一個符號被傳遞到每個迭代的 for 本身);skip=n(指在文件開始時忽略的行數)。
  5. 參數/f的效果:for會先讀出command1執行后得到的所有結果,並把它作為一個集合,並且以每一行作為一個元素傳入參數%variable
4.1.4 批處理順序執行多條命令
命令1 & 命令2 & 命令3 ... (無論前面命令是否故障,照樣執行后面)   
命令1 && 命令2 && 命令3....(僅當前面命令成功時,才執行后面)   
命令1 || 命令2 || 命令3.... (僅當前面命令失敗時,才執行后面)
4.1.5 Bat批處理之if詳解
  1. 判斷字符串是否相等的時候,if會區分大小寫。若不想區分大小寫,則需要if /i "字符串1"=="字符串2" 的格式。

  2. 判斷兩個數值之間的大小關系時,由於在批處理中><為重定向符號,這就意味着我們不能用>來表示大於,<表示小於。

EQU - 等於
NEQ - 不等於
LSS - 小於
LEQ - 小於或等於
GTR - 大於
GEQ - 大於或等於
4.2 customize Tools menu

關於customize Tools menu對話框的詳細內容可以參考以下文檔引用:

  • Menu Content

    Text shown in the Tools menu. This line may contain Key Codes and File Codes. Shortcuts are defined with an ampersand ( & ) character. The current selected menu line allows you to specify the options listed below.

  • Prompt for Arguments

    If enabled, a dialog box opens at the time you invoke the menu item that allows you to specify the command line arguments for the user program.

  • Run Minimized

    Enable this option to execute the application with minimized window.

  • Run Independent

    If enabled, µVision will not wait for the completion of the external application. If disabled, µVision waits until the external application terminates. The output of the external application will be written in the Output Window — Build page. The output can be made clickable to jump to a specific location in a source code. The output format shall be same as described in Clickable Output section in User dialog.

  • Command

    Program file that is executed with the selected menu item. You can use environment variables as described in Key Sequences to pass path arguments.

  • Initial Folder

    Current working folder for the application program. If this entry is empty, µVision uses the base folder of the project file.

  • Arguments

    Command line arguments that are passed to the application program. You may use Key Sequences to pass arguments.

  • All Entries

    Export or Import Tools menu customization settings. Use this to share current settings across PCs.

4.3 Argument

關於Argument可以參考KEIL MDK 自定義工具時的命令行參數,本文用到的參數有:

符號 意義 例子
!E 帶有擴展名和相對路徑的文件名 .\main\main.c
%P 當前項目的文件名 PROJECT1.UVPROJX
  • 關於圖上Keil的其他插件,有問題歡迎咨詢。

5. 總結

第一次在博客園發布文章,由於博主能力有限,文中可能存在描述不正確,歡迎指正、補充!感謝您的閱讀。如果文章對您有用,那么請輕輕點個贊,以資鼓勵。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM