Search code examples
bashawksedlibreofficelibreoffice-writer

lowriter Bash Script to Convert all doc to pdf In-Place


So.. I've been tasked with converting a bunch of *.doc files to *.pdf utilizing lowriter

What I would like to do is do this in place, but since there is no option to do that using lowriter, I figured I would capture the originating file and path, capture the conversion, and then move the converted file to the originating path, and then delete the original *.doc

The problem is my sed and or awk is weak at best ;) so I cannot figure out how I can "capture" the converted file name from the output.

My Code:

#!/bin/bash

FILES=/my/path/**/*.doc

shopt -s globstar

for f in $FILES; do

    the_file=$f;
    the_orig_dir=$(dirname "$the_file") ;

    converted=$(lowriter --headless --convert-to pdf "$the_file");
    
    echo $converted;
done;

and the output is:

convert /my/path/Archives/Ally/Heavenly Shop.doc -> /my/Heavenly Shop.pdf using filter : writer_pdf_Export
convert /my/path/Archives/Ally2/Solutions Shop.doc -> /my/Solutions Shop.pdf using filter : writer_pdf_Export
convert /my/path/Archives/Ally3/Xpress Shop.doc -> /my/Xpress Shop.pdf using filter : writer_pdf_Export

What I need to do is capture the path/filename of the converted file after the -> and before the :. I just don't know how I can do this. Can someone tell me?


Solution

  • #!/bin/bash
    
    FILES=/my/specific/input/folder/**/*.doc
    
    shopt -s globstar
    
    for f in $FILES; do
    
        the_file=$f;
        the_orig_dir=$(dirname "$the_file") ;
    
        converted=$(lowriter --headless --convert-to pdf "$the_file");
        
        new_file=$(echo "$converted" | grep -o -P '(?<= -> ).*(?= using filter : )');
        
        new_file_name=$(basename "$new_file");
        
        
        echo "$the_orig_dir/$new_file_name";
        
        
        set -x;
        
        rm -f $the_file;
        
        mv "$new_file" "$the_orig_dir/";
        
        set +x;
        
    done;
    

    does what I need it to do