@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
The %%a
loop assigns each .cpp
filename in turn to %%a
.
The %%c
loop reads each line from the file and prefixes the line with a line number in square brackets using the find
utility (note : Microsoft's cmd
utility find
) the resultant line is then tokenised, [number
going to %%c
and remainder of line after ]
to %%d
.
The %%t
loop finds the first token on the line (if it exists) then regurgitates the line and adds a blank line if the first token was (any of the strings in the list processed by %%m
) - I added /i
to the if
to make the match case-insensitive.
As a check, I'd use
fc /w "sourcedirectoryname*.cpp" "destinationdirectoryname*.cpp"
which should execute a file-compare between the original and processed files, disregarding whitespace.
The source directory and destination directory must be different, otherwise batch will attempt to overwrite the very file it's reading. No good will come of that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…