Home > Mobile >  Find files with no pdf extension where docx file exists. Run 'lowriter --headless --convert-to
Find files with no pdf extension where docx file exists. Run 'lowriter --headless --convert-to

Time:12-19

Just wondering if there is a simpler, shorter, better command than the below:

Files:

123.pdf  
123.docx  
456.docx   <= Run 'lowriter --headless --convert-to pdf' against this file 
789.docx
789.pdf
'another file.docx' <= Run 'lowriter --headless --convert-to pdf' against this file 

This command only works on files with no spaces:

ls *.docx | perl -nle '
  s/\.docx$/.pdf/; 
  print unless -e 
' | sed 's/\.pdf/\.docx/' | while read line ; do
  lowriter --headless --convert-to pdf $line ;
done

My filenames have no spaces so the command works for me. But maybe others do need it to work for filenames with spaces

CodePudding user response:

Summary from comments:

parallel '[ -f {.}.pdf ] || lowriter --headless --convert-to pdf {}' ::: *.docx

CodePudding user response:

command only works on files with no spaces

Can you try put quotes around the last $line? Like this .... --convert-to pdf "$line" ?

As an alternative you could do everything in Perl like this:

for my $file (<*.docx>) {
    my $basename = $file =~ s/\.docx$//r;
    my $pdf_file = "${basename}.pdf";
    if (!-e $pdf_file) {
        system 'lowriter', '--headless', '--convert-to', 'pdf', $file;
    }
}
  • Related