How to execute a third party utility on every file in a folder recursively?

2

1

I use a CLI utility called tesseract-ocr. The tesseract-ocr's usage in the cmd is this: tesseract.exe imagename outputbase [options...] [configfile...] In my case I type: tesseract photo0043.jpg photo0043.txt -l Cyrillic The utility will take the file called photo0043.jpg and output its job into the file called photo0043.txt How to make a batch to run the utility on an entire directory recursively?

computationalprince

Posted 2019-02-11T20:47:48.330

Reputation: 101

1

This may be helpful - Software to batch OCR multiple image files to multiple text files using Tesseract?

– user3169 – 2019-02-14T07:08:59.327

Answers

1

Use a .bat file with the FOR /R command to loop through files and recurse on subfolders.

Something like:

@echo off
Setlocal enabledelayedexpansion

For /R C:\path\to\folder %%a in (*.jpg) Do (
Set filename=%%~na
tesseract "%%a" !filename!.txt -l Cyrillic
)

Warning: I have not tested this script. It needs some tweaking if you have .jpg files in subfolders of the specified folder.

harrymc

Posted 2019-02-11T20:47:48.330

Reputation: 306 093

I've surrounded the folder path and %%~na with double quotes and it worked but not for .jpg files in subfolders. Also, how to make it so that the output files are created in the same location where input files are because when I ran the batch everything was created in the root folder that was specified. – computationalprince – 2019-02-11T21:26:56.897

You could add the target path in the script in front of !filename!.txt. Might need enclosing in double quotes. – harrymc – 2019-02-12T12:04:24.073

This does the job for me if anybody is interested.. `@echo off Setlocal enabledelayedexpansion

For /R "C:\path\to\folder" %%a in (*jpg) Do ( Set filename="%%~dpna" "tesseract" "%%a" !filename!.TESS -l Cyrillic )` – computationalprince – 2019-02-13T16:32:56.160