1.

Solve : only one instance of batch?

Answer»

I have this script which prevents to run two instances of the same batch.
But problem is because I like to set path for that lock FILE and whatever I do doesn't work?
 What I amd doing wrong?
original code
Code: [Select]:init
set "started="
2>nul (
 9>"%~f0.lock" (
  set "started=1"
  call :start
 )
)
if defined started (
    del "%~f0.lock" >nul 2>nul
) else (
    echo Process aborted: "%~f0" is already running
    ping localhost > nul
)

exit /b




:start
cd /d %~dp0
start /b notepad.exe
my changed code
Code: [Select]:init
set "started="
SET PATH=%lockpath%;"d:\test"
2>nul (
 9>"%lockpath%%~f0.lock" (
  set "started=1"
  call :start
 )
)
if defined started (
    del "%lockpath%%~f0.lock" >nul 2>nul
) else (
    echo Process aborted: "%lockpath%%~f0" is already running
    ping localhost > nul
)

exit /b

:start
cd /d %~dp0


start /b notepad.exe
What does variable %lockpath% contain?

if you have killed the system PATH variable by doing this Code: [Select]SET PATH=%lockpath%;"d:\test", why do you expect ping to work?

Do you know why it is a bad idea to name your own private variable "PATH"?

Did you write this script yourself or copy it from somewhere?
I have copied that script, point of that script is to alow only one instance of batch to be run.
with this I tried to make script to create lockfile in folder d:\test
Code: [Select]SET PATH=%lockpath%;"d:\test"because now script create lockfile in folder where is runned from.Any idea about path?Run this batch and consider what it tells you

echo off
Echo (1) running Ping command
echo ------------------------
ping -n 1 127.0.0.1
echo.
set PATH=blablabla
Echo (2) running Ping command
echo ------------------------
ping -n 1 127.0.0.1
pause
 

(1) running Ping command
------------------------

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

(2) running Ping command
------------------------
'ping' is not recognized as an internal or external command,
operable PROGRAM or batch file.
Press any key to continue . . .What does that tell you?
Quote from: Salmon Trout on March 27, 2019, 12:18:36 PM

What does that tell you?
Sorry, really have no clueDid the ping command work both times?I told you WAY up above you shouldn't use a variable called PATH  or path in your scripts. Maybe you didn't read that. You didn't ask why. You don't understand anything about batch scripts, which makes it risky to run scripts you found on the web, and doubly risky to alter them when you don't know what you are doing. You could try something like this below, but maybe you would be better off actually trying to learn how to write your own scripts? or another language? People say good things about Python.

echo off
setlocal enabledelayedexpansion
echo my name is %~nx0
echo now I look for my name using Tasklist
set found=0
for /f "delims=" %%A in ('tasklist /v ^| find "cmd.exe" ^| find "%~nx0"') do set /a found+=1
echo Found this batch in Tasklist %found% time(s).
if %found% gtr 1 (
   echo Script with same name already running
   echo Exiting...
   pause
   exit
   )
REM rest of script
echo If I reached here I am the only %~nx0 running
pause

yes you are right.
I have intention to take class for powershell and learn that.
I know using path which is command is not good like using the name of the batch as some exe program in script.
I learn something but still need alot of it to learn.
thank you

when I test this notepad opens everytime when I run it but it should not.

Code: [Select]echo off
setlocal enabledelayedexpansion
echo my name is %~nx0
echo now I look for my name using Tasklist
set found=0
for /f "delims=" %%A in ('tasklist /v ^| find "cmd.exe" ^| find "%~nx0"') do set /a found+=1
echo Found this batch in Tasklist %found% time(s).
if %found% gtr 1 (
   echo Script with same name already running
   echo Exiting...
   pause
   exit
   )
notepad.exe
echo If I reached here I am the only %~nx0 running
pauseI thought you wanted a batch which would only allow one running instance of itself. Quote from: Salmon Trout on March 28, 2019, 09:54:25 AM
I thought you wanted a batch which would only allow one running instance of itself.

Yes it should run only one instance of batch but I see sometimes is runned two or more.
That's why I was searching for a script which will prevent starting another when first one already runs.
So script which I post it do that, but problem is I can't set a path where it SAVE that lock files.
When I run that script, while it is still running, if you open a new command window, and run it again, the new instance exits without starting Notepad.
Quote from: Salmon Trout on March 28, 2019, 11:43:24 AM
When I run that script, while it is still running, if you open a new command window, and run it again, the new instance exits without starting Notepad.
When I do that it opens notepad everytime.
I tested 3 times and opened 3 notepads.


Discussion

No Comment Found