Ik heb een vraag over de if – else-structuur in een batchbestand. Elke opdracht wordt afzonderlijk uitgevoerd, maar ik kon de “if – else”-blokken niet veilig gebruiken, dus deze delen van mijn programma werken niet. Hoe kan ik deze onderdelen laten draaien? Dank je.
IF %F%==1 IF %C%==1 (
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
)
ELSE IF %F%==1 IF %C%==0 (
::moving the file c to d
move "%sourceFile%" "%destinationFile%"
)
ELSE IF %F%==0 IF %C%==1 (
::copying a directory c from d, /s: boş olanlar hariç, /e:boş olanlar dahil
xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
)
ELSE IF %F%==0 IF %C%==0 (
::moving a directory
xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
rd /s /q "%sourceMoveDirectory%"
)
Antwoord 1, autoriteit 100%
Uw syntaxis is onjuist. Je kunt ELSE IF
niet gebruiken. Het lijkt erop dat je het toch niet echt nodig hebt. Gebruik gewoon meerdere if
-instructies:
IF %F%==1 IF %C%==1 (
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
)
IF %F%==1 IF %C%==0 (
::moving the file c to d
move "%sourceFile%" "%destinationFile%"
)
IF %F%==0 IF %C%==1 (
::copying a directory c from d, /s: boş olanlar hariç, /e:boş olanlar dahil
xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
)
IF %F%==0 IF %C%==0 (
::moving a directory
xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
rd /s /q "%sourceMoveDirectory%"
)
Geweldige referentie voor batchbestanden: http://ss64.com/nt/if.html
Antwoord 2, autoriteit 49%
Ik denk dat er in de vraag en in sommige antwoorden enige verwarring bestaat over de betekenis van deze pseudocode in DOS: IF A IF BX ELSE Y. Het betekent niet IF(A en B) THEN X ELSE Y , maar in feite betekent IF A( IF B THEN X ELSE Y). Als de test van A faalt, wordt de hele innerlijke if-else genegeerd.
Zoals een van de genoemde antwoorden, kan in dit geval slechts één van de tests slagen, dus het ‘anders’ is niet nodig, maar dat werkt natuurlijk alleen in dit voorbeeld, het is geen algemene oplossing om als- te doen anders.
Er zijn veel manieren om dit te omzeilen. Hier zijn een paar ideeën, ze zijn allemaal nogal lelijk, maar hey, dit is (of was tenminste) DOS!
@echo off
set one=1
set two=2
REM Example 1
IF %one%_%two%==1_1 (
echo Example 1 fails
) ELSE IF %one%_%two%==1_2 (
echo Example 1 works correctly
) ELSE (
echo Example 1 fails
)
REM Example 2
set test1result=0
set test2result=0
if %one%==1 if %two%==1 set test1result=1
if %one%==1 if %two%==2 set test2result=1
IF %test1result%==1 (
echo Example 2 fails
) ELSE IF %test2result%==1 (
echo Example 2 works correctly
) ELSE (
echo Example 2 fails
)
REM Example 3
if %one%==1 if %two%==1 (
echo Example 3 fails
goto :endoftests
)
if %one%==1 if %two%==2 (
echo Example 3 works correctly
goto :endoftests
)
echo Example 3 fails
)
:endoftests
Antwoord 3, autoriteit 11%
AFAIK je kunt geen if else
in batch doen zoals je kunt in andere talen, het moet genest zijn if
‘s.
Als u geneste if
‘s gebruikt, ziet uw batch er als volgt uit
IF %F%==1 IF %C%==1(
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
) ELSE (
IF %F%==1 IF %C%==0(
::moving the file c to d
move "%sourceFile%" "%destinationFile%"
) ELSE (
IF %F%==0 IF %C%==1(
::copying a directory c from d, /s: boş olanlar hariç, /e:boş olanlar dahil
xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
) ELSE (
IF %F%==0 IF %C%==0(
::moving a directory
xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
rd /s /q "%sourceMoveDirectory%"
)
)
)
)
of zoals James suggereerde, koppel je if
‘s aan elkaar, maar ik denk dat de juiste syntaxis is
IF %F%==1 IF %C%==1(
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
)
Antwoord 4, autoriteit 2%
Ik geloof dat je iets kunt gebruiken zoals
if ___ (
do this
) else if ___ (
do this
)
Antwoord 5
Een beetje laat en misschien nog steeds goed voor complexe if-condities, omdat ik een “done”-parameter zou willen toevoegen om een if-then-else-structuur te behouden:
set done=0
if %F%==1 if %C%==0 (set done=1 & echo found F=1 and C=0: %F% + %C%)
if %F%==2 if %C%==0 (set done=1 & echo found F=2 and C=0: %F% + %C%)
if %F%==3 if %C%==0 (set done=1 & echo found F=3 and C=0: %F% + %C%)
if %done%==0 (echo do something)
Antwoord 6
IF...ELSE IF
-constructies werken heel goed in batchbestanden, vooral wanneer u slechts één voorwaardelijke expressie op elke IF-regel gebruikt:
IF %F%==1 (
::copying the file c to d
copy "%sourceFile%1" "%destinationFile1%"
) ELSE IF %F%==0 (
::moving the file e to f
move "%sourceFile2%" "%destinationFile2%" )
In jouw voorbeeld gebruik je IF...AND...IF
type constructie, waarbij tegelijkertijd aan 2 voorwaarden moet worden voldaan. In dit geval kunt u nog steeds de constructie IF...ELSE IF
gebruiken, maar met extra haakjes om onzekerheid voor de volgende ELSE-voorwaarde te voorkomen:
IF %F%==1 (IF %C%==1 (
::copying the file c to d
copy "%sourceFile1%" "%destinationFile1%" )
) ELSE IF %F%==1 (IF %C%==0 (
::moving the file e to f
move "%sourceFile2%" "%destinationFile2%"))
De bovenstaande constructie is gelijk aan:
IF %F%==1 (
IF %C%==1 (
::copying the file c to d
copy "%sourceFile1%" "%destinationFile1%"
) ELSE IF %C%==0 (
::moving the file e to f
move "%sourceFile2%" "%destinationFile2%"))
Verwerkingsvolgorde van batchopdrachten is afhankelijk van CMD. exe-ontledingsvolgorde. Zorg er gewoon voor dat uw constructie die logische volgorde volgt, en in de regel zal het werken. Als uw batchscript zonder fouten wordt verwerkt door Cmd.exe, betekent dit dat dit de juiste constructie is (d.w.z. ondersteund door uw OS Cmd.exe-versie), zelfs als iemand anders zei.
Antwoord 7
Hier is mijn code Voorbeeld voor if..else..if
die het volgende doen
Gebruiker vragen om procesnaam
Als de procesnaam ongeldigis
Dan is het schrijven naar gebruiker
Error : The Processor above doesn't seem to be exist
als de procesnaam services
. is
Dan is het schrijven naar gebruiker
Error : You can't kill the Processor above
als de procesnaam geldigis en niet services
Dan is het schrijven naar gebruiker
het proces is afgebroken via taskill
dus ik noemde het Proceskiller.bat
Hier is mijn code:
@echo off
:Start
Rem preparing the batch
cls
Title Processor Killer
Color 0B
Echo Type Processor name to kill It (Without ".exe")
set /p ProcessorTokill=%=%
:tasklist
tasklist|find /i "%ProcessorTokill%.exe">nul & if errorlevel 1 (
REM check if the process name is invalid
Cls
Title %ProcessorTokill% Not Found
Color 0A
echo %ProcessorTokill%
echo Error : The Processor above doesn't seem to be exist
) else if %ProcessorTokill%==services (
REM check if the process name is services and doesn't kill it
Cls
Color 0c
Title Permission denied
echo "%ProcessorTokill%.exe"
echo Error : You can't kill the Processor above
) else (
REM if the process name is valid and not services
Cls
Title %ProcessorTokill% Found
Color 0e
echo %ProcessorTokill% Found
ping localhost -n 2 -w 1000>nul
echo Killing %ProcessorTokill% ...
taskkill /f /im %ProcessorTokill%.exe /t>nul
echo %ProcessorTokill% Killed...
)
pause>nul
REM If else if Template
REM if thing1 (
REM Command here 2 !
REM ) else if thing2 (
REM command here 2 !
REM ) else (
REM command here 3 !
REM )
Antwoord 8
hier is hoe ik omging met een andere situatie
if %env%==dev (
echo "dev env selected selected"
) else (
if %env%==prod (
echo "prod env selected"
)
)
Opmerkinghet is niet hetzelfde als if-elseif-blok als de andere programmeertalen zoals C++ of Java, maar het doet wat je moet doen