astyle是一个对源文件进行格式化的不错工具,它可以嵌入到编辑、编译工具中,也可能直接在命令行运行;可以针对单个文件,也可以针对目录。本文所描述的方法,是指在window下的命令行方式,通过鼠标拖动文件夹到批处理上进行格式化的简易方法,使用的版本是2.0.1。批处理内容如下:
@echo off
::批量将指定目录中的所有C文件用Astyle进行代码美化操作,支持鼠标拖动操作
::参考http://blog.chinaunix.net/uid-23381466-id-58917.html
::参考http://blog.csdn.net/windcao/article/details/12319225
::设置astyle.exe执行路径
set astyle=%~dp0\astyle.exe
::循环遍历目录查找指定文件进行处理
for /r %~dp1%~nx1 %%a in (.) do %astyle% %%a\*.cpp --style=linux -s4 -S -N -L -m0 -M40 --recursive --convert-tabs %f
for /r %~dp1%~nx1 %%a in (.) do %astyle% %%a\*.c --style=linux -s4 -S -N -L -m0 -M40 --recursive --convert-tabs %f
for /r %~dp1%~nx1 %%a in (.) do %astyle% %%a\*.hpp --style=linux -s4 -S -N -L -m0 -M40 --recursive --convert-tabs %f
for /r %~dp1%~nx1 %%a in (.) do %astyle% %%a\*.h --style=linux -s4 -S -N -L -m0 -M40 --recursive --convert-tabs %f
for /r %~dp1%~nx1 %%a in (.) do %astyle% %%a\*.s --style=linux -s4 -S -N -L -m0 -M40 --recursive --convert-tabs %f
::删除astyle生成的所有中间文件
for /r %~dp1%~nx1 %%a in (*.orig) do del %%a
pause
说明:
1、批处理存放位置同astyle.exe,这样在运行的时候可通过“%~dp0”定位到执行程序位置;
2、“%~dp1%~nx1”即拖动的文件夹对应的绝对路径,通过for循环遍历处于它下面的所有子目录