How to make an error message in a batch script if the target isn't present?
Solution 1
I would also like to use a cleaner dialog for the error message. Can I incorporate such a method into the batch script?
Yes, you can run msgbox with a batch script using wscript as in the example I provided below. Since you also indicated you wanted "to use a cleaner dialog for the error message" I used the top line to set a variable and start the batch script again but with the /MIN
option to keep the batch window from being so noticeable. Simple remove the top line if you want to not use it.
Batch Script
Be sure to set the values for the variables SET TARGETPATH=
, SET msgboxTitle=
, and SET msgboxBody=
for your needs accordingly.
IF NOT DEFINED MINIMIZED SET MINIMIZED=1 && START "" /MIN "%~dpnx0" %* && EXIT
SET TARGETPATH=C:\Users\User\Desktop\Test
IF NOT EXIST "%TARGETPATH%" GOTO :ERROR
%SYSTEMROOT%\EXPLORER /SELECT, "%TARGETPATH%"
GOTO :END
:ERROR
SET msgboxTitle=This is my Message Title
SET msgboxBody=This is my Message Body
SET tmpmsgbox=%temp%\~tmpmsgbox.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
WSCRIPT "%tmpmsgbox%"
:END
Further Resources
Solution 2
set MESSAGETXT="Your Text Goes Here" set TARGETPATH="Your Path Goes Here" IF NOT EXIST "%TARGETPATH%" GOTO ERROR %SYSTEMROOT%\explorer.exe /select, "%TARGETFILEPATH%" GOTO END :ERROR msg "Username" "%MESSAGETXT%" :END
You almost had it. SS64 is your friend when it comes to batch programming. And general command line usage as well: Here is their site. I have no affiliation with them.
EDIT:
Wow, yeah I made some bad assumptions there and just added the code without testing/looking at it. Windows 7 doesn't have net send
anymore but uses msg
instead. I've updated my answer and it works on my system. You can add a PAUSE
command at the end if you want the command line to stay visible until you press a key to get rid of it.
Solution 3
Unfortunately, the script provided by @Blerg didn't work *(for me). I ventured into Stack Overflow and found this thread and used it in tandem with @Blerg's script. The script is now able to throw an error message using this script that I threw together:
IF NOT EXIST "%TARGETPATH%" GOTO ERROR
%SYSTEMROOT%\explorer.exe /select, "%TARGETPATH%"
GOTO END
:ERROR
START CMD /C "ECHO The target could not be located because it does not exist! && PAUSE"
:END
(All I had to do was replace the fifth line.)
Even though this works, I would also like to use a cleaner dialog for the error message like this VBS script can make:
x=msgbox("%WINDOWTEXT%" ,0, "%WINDOWTITLE%")
Can I incorporate such a method into the batch script, or should I translate the batch script into a visual basic script?
Related videos on Youtube
Mr. Mendelli
Updated on September 18, 2022Comments
-
Mr. Mendelli over 1 year
I have made a batch script template that I can program to select a given target, but I would like the script to also throw a custom error dialog if the target is not already present on the end-users machine.
Here's my current script:
set targetfilepath=%TARGETPATH% %SYSTEMROOT%\explorer.exe /select, "%TARGETFILEPATH%"
Could I incorporate this into my script? If so, where should I place it?:
IF NOT EXIST goto net send localhost "%MESSAGETXT%"
-
Mr. Mendelli almost 7 yearsThanks! I had no idea that site existed, I know what I'll be frequenting for info on batch scripting from now on. However, I tried your script, but it did not seem to work. Should there be a pause perhaps?
-
Mr. Mendelli almost 7 yearsExcellent! This worked very well, and is easy to add to an existing script. Thank you.