Remove C++ comments in file to allow to read number of lines of code (VBS)

Soldato
Joined
17 Jun 2012
Posts
11,259
Anyone got a best way of doing this.

Trying to count number of lines of code in a C++ file but need to remove comments, comments in C++ are either one line starting // or multi-line between /* and */.

The one liners are ok but bit stuck as best way to ignore the multi liners.

I have this so far.


Code:
sub openfile1(fop)
 Set objInputFile = objFSO.OpenTextFile(fop)
 do until objInputFile.atendofstream
        fname = objInputFile.readline
 
  if fname <> "" then
  if left(fname,2) <> "//" then
    c= c + 1
   end if
  end if
 loop
 
 fl.Write(fop & " " & c) & vbcrlf
end sub

So I need to add something like,

Code:
if left(fname,2) = "/*" then
Do until left(fname,2) = "*\"
objInputFile.skipline
loop
end if
 
Soldato
OP
Joined
17 Jun 2012
Posts
11,259
I just need the regex now. My regex is pretty poor, been trying this for about 2 hours. Any tips on what the regex is, for example I've found that,

Code:
//*(.?)*\/

removes all //

I thought that just,

/*(.?)*\ would work but no.
 
Soldato
OP
Joined
17 Jun 2012
Posts
11,259
Well the regex was,

Code:
/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/

Here's the finished article, but messy bit works. Given a starting dir this will find every c,cpp and h file within all subfolders also. It then open each and remove the /**/ comments, appends all this source code to one file and then counts the lines, also ignore // comments and whitespace.

Code:
Option Explicit
dim fso, objFSO,objStartFolder,fl,objFolder,colFiles,objFile,startposition,fileextension,Subfolder,fname,strReplacedText,objInputFile,fl1
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Documents and Settings\B0014596903\Desktop\TTimo-doom3.gpl-fb1609f"
Set fl = fso.OpenTextFile("c:\Documents and Settings\B0014596903\Desktop\output.txt", 8, True) 
Set fl1 = fso.OpenTextFile("c:\Documents and Settings\B0014596903\Desktop\output1.txt", 2, True)
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
 
'fl.Write(objFile.Name) & vbcrlf
 
 startposition = split(objFile.Name, ".")
 fileextension = ubound( startposition)
 
 select case startposition(fileextension)
 case "cpp"
 removecomments(Subfolder.Path & "\" & objFile.Name)
 case "h"
 removecomments(Subfolder.Path & "\" & objFile.Name)
 case "c"
 removecomments(Subfolder.Path & "\" & objFile.Name)
 end select
 
 
 
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
 For Each Subfolder in Folder.SubFolders
  Set objFolder = objFSO.GetFolder(Subfolder.Path)
  Set colFiles = objFolder.Files
  For Each objFile in colFiles
   startposition = split(objFile.Name, ".")
   fileextension = ubound( startposition)   
   select case startposition(fileextension)
   case "cpp"
   removecomments(Subfolder.Path & "\" & objFile.Name)
   case "h"
   removecomments(Subfolder.Path & "\" & objFile.Name)
   case "c"
   removecomments(Subfolder.Path & "\" & objFile.Name)
   end select      
  Next  
  ShowSubFolders Subfolder
 Next
End Sub
sub openfile_(fop)
 dim c
 Set objInputFile = objFSO.OpenTextFile(fop)
 do until objInputFile.atendofstream
  fname = objInputFile.readline  
  if fname <> "" then
   if left(fname,2) <> "//" then
    c= c + 1
   end if
  end if
 loop 
 fl1.Write(c)
 
end sub
sub removecomments(fop)
 
 dim objRegExp,colMatches,objMatch,objInputFile
 Set objInputFile = objFSO.OpenTextFile(fop)
 fname = objInputFile.readall
 Set objRegExp = New RegExp
 objRegExp.Global = True
 objRegExp.IgnoreCase = True
 objRegExp.Pattern = "/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/" 
 Set colMatches = objRegExp.Execute(fname) 
 For Each objMatch In colMatches
 
  strReplacedText = objRegExp.Replace(fname, "")
 Next
 
 fl.Write(strReplacedText) & vbcrlf 
 
end sub
openfile_("C:\Documents and Settings\b0014596903\Desktop\output.txt")
 
Last edited:
Back
Top Bottom