Batch file problem - cannot set var

March 9, 2009 at 22:54:56
Specs: Windows Vista
Hi!

I'm trying to get the following script to work, but it won't let me set the variable %count%, and I cannot for the life of me see why :S I'd be very happy if someone could help me out here! :)

@Echo OFF
SETLOCAL

set windowTitle=My Kids
set xmlfile="My_Kids.xml"
set outputdir="y:\jukebox\kids"

title %windowTitle%

:test
if not exist script.lck goto start

find "%~n0" < script.lck > nul
if not errorlevel 1 (

	FOR /F %%A IN ('tasklist /FI "WINDOWTITLE eq %windowTitle%" ^| find /C "cmd"') DO (
		echo %%A
		SET /A COUNT=%%A
		echo %COUNT%
	)
	ECHO Windows with title "%windowTitle%" and "cmd" %COUNT%
	pause
	if "%count%"== "1" do (
		echo Batch file already running! Terminating...
		pause		
		exit
	) else (
		echo Batch file not running, so deleting stale lock file!
		del script.lck
		pause
	)
	goto start
) 

echo Lock file exists! Will try again every 10 seconds. If no other scripts are running, please delete the lock file manually.
echo.

set /p var= < script.lck

echo Waiting for %var%

:wait
set /p var2= < script.lck
if not "%var2%"=="%var%" (
	echo Now waiting for %var2%
	set var=%var2%
)
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n 10 -w 1000> nul
if not exist script.lck goto start
goto wait

:start
echo %~n0, started @ %DATE%, %time% > script.lck
call moviejukebox %xmlfile% -o %outputdir%
del script.lck


See More: Batch file problem - cannot set var

Report •


#1
March 10, 2009 at 00:19:46
1. change the 2nd line to : setlocal enabledelayedexpansion

2. when echo ing variable value inside for loop:
instead of echo %count%, you now use echo !count!

3. variable count is not the same as variable COUNT. variable name is case sensitive.

4. possible bug: if user close the batch in the middle, then the code is locked forever, unless user have a knowledge of know-how-to-fix-batch-code.


Report •

#2
March 10, 2009 at 00:58:47
reno: 3. variable count is not the same as variable COUNT. variable name is case sensitive.

Tue 03/10/2009  3:52:41
E:\>set count=1


Tue 03/10/2009  3:52:45
E:\>set COUNT=2


Tue 03/10/2009  3:52:51
E:\>echo %count%
2

Case sensitivity isn't really an issue with Command Scripts.

Aside: The new PRE tag style sucks.


Report •

#3
March 10, 2009 at 01:00:14
Thanks, I will try it shortly!

But regarding the lock, it actually checks if the lock file was written by this script, and if so...it deletes it if no other instances are already running. That's actually what I'm checking in the non-working code :) (Yes, I know it's not working right now, but it will as soon as I got the count working ;))

Thanks!


Report •

Related Solutions

#4
March 10, 2009 at 02:11:09
ah, i always assume var name is case sensitive. thank you razor for clearing things up.

the var name in for loop is the one thats case sensitive:
for %a in (hello world) do echo %A

yeah, IMO, the pre tag need some bold color for easier reading or a rectangle box with the title 'code'


Report •


Ask Question