I have used the below code to merge .cpp files in a directory.This code was a result of question asked in super user but there are no replies since my question was edited.So please don't mark this question as duplicate .
@echo off
cd C:\test
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >merged.html
for /r %%f in (*.cpp) do (
echo ^<pre^>
echo File Name : //Here i want the file name of each program without .cpp extension
echo Description :
echo Author : Name
echo Date : //Here i want the date associated with each file DD-MM-YYYY
echo.
type "%%f"
echo ^</pre^>
) >> merged.html
echo ^</body^>^</html^> >> merged.html
Current Merged.html file
File name :
Desciption :
Author : Name
Date :
#include
int main()
{
.....
.....
.....
}
Then the second file on the next page .
Issue with the above code
It is omitting the header file names i.e
In the orignal file i have #include but in the merged file i'm only having #include.I guess the problem is due to the "<" and ">" .
What i need
I need the date associated with each program file in the merged file after Date : (See my code above, Read the commented part) .Also i need the file name of each program after File name : like abc(without the extension .cpp in the file).
@ECHO Off
SETLOCAL
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >u:\newfile.txt
for /r %%f in (*.cpp) do (
echo ^<pre^>
echo File Name : %%~nf
echo Description :
echo Author : Name
FOR /f %%d IN ("%%~tf") DO echo Date : %%d
echo.
type "%%f"
echo ^</pre^>
)>>u:\newfile.txt
echo ^</body^>^</html^> >> u:\newfile.txt
GOTO :EOF
Produces u:\newfile.txt. I removed your cd
to your source directory to suit my testing.
The filename is relatively simple, just %%~nf
- see the documentation on for
from the prompt
for /?
You don't tell us what format you use for your date, so manipulating that is a topic in itself (that has been covered many times over on SO). The format I use is dd/mm/yy hh:mm:ss
so I can use the default tokens=1delims=
. If as seems likely, you are using DDDday dd-mm-yy hh:mm:ss
then you'd need to use FOR /f "tokens=2" %%d IN...
I could find no problem with redirectors in the .cpp
files (but please don't assume that people have samples of or familiarity with them.)
Now - it's going to be a real pain maintaining this as you'd need to re-edit the file over and over again to insert the names and descriptions.
Try this:
@ECHO Off
SETLOCAL
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >u:\newfile.txt
for /r %%f in (*.cpp) do (
echo ^<pre^>
echo File Name : %%~nf
SET "namefound="
FOR /f "tokens=1,2,3delims=|" %%p IN (q29056600.txt) DO IF /i "%%p"=="%%~nxf" (
echo Description : %%q
echo Author : %%r
SET namefound=Y
)
IF NOT DEFINED namefound (
echo Description :
echo Author : Name
)
FOR /f %%d IN ("%%~tf") DO echo Date : %%d
echo.
type "%%f"
echo ^</pre^>
)>>u:\newfile.txt
echo ^</body^>^</html^> >> u:\newfile.txt
GOTO :EOF
Where the file q29056600.txt
contains
hello.cpp|Hello World version 1|Fred Nurk
helloworld.cpp|Hello World version Two|Joe Bloggs
That is, filename description autor separated by pipes. The extra code
namefound
Here's the above with a constant author
line. You'd simply need to omit the author column from the support file.
@ECHO Off
SETLOCAL
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >u:\newfile.txt
for /r %%f in (*.cpp) do (
echo ^<pre^>
echo File Name : %%~nf
SET "namefound="
FOR /f "tokens=1,2delims=|" %%p IN (q29056600.txt) DO IF /i "%%p"=="%%~nxf" (
echo Description : %%q
SET namefound=Y
)
IF NOT DEFINED namefound (
echo Description :
)
echo Author : Name
FOR /f %%d IN ("%%~tf") DO echo Date : %%d
echo.
type "%%f"
echo ^</pre^>
)>>u:\newfile.txt
echo ^</body^>^</html^> >> u:\newfile.txt
GOTO :EOF
As for the problem with missing text: I suspect you aren't looking at the file with a text editor like notepad
but with some variety of HTML viewer, so the missing data is being interpreted as HTML.
If I am correct about the <>
problem, then the following may be a fix:
@ECHO Off
SETLOCAL
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >u:\newfile.txt
for /r %%f in (*.cpp) do (
echo ^<pre^>
echo File Name : %%~nf
SET "namefound="
FOR /f "tokens=1,2delims=|" %%p IN (q29056600.txt) DO IF /i "%%p"=="%%~nxf" (
echo Description : %%q
SET namefound=Y
)
IF NOT DEFINED namefound (
echo Description :
)
echo Author : Name
FOR /f %%d IN ("%%~tf") DO echo Date : %%d
echo.
TYPE "%%f"|sed "s/\x3c/\<\x3b/g;s/\x3e/\>\x3b/g;s/\x25/\%\x3b/g"
echo ^</pre^>
)>>u:\newfile.txt
echo ^</body^>^</html^> >> u:\newfile.txt
GOTO :EOF
Note that this includes sed
- a free utility. I used GNUSED.