0

I have to run a tool from libreoffice i.e. soffice to convert doc/docx to PDF files.

It works well. But I don't want to convert them again and again. So I need a if check to determine if the file.

So, I am stuck with following :

  • After getting filename how can I replace the extension from .doc or .docx to pdf.
  • After renaming how to check if the file exists or not.

I have tried until this :

@echo off
for %%f in (*.doc*) do (
    if "%%~xf"==".doc" (
        echo %%f
        set str = %%f
        call :strLen str strlen
        echo String is %strlen% characters long
        soffice --headless --convert-to pdf:writer_pdf_Export --outdir . "%%f"
    )
    if "%%~xf"==".docx" (
        echo %%f
        soffice --headless --convert-to pdf:writer_pdf_Export --outdir . "%%f"
    )
)

:strLen
setlocal enabledelayedexpansion

:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
Compo
  • 36,585
  • 5
  • 27
  • 39
graphics123
  • 1,191
  • 3
  • 20
  • 57
  • 1
    Your `set` syntax is bad. See `set /?` and remove the spaces around the `=`. You already used `%%~xf` to extract the extension. See `for /?` for more modifiers. And of course there is that [delayed expansion trap](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Feb 01 '21 at 07:33
  • Your main routine is missing an `exit /b`. Your `:strlen` function doesn't initialize `len`, when it works, it's only luck – jeb Feb 01 '21 at 07:41
  • The `length` issue seems superfluous - it's only reported, sometimes & unreliably. You don't appear to be actually using the data for any purpose. Perhaps you could edit your question to include the missing word(s) in `So I need a if check to determine if the file.` already exists? exists? doesn't exist? Guessing that `"%%f"`on the `soffice` line is the file-to-be-converted, what is the name of the file produced? Is it `filename.pdf` or `filename.doc(x).pdf` or `someconstantname.pdf` or what? – Magoo Feb 01 '21 at 13:46

0 Answers0