kotAPI
kotAPI

Reputation: 419

How to lock the computer after executing an exe file using batch file programming in windows?

I have a batch file program something like.

@echo off
start Mat.exe
>>Need a code here to runand check for termination of "Mat.exe"
rundll32.exe user32.dll, LockWorkStation

>>end of program

If anyone can help me set up the program so that I can lock my computer as soon as the "Mat.exe" file is terminated. I'd really appreciate it. Thanks in advance

Upvotes: 1

Views: 3317

Answers (2)

m8L
m8L

Reputation: 119

Try using the Start command with the /Wait switch. Here is an example:

@echo off 
REM //start Mat.exe
start /wait [path of Mat.exe]
rundll32.exe user32.dll, LockWorkStation

Remember to use the 8.3 filenames and do not include (" ") in the path string

Upvotes: 1

Christoph
Christoph

Reputation: 315

Simply drop the start before Mat.exe so your batch-file will wait until Mat.exe has finished.

Edit: This only works if your Mat.exe runs in a console or similar. If dropping the start didn't work you may have to check wheather or not Mat.exe is still running. For that I found a quite useful post, which should work for you: How to wait for a process to terminate to execute another process in batch file

Edit2: Complete Code:

@echo off
:LOOP
start Mat.exe
TASKLIST 2>&1 | find "Mat.exe" > nul
IF ERRORLEVEL 1 (
  rundll32.exe user32.dll, LockWorkStation
) ELSE (
  SLEEP 3
  GOTO LOOP
)

You can adjust the SLEEP as you like, but i would suggest use at least 1 sec, otherwise you'll have 100% usage.

Upvotes: 5

Related Questions