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. 总结

第一次在博客园发布文章,由于博主能力有限,文中可能存在描述不正确,欢迎指正、补充!感谢您的阅读。如果文章对您有用,那么请轻轻点个赞,以资鼓励。

posted @ 2020-04-17 17:03  Daphel  阅读(4856)  评论(0编辑  收藏  举报