Applying recursion in a batch script

Asked

Viewed 807 times

2

Some time ago I came to the need to use the Microsoft Office Document Imaging to convert a document template created in Microsoft Word as an anti-copy prevention measure of the model itself (positioning, measurements, definitions, etc.).

But the application in question, for those who do not know, is very spartan, slow and with some weird restrictions too for a GUI. Fortunately, the functionality I needed could be executed on the command line.

I’ve never been a big fan of batch scripts, but I still chose to create a file. BAT to automate OCR of MDI files previously generated by the virtual printer provided by the application.

Like the syntax of batch scripts it was old stuff when I got my first computer, I never bothered to learn more than necessary to install Windows 98 (at the time) and I ended up with the following file:

@echo off

set filesCount=0

echo Performing OCR over files
echo.

for /r %%i in (*.mdi) do (

set /a filesCount+=1

echo Curent file: %%i

"C:\Program Files\Common Files\Microsoft Shared\MODI\11.0\MSPVIEW.EXE" -f %%i

echo OCR performed succesfully
echo.

)

echo.
echo OCR performed over %filesCount% files found
echo.

pause

It works well, but I have to duplicate (or move) the file. BAT for each directory before running it. And because execution is applied to thousands of hierarchically structured files in several sublevels, it becomes a tedious task almost not making it worth using said script.

Is it possible to modify this script to operate recursively? That way, I would store it in the parent directory of all sublevels and run it once.

  • I tested it here and your bat is already running the command recursively, just run it in the root directory...

  • @Bruno wants to run this command recursively over his entire root directory?

  • @Jader: Really? I confess I never tried it to see what would happen. Perhaps because the PC I would use is coal-powered. Kyllopardium: Not in the root directory, but from a root. Something like . / of *Nix. From where . BAT lies forward (or "inside")

  • look at the test I did: print obviously I removed the line with the executable, but you can see that it takes all the files in all directories...

  • I will try it only Monday, at the service. In the interim, if you want to answer with this test, I would be happy to evaluate and mark as solved. But only Monday :p

1 answer

2


According to a test I performed here, your bat already works recursively:

test bat.

@echo off

set filesCount=0

echo Performing OCR over files
echo.

for /r %%i in (*.mdi) do (

set /a filesCount+=1

echo Curent file: %%i


echo OCR performed succesfully
echo.

)

echo.
echo OCR performed over %filesCount% files found
echo.

pause

outworking:

inserir a descrição da imagem aqui

Obs.: the line "C:\Program Files\Common Files\Microsoft Shared\MODI\11.0\MSPVIEW.EXE" -f %%i was removed to avoid errors during execution as I do not have this executable.

  • Boy... if I’d known it would take so many days before I could test the script, I wouldn’t have said "Monday". But it worked perfectly, it was already recursive and I didn’t even know. And the responsible for this is the flag /r

Browser other questions tagged

You are not signed in. Login or sign up in order to post.