3

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%"
3
  • You can use IF EXIST to test if a path or file already exists and continue from there.
    – LPChip
    Jul 4, 2017 at 7:40
  • SuperUser is not a "Please write me a script" kind of service. If you google for IF and CMD you'll find plenty of examples. Feel free to edit your question with a more full script so we can help with small errors.
    – LPChip
    Jul 4, 2017 at 7:51
  • I'm voting to close this question as off-topic because it asks for specific scripts.
    – Overmind
    Jul 5, 2017 at 6:54

3 Answers 3

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

1
  • Excellent! This worked very well, and is easy to add to an existing script. Thank you. Jul 5, 2017 at 4:31
4
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.

1
  • Thanks! 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? Jul 4, 2017 at 23:12
1

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?

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .