2016-03-31 23 views
1

Aşağıdaki kodu çalıştırdığımda "taskkill beklenmiyor" alıyorum. Program% stop% değerini ayarlayabilmeden kapanır. Bu hatayı nasıl düzeltebilirim?Batch - komutu beklenmiyor

@echo off 
set ucs=ucs.exe 
cls 
echo. 
echo Check %ucs%... 
tasklist /FI "IMAGENAME eq %ucs%" 2>NUL | find /I /N "%ucs%">NUL 
if %errorlevel%==0 (
echo %ucs% is running! 
echo You need to stop it before editing ucsdb 
echo 1 - Stop 
echo 0 - Menu 
set /p "stop=Stop %ucs%?" 
if %stop%==1 taskkill /im %ucs% /t && goto sqlitebrowser 
if %stop%==0 goto sqlitemenu 
if not %stop%==1 if not %stop%==2 echo Option "%stop%" not exist! && timeout /t 2 /nobreak >NUL && goto sqlitemenu 
) ELSE (
echo %ucs% is not running 
echo Loading SqliteBrowser... 
timeout /t 2 /nobreak >nul 
goto sqlitebrowser) 

Kod Düzenleme:!!!!

setlocal EnableDelayedExpansion 
cls 
echo. 
echo Check %ucs%... 
pause 
tasklist /FI "IMAGENAME eq %ucs%" 2>NUL | find /I /N "%ucs%">NUL 
if %errorlevel%==0 (
echo %ucs% is running! 
echo You need to stop it before editing ucsdb 
echo 1 - Stop 
echo 0 - Menu 
set /p "stop=Stop %ucs%?" 
if !stop!==1 taskkill /im %ucs% /t && goto sqlitebrowser 
if !stop!==0 goto sqlitemenu 
if not !stop!==1 if not !stop!==0 echo Options "!stop!" not exist! && timeout /t 2 /nobreak >nul && goto sqlitemenu 
... 

stop değilse == 1 stop değilse == 0 "! Dur!" Seçenekler yankı mevcut değil!

if %errorlevel%==0 (

Bunların hepsi tek bir satırda varmış gibi davranılır kapanış parantez kadar komutları anlamına gelir: işe değil düzgün

+1

Oldukça eminim '% stop%' değişkeni tanımlanmamış ... – aschipfl

+2

Olası çoğaltması [Toplu dosyada gecikmeli genişletme örneği] (http://stackoverflow.com/questions/10558316/example-of- gecikmeli-genişletme-toplu iş dosyası) –

+0

Program% stop% değerini ayarlayabilmeden önce kapanır. Düzenlenmiş kodunuza (son kod satırı) – Blank517

cevap

0

sorun burada bir parantez açma olması. Ortam değişkenleri hemen genişletilir. Özellikle, %stop%'u genişletmeye çalışır. Ancak, henüz tanımlanmamış olması, bu nedenle boş bir dizeye genişler ve if deyimi şuna benzer: Yapmanız Gerekenler

if ==1 taskkill [etc...] 

kullanım genişleme gecikmiş olduğunu. Dennis van Gils' comment'dan the link'u okuyun.

+1

Her zaman insanları da yönlendiririm: [ss64] (http://ss64.com/nt/delayedexpansion.html) –

+0

Teşekkürler şimdi çalışıyor! Ama 'değilse! Dur! == 1 değilse! Dur! == 2 echo Seçenek "! Dur!" mevcut değil! 'her zaman bu çıktıyla yürütülür: Seçenek "! dur!" mevcut değil! – Blank517

+0

@ Blank517 betiğinizin başlangıcında 'setlocal EnableDelayedExpansion' eklediniz mi? –