0

I have made a template batch script that I can tell to carry out a function. I usually use it for pointing to files or locations, in this case, I want it to find and highlight a file and throw a custom error if the target isn't present. Whether the target is present or not, my script throws my error. What am I doing wrong or missing?


Here is my script:

SET TARGETPATH= "%TARGETPATH%"
IF NOT EXIST= "%TARGETPATH%" GOTO :ERROR
%SystemRoot%\explorer.exe /select, "%TARGETPATH%"
:END

:ERROR
SET msgboxTitle=%TITLE%
SET msgboxBody=%MESSAGE%
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
WSCRIPT "%tmpmsgbox%"

:END

I have moved and adjusted he position of line 3 (the target selector) to many different positions to prevent the error in a successful target acquisition. I have even set a :END after the selector among many other things. How can I get this script to throw the error if, and only if the specified target isn't present?

  • 2
    Check your syntax. To my knowledge the IF uses IF .. == .. so you're missing an =. – LPChip Feb 23 '18 at 10:04
  • 3
    Line 4 should be goto END; otherwise, after line 3 you will drop through into the error handling code. – AFH Feb 23 '18 at 10:06
  • 3
    NOT EXIST doesn't require = at all, just use ` NOT EXIST "%TARGETPATH%"` – Máté Juhász Feb 23 '18 at 10:06
  • Thank you for the replies, I combined @AFH's goto END: that I forgot to add in combination with @Máté Juhász's code correction. The script functions correctly now. If some one would like to provide the solution an answer, I will accept and upvote. – Mr. Mendelli Feb 23 '18 at 10:10
4

Line 2 : IF NOT EXIST should look like this

If Not Exist %TARGETPATH% GOTO :ERROR

And at line 4

goto :END

From this Stackoverflow question

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.