Search code examples
batch-filecmddosbeyondcompare

How to generate reports for every 100 files using Batch Scipt


Here's the scenario, for example I have list of files listed in pairing.txt. Now I am doing comparison for all the files listed then generating reports. Now, I want to have a feature that will enable the user how many files he want for one report. For example, I have 205 files listed.

FIRST 100 -    Report1.html
NEXT 100 -     Report2.html
Remaining 5 -  Report3.html

Here my actual code

for /f "tokens=2-4" %%a in ('type c:\user\pairing.txt') do (
  set /A Counter+=1
  echo Processing  ColumnA : %%a  ColumnB: %%b 
  echo  Processing  ColumnA : %%a  ColumnB: %%b >>comparison.log
  %varBCpath% %varBCscript% c:\user\ColumnA\%%a c:\user\ColumnB\%%b "c:\comp\report.html" "%title%" /silent
  type  c:\user\report.html >> c:\user\report\Report.html
)

What it does is, It will get the files listed in pairing.txt and compare it using beyond compare. Now by default, all comparison will be displayed in 1 html report. What if I want the user to have the option to enter how many files he want to display in every html report?


Solution

  • Supposing correctness of the code provided in question, an user input expected in set /P command and tested whether numeric and positive; output file numbering is done in the :myType subroutine as follows:

    :: some code here
    
    :againhowmany
    set /A "howmany=100"
    set /P "howmany=how many files do you want for one report (Enter=%howmany%) "
    set /A "howmany%%=100000"
    if %howmany% LEQ 0 goto :againhowmany 
    
    set /A "Counter=0"
    set /A "ReportNo=1"
    
    for /f "tokens=2-4" %%a in ('type c:\user\pairing.txt') do (
      set /A "Counter+=1"
      echo Processing  ColumnA : %%a  ColumnB: %%b 
      echo  Processing  ColumnA : %%a  ColumnB: %%b >>comparison.log
      %varBCpath% %varBCscript% c:\user\ColumnA\%%a c:\user\ColumnB\%%b "c:\comp\report.html" "%title%" /silent
      call :myType
    )
    
    :: some code here 
    goto :eof
    
    :myType
      type  c:\user\report.html >> c:\user\report\Report%ReportNo%.html
      set /A "Counter%%=%howmany%"
      if %Counter% EQU 0 set /A "ReportNo+=1" 
    goto :eof
    

    Final output files should be

    c:\user\report\Report1.html
    c:\user\report\Report2.html
    c:\user\report\Report3.html
    ...
    

    Edit: according to OP's comment: For example I have 12 list of files, then I entered 10 files for each report. How about the remaining 2 files?

    Under that clause final output files should be

    Report1.html   - files (pair)  1..10
    Report2.html   - files (pair) 11..12
    

    Note the only script changes:

    • CountNo variable renamed to ReportNo for better insight;
    • ReportNo initiated to 1 (to observe instructions in question).

    Edit 2: I had forgotten set /a command provides (32 bit signed) integer arithmetic, so the :myType procedure could be simplified as follows:

    :myType
      set /A "ReportNo=(%Counter%-1)/%howmany%+1"
      type  c:\user\report.html >> c:\user\report\Report%ReportNo%.html
    goto :eof