3

I have been scratching my head for a few days trying to get this to work. I am trying to write a batch script to look for text in a PDF file and move said file to a folder. Sounds simple but i have failed to get a for loop to parse a variable. This is what i have so far:

@echo off
SetLocal
setlocal enabledelayedexpansion
set dir=C:\Pdf Invoices
set inc=C:\Pdf Invoices\ESD\Includes
title Signing Invoices
echo Signing Invoices.....
echo.

:Movefiles
REM Check "IN" folder for invoices

if exist "%dir%\In\*.pdf" (goto MOVEFORPROCESSING) else (goto END)

:MOVEFORPROCESSING
for /f %%a in ('DIR /b "%dir%\In\*.pdf"') do (
    move "%dir%\In\%%a" "%dir%\ESD\Processing\" >nul
)

for /r "%dir%\ESD\Processing" %%F in (*.pdf) do (
    set type="%inc%\pdftextreplacer_cmd\pdftr.exe" -searchtext "USD" "%%F" | find /C "USD"
    If "%type%" == "0" (
        echo File is ZAR
)   else (
        echo File is USD
        )
)

:END

Basically if the file contains USD move to USD folder and IF file contains "ZAR" move to "ZAR" folder. I am using pdftextreplacer to search the pdf files which works fine.

Any help would be greatly appreciated.

zb226
  • 9,586
  • 6
  • 49
  • 79
  • turn echo off and add a goto :EOF after your IF Exist and see what is happening. You need to be a bit more descriptive as to what isn't working. You have 2 for loops. Which one isn't being processed correctly? – Matt Williamson Oct 22 '13 at 14:12
  • Sorry Matt see what you mean. Its the second loop thats giving me the headache. The echo's are there for testing to see where the batch is falling over. –  Oct 22 '13 at 14:35
  • 1. Find /c needs a : for exact string match. 2. When comparing numbers you shouldn't use quotes. What do you get if you just echo %type%? I have a feeling just checking errorlevel as stated in the answer below will help. – Matt Williamson Oct 22 '13 at 14:43
  • checkout my answer here, it might help: http://stackoverflow.com/questions/19256980/batch-script-concatenate-sequential-numbers-on-files-to-keep-the-names-unique – cure Oct 22 '13 at 14:46
  • Remove the spaces in the `==` statement. Like so `If "!type!"=="0" (` Batch interprets those spaces literally around the `==` operator. Therefore, "0 "==" 0" will be false. Also, you need to [EnableDelayedExpansion](http://stackoverflow.com/a/13809834/891976) and use the `!` notation if you are going to access a variable that is set within the same set of parentheses. `!type!` – David Ruhmann Oct 22 '13 at 15:06

1 Answers1

3

Posible problem with white spaces

for /F "tokens=*" %%a in (.....

Posible delayed expansion problem with variable %type%

EXISTS problem with logic of setting variable %type%. You can no set a variable to a return value of a executing command. Rewrite to

"%inc%\pdftextreplacer_cmd\pdftr.exe" -searchtext "USD" "%%F" | find "USD" >nul 
if errorlevel 1 (
    echo File is ZAR
) else (
    echo File is USD
)
MC ND
  • 69,615
  • 8
  • 84
  • 126