batch_add_copyright_to_fileheader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //https://msdn.microsoft.com/en-us/library/aew9yb99(v=vs.85).aspx
  2. var debug=0;
  3. var fso = new ActiveXObject("Scripting.FileSystemObject");
  4. var fLog;
  5. if(debug==1)
  6. fLog = fso.createtextfile("log.txt",true);
  7. var filters=['cxx','c','cpp','h'];
  8. var comment=
  9. "/****************************************************************\r\n"+
  10. "This is a simple tools for add block comments to c/cpp file's header \r\n"+
  11. "Copy this js file to special directory which you want to add header comments.\r\n"+
  12. "double click and run it, It will recursive to add head copyright info to your c/cpp files \r\n\r\n\r\n"+
  13. "It's a part of StrollingWolf Comments Remove tools. \r\n"+
  14. "If you have any advice, please email to me: yaolixing01@gmail.com\r\n"+
  15. "Enjoy it!\r\n"+
  16. " ------2019 \r\n"+
  17. "*****************************************************************/\r\n";
  18. var ForReading = 1, ForWriting = 2, ForAppending = 8;
  19. var asDefault = -2, asUnicode = -1, asASCII = 0;
  20. function doWithFile(fl)
  21. {
  22. fl = fl + '';
  23. //search file's header, If have exist block comments, then remove it, otherwise add our copyright header.
  24. var s='';
  25. var ext="", extPos = fl.lastIndexOf('.');
  26. if (extPos!=-1)
  27. {
  28. ext = fl.substr(extPos+1).toLowerCase();
  29. }
  30. //file's extend name filter
  31. var i=0;
  32. for( i=0;i<filters.length; i++)
  33. {
  34. if (filters[i] == ext)
  35. {
  36. break;
  37. }
  38. }
  39. if (i==filters.length && filters.length !=0)
  40. {
  41. return;
  42. }
  43. //ForReading 1 , file is readonly.
  44. //ForWriting 2, means file is write only.
  45. //ForAppending 8 , means open file and appent from file end.
  46. var f = fso.OpenTextFile(fl, ForReading , false, asDefault);
  47. if (!f)
  48. {
  49. return;
  50. }
  51. //judge whether a document begins with a comment
  52. var bStartWithCommentLine = 0, bStartWithCommentBlock=0;
  53. var sline='';
  54. //use state machine. skip all header comments.
  55. var sFirstLineSrc ="";
  56. var nState = 0;
  57. var nLineNum=0;
  58. while (!f.AtEndOfStream)
  59. {
  60. sline = f.ReadLine();
  61. nLineNum++;
  62. if (/\S+/gi.test(sline))
  63. {
  64. if ( (nState&2)==0 && /^\s*\/\//gi.test(sline))
  65. {
  66. bStartWithCommentLine = 1;
  67. nState |= 1;//it's line comment.
  68. if(debug==1){
  69. fLog.WriteLine('line comment:'+sline +' state:'+nState + " line number:"+nLineNum);
  70. }
  71. }else if (/^\s*\/\*/gi.test(sline))
  72. {
  73. bStartWithCommentBlock = 1;
  74. nState |= 2;//block comment start
  75. if(debug==1){
  76. fLog.WriteLine('block comment start:'+sline +' state:'+nState+ " line number:"+nLineNum);
  77. }
  78. if(/\*\//gi.test(sline)){
  79. //single line comment
  80. nState &= ~2;//remove block comment state
  81. if(debug==1){
  82. fLog.WriteLine('block comment end:'+sline+' state:'+nState + "line number:"+nLineNum);
  83. }
  84. }
  85. }else if( (nState&2)!=0 && /\*\//gi.test(sline))
  86. {
  87. nState |= 4;//block comment end
  88. nState &= ~2;//remove block comment state
  89. if(debug==1){
  90. fLog.WriteLine('block comment end:'+sline+' state:'+nState + " line number:"+nLineNum);
  91. }
  92. }else if ((nState&2)==0 &&/\S+/gi.test(sline))
  93. {
  94. sFirstLineSrc = sline;
  95. nState=0;
  96. }
  97. if (sFirstLineSrc.length>0)
  98. {
  99. break;
  100. }
  101. }
  102. }
  103. if(debug==1)
  104. {
  105. if ( bStartWithCommentLine==1)
  106. {
  107. fLog.WriteLine(sline);
  108. }else if (bStartWithCommentBlock==1)
  109. {
  110. fLog.WriteLine(sline);
  111. }
  112. }
  113. //write data to another temp new file
  114. var fl2 = fl+'._slfsfsafsfxx0_';
  115. var ftmp = fso.OpenTextFile(fl2, 2, true);
  116. ftmp.WriteLine(comment);
  117. if (sFirstLineSrc.length>0)
  118. {
  119. ftmp.WriteLine(sFirstLineSrc);
  120. }
  121. //copy left contents of origin file.
  122. while (!f.AtEndOfStream)
  123. {
  124. sline = f.ReadLine();
  125. ftmp.WriteLine(sline);
  126. }
  127. ftmp.Close();
  128. f.Close();
  129. //del origin file
  130. fso.DeleteFile(fl, true);
  131. //rename temp file to origin file.
  132. fso.MoveFile(fl2, fl);
  133. }
  134. var WSShell = WScript.CreateObject("WScript.Shell");
  135. function getPathFiles(spath)
  136. {
  137. var fd = fso.GetFolder(spath);
  138. var fc = new Enumerator(fd.files);
  139. var folderName = fso.GetBaseName(spath);
  140. if(folderName.indexOf('__')==0)
  141. {
  142. //we don't dowith files which is '__' as filename's prefix
  143. if(debug==1){
  144. fLog.WriteLine(spath+', '+folderName);
  145. }
  146. return;
  147. }
  148. for (; !fc.atEnd(); fc.moveNext())
  149. {
  150. doWithFile(fc.item());
  151. }
  152. fc=null;
  153. var childPaths = new Enumerator(fd.SubFolders);
  154. var subfolder;
  155. for (; !childPaths.atEnd(); childPaths.moveNext())
  156. {
  157. subfolder = childPaths.item();
  158. getPathFiles(subfolder);
  159. }
  160. childPaths=null;
  161. }
  162. getPathFiles('.');
  163. if(debug==1)
  164. fLog.Close();
  165. fso=null;