3

I have a batch file which can update a web project and clean/rebuild it. I made it executable for network users. Now I want to make the batch executable only by one user at the same time. Like synchronize object in programming languages.

Is there a possibility to do that?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Are you talking about an executable (as per your subject) or a batch file (as per your question text)? – Harry Johnston Sep 22 '14 at 04:09
  • You want to prevent two executions at the same time? Or do you want to allow multiple people to run it, but only allow a given user the ability to run it once? And as Harry Johnston asked - is this a batch file or an exe file? – dbenham Sep 22 '14 at 04:39
  • thanks for ask, it is a batch file, and exactly i want to limit only one user can run it in once time – Lionel Nguyen Sep 22 '14 at 05:04

1 Answers1

2

A simple solution to check if batch file is already running is using file system.

The batch file can check if a file exists and denies execution in this case, otherwise it creates the file, runs the commands and finally deletes the file.

@echo off
if exist "C:\Temp\BatchLock.txt" goto BatchRunning
echo Batch file is running by %username%.>C:\Temp\BatchLock.txt

rem All other commands of the batch file

del C:\Temp\BatchLock.txt
goto :EOF

:BatchRunning
type C:\Temp\BatchLock.txt
echo/
echo Please run the batch later again.
echo/
echo Press any key to exit ...
pause >nul

Of course C:\Temp is not a good storage location for the lock text file. It must be a directory which is identical for all users, a directory on server with write permissions for all users.

Mofi
  • 46,139
  • 17
  • 80
  • 143