我有这样的文件夹结构:
----src/
----foo.cpp
----bar.cpp
foo.cpp
因此是:
#pragma once
#include "foo.h"
void FOO::func1(){
printf("Foo Func1\n");
}
void func2(){
printf("Func2\n");
}
我想重写此文件,在函数实现之间以及前后一个空行之间。也就是说,在任何函数实现中以及函数实现之外,我都希望删除所有空行。我想foo.cpp
这样改变:
#pragma once
#include "foo.h"
void FOO::func1(){
printf("Foo Func1\n");
}
void func2(){
printf("Func2\n");
}
bar.cpp
同样。在这种情况下,是否可以编写和运行脚本/批处理(Linux或Windows都可以)在给定文件夹/src/
中的所有文件上执行此操作。
其他详细信息:我当前的操作方式是打开vim
,使用{
和}
在vim
段落之间导航的每个文件,使我进入下一个空行,并dd
删除该行(如果要删除的话)。
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.cpp" '
) DO >"%destdir%\%%a" (
FOR /f "tokens=1*delims=]" %%c IN ('find /n /v "" ^<"%sourcedir%\%%a"') DO (
FOR /f %%t IN ("%%d") DO (
ECHO %%d
FOR %%m IN (} #include) DO IF /i "%%t"=="%%m" echo/
)
)
)
GOTO :EOF
该%%a
循环给每个.cpp
依次文件名%%a
。
该%%c
循环从文件中读取每一行和前缀与使用方括号行号的行find
实用程序(注:微软的cmd
工具find
)得到的线,然后标记化,[number
去%%c
和remainder of line after ]
到%%d
。
该%%t
环发现上线的第一个标记(如果存在的话),然后反刍线,并增加了一个空行,如果第一令牌(任何由处理列表中的字符串%%m
) -我添加/i
到if
进行匹配病例不敏感。
作为支票,我会用
fc /w "sourcedirectoryname\*.cpp" "destinationdirectoryname\*.cpp"
无论空白,它都应在原始文件和已处理文件之间执行文件比较。
源目录和目标目录必须不同,否则批处理将尝试覆盖正在读取的文件。没有好处。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句