1
 usage: pdftotext [options] <PDF-file> [<text-file>]

I'm using pdftotext (xpft, usage above) to covert all pdfs in a directory (and subdirectories) to textfiles. It doesn't matter if structure is preserved or not I just want to write the files to a different directory.

I have cmd cd already pointing at the directory ("C:\input" say ).

So if the path of a given input file is

 C:\input\filename.pdf

And I want to output to

 C:\output\filename.txt

My command:

 for /r %i in (*.pdf) do pdftotext "%i" -raw "C:\output\%i"

Nearly works, but it attempts to output to

 C:\input\C:\output\filename

which obviously causes an error.

How do I solve this?

Some_Guy
  • 794

1 Answers1

3

How do I use a for loop and output to a different directory

Use the following command:

for /r %i in (*.pdf) do pdftotext -raw "%i" "C:\output\%~ni.txt"
  • %~ni Expands %i to a file name only (i.e. removes drive letter, path and .pdf extension)

  • %~ni.txt also appends a new extension, .txt


Extended syntax

When an argument is used to supply a filename then the following extended syntax can be applied:

we are using the variable %1 (but this works for any parameter)

  • %~f1 Expand %1 to a Fully qualified path name - C:\utils\MyFile.txt

  • %~d1 Expand %1 to a Drive letter only - C:

  • %~p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

  • %~n1 Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash) - the last folder in that path.

  • %~x1 Expand %1 to a file eXtension only - .txt

  • %~s1 Change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists.)

  • %~1 Expand %1 removing any surrounding quotes (")

  • %~a1 Display the file attributes of %1

  • %~t1 Display the date/time of %1

  • %~z1 Display the file size of %1

  • %~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

  • %~dp1 Expand %1 to a drive letter and path only

  • %~sp1 Expand %1 to a path shortened to 8.3 characters

  • %~nx2 Expand %2 to a file name and extension only

Source Command Line arguments (Parameters)


Further reading

DavidPostill
  • 162,382