-2

In Groovy scripting how to read all .txt files in a given folder and create a single word document by copying the contents of .txt files.

Example Scenario -

In folder C:\Sample i have n-no of .txt files. I need to copy the data of .txt files and create one .doc file and copy paste the contents of all .txt files.

Please let me know how to accomplish this in Groovy scripting

Vinu
  • 33
  • 3

1 Answers1

0

After poking internet a bit, I could come up with the following script:

@Grab(group='org.apache.poi', module='poi-ooxml', version='3.7')
import org.apache.poi.xwpf.usermodel.XWPFDocument

XWPFDocument doc = new XWPFDocument()

new File( '/some/dir' ).eachFile{ File f ->
    if( f.directory ) return
    doc.createParagraph().createRun().text = "--------- $f.name ---------"
    doc.createParagraph().createRun().text = f.text
}

new File( 'result.docx' ).withOutputStream{ doc.write it }

the resulting docx-file is of the following "structure":

------------- error.gsp -------------
<!doctype html>
<html>
   <head>
....
------------- index.gsp -------------
<!doctype html>
<html>
injecteer
  • 20,038
  • 4
  • 45
  • 89