How can I make it so that when the folder can't be found, the code will use the goto command?
Here's my code:
:T
Echo Folder is Already Unlocked
:CODE
if attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" goto T
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" Codes
call :c 09 " Codes Unlocked successfully
goto UNLOCK
Here's some examples - given as "answers" since the formatting is easier to use
#ECHO OFF
SETLOCAL
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: Now some multi-line responses
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (
echo errorlevel is 1 or greater
echo I say again - errorlevel 1 1 or greater
echo I say a third time - errorlevel 1 1 or greater
) else (
echo errorlevel is 0
echo again errorlevel is 0
echo once again errorlevel is 0
)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (
echo Y-errorlevel is 1 or greater
echo I say again - errorlevel 1 1 or greater for Y
echo I say a third time - errorlevel 1 1 or greater - Y
) else (
echo Y-errorlevel is 0
echo Y-again errorlevel is 0
echo Y-once again errorlevel is 0
)
:: conditional modification of variable
set /a var=22
echo x|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing x, var is %var%
set /a var=22
echo y|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing y, var is %var%
:: You'll need to change these lines to files/directories that exist or don't exist
set "fileexists=c:\pdoxusrs.net"
set "direxists=c:\users"
set "filemissing=c:\missing.net"
set "dirmissing=c:\nothere"
for %%a in ("%fileexists%" "%direxists%" "%filemissing%" "%dirmissing%") do (
if exist "%%~a" (echo "%%~a" exists) else (echo "%%a" missing)
)
goto :eof
Now if errorlevel isn't being set to 0/non-zero as you appear to be expecting, then try using if exist instead. Probably easier that way, actually. Unfortunately "doesn't work" simply means nothing - we'd need to know exactly what happened, and we'd need you to edit your original posting to add-in your current code (or at least, a representative portion.)
Related
I have this
#echo off
:1
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo -(Option 1)
echo Option 2
echo Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt1
:2
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo Option 1
echo -(Option 2)
echo Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 1
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt2
:3
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo Option 1
echo Option 2
echo -(Option 3)
choice /c WSE /n
if ERRORLEVEL 1 goto 2
if ERRORLEVEL 2 goto 1
if ERRORLEVEL 3 goto opt3
:opt1
cls
echo You chose Option 1
pause >nul
exit
:opt2
cls
echo You chose Option 2
pause >nul
exit
:opt3
cls
echo You chose Option 3
pause >nul
exit
What it is supposed to do is look like a selection menu, but for some reason it just constantly loops through ":1" from lines 2 to 9 it just loops over and over again, why is it doing this? How do I make it not do this?
Read choice /?.
According to that help page, errorlevel(s) are supposed to be checked in reversed order.
choice /c WSE /n
if ERRORLEVEL 3 goto opt3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 1 goto 1
Why? Because if ERRORLEVEL 1 means if %errorlevel% geq 1. Alternatively, you can do:
if %errorlevel% equ 1 goto 1
if %errorlevel% equ 2 goto 2
if %errorlevel% equ 3 goto opt3
Or even crazier(but safer) with findstr.
set errlvl=%errorlevel:~-1%
echo "%errlvl%"| findstr /l /x /i """1""" && goto :1
echo "%errlvl%"| findstr /l /x /i """2""" && goto :2
echo "%errlvl%"| findstr /l /x /i """3""" && goto :opt3
We do not really need that set statement, but a precaution
Just another reminder, if the user pressed key(s) out of the "WSE", an annoyingly loud beep will occur. You may want to check out my poorly title question to find out methods of silencing them.
Having an issue with the following issue with a batch file...
Problem: I have created a batch script to check if files are present in a folder location and if there are files present it will write the PC name (labeled asset) to a log file. However, after scanning 99 PCs it hangs for a about 20 seconds and then scans a few more and hangs. I need to figure out a way to have this run smoothly.
Performance shows that the processor does spike. I've tried calling it from another batch file with a low priority with the same results.
All PCs are named "ABC" followed by 4 numbers.
Full code is below because I do not know where the focus should be. If there is a more appropriate edit I would be happy to oblige.
cls
#echo off
echo Set range of asset tags to scan:
:min
echo:
set /p min="Enter lowest asset number to scan (number only): "
if not defined min goto :min
if %min% lss 1000 (
msg * Asset must be 4 digits
goto :min
)
if %min% gtr 5000 (
msg * Min asset out of range
goto :min
)
:max
echo:
set /p max="Enter highest asset number to scan (number only): "
if not defined max goto :max
if %max% gtr 5000 (
msg * Max asset out of range
goto :max
)
if %max% lss %min% (
msg * Max cannot be lower than min
goto :max
)
set /a max=%max%+1
#echo off
REM Count Logic
set count=%min%
REM sets date/time log stamp to consistent value. If done per line, the seconds change will create multiple files.
set name=Scan_Results_%DATE:~-4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6 ,2%%TIME:~10,2%
REM creates a date and time stamped log file from location batch file is run
echo Dictation Scan from %Date% %Time% > %name%.txt
#echo off
:loop
#echo off
set /a count=%count%
echo Asset tag being scanned: %count%
#echo off
REM "ABC" to count as string to inject in filepath
set asset=abc%count%
ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No
:scan
REM IF LOGIC (If files are detected DO X or else Y [loop until end])
#echo off
for /F %%i in ('dir /b "\\%asset%.domain.com\C$\Program Files\Speech Machines\SubSpace\temp\*.*"') do (
goto :Yes
)
goto :No
:Yes
echo writing asset to log file
echo %asset% >> %name%.txt
set /a count=%count%+1
goto :iteration
:No
set /a count=%count%+1
goto :iteration
:iteration
REM This is the highest asset tag number that you want to scan
if not #%count%#==#%max%# goto loop
goto final
:final
echo %found% >> %name%.txt
#echo SCANNING COMPLETE.
Pause
exit
The logic in
ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No
fails to detect offline machines. The if errorlevel n construct will be evaluated as true for any errorlevel value equal or greater than n, so, if errorlevel 0 will be true for any non negative errorlevel.
You can try with any of
ping -n 1 %asset%.domain.com > NUL
if errorlevel 1 ( goto :no ) else ( goto :scan )
ping -n 1 %asset%.domain.com > NUL
if not errorlevel 1 ( goto :scan ) else ( goto :no )
ping -n 1 %asset%.domain.com > NUL && goto :scan || goto :no
ping -n 1 %asset%.domain.com > NUL
if %errorlevel% equ 0 ( goto :scan ) else ( goto :no )
BUT, depending on the ip version and subnet of source and target machines, testing the ping errorlevel is not a secure way to test if the target machine is online (read here)
I made a game menu and the "2" works gret but when i click "5" it adds +1 to "menumark"
BTW I have Windows 7.
Some more questions:
1) Can you put a color to a specific letter?
2) Why doesnt: if errorlevel 3 set /a menumark=%menumark%-1 work?
Code:
...
...
...
set menumark=1
set checkmark=1
set mark1=/
set mark2=.
set mark3=.
...
...
...
:MENU
cls
echo ----------
echo %mark1% (1) Play.
echo %mark2% (2) Settings.
echo %mark3% (3) Scores.
echo ----------
choice /c 25 /n
if errorlevel 1 set /a menumark=%menumark%+1
if errorlevel 2 set checkmark=2
if %menumark%==4 set menumark=1
if %menumark%==0 set menumark=1
if %menumark%==1 set mark1=/&set mark2=.&set mark3=.
if %menumark%==2 set mark2=/&set mark1=.&set mark3=.
if %menumark%==3 set mark3=/&set mark2=.&set mark1=.
if %checkmark%==2 if %menumark%==1 goto TOP
if %checkmark%==2 if %menumark%==2 goto SETTINGS
if %checkmark%==2 if %menumark%==3 goto HIGHSCORE
goto MENU
...
...
...
Your problem is the control of errorlevel. The code if errorlevel n command will execute the indicated command for any errorlevel value greater or equal to n. For this reason, to use this kind of testing it is necessary to test from greater to lower values of errorlevel and properly code to avoid problems
if errorlevel 3 echo 3
if errorlevel 2 echo 2
if errorlevel 1 echo 1
if your errorlevel is 3, the previous code will echo the three values, as all the conditions are evaluated to true.
if errorlevel 3 (
echo 3
) else if errorlevel 2 (
echo 2
) else if errorlevel 1 (
echo 1
) else echo no error level
if %errorlevel%==3 echo 3
if %errorlevel%==2 echo 2
if %errorlevel%==1 echo 1
Both versions will avoid the "problem" (or you can use gotos to avoid falling through the options)
For the color management, here you can find all the required information.
I'm trying to store the ERRORLEVEL environment variable into a a local batch variable. But it always turns out to be 0.
CALL foo.exe
SET LEVEL=%ERRORLEVEL%
IF ERRORLEVEL 1 (
SET /A ERRORCOUNT=ERRORCOUNT+1
) ELSE (
SET /A OK=OK+1
)
ECHO/ >> logtemp.txt
ECHO ** EXIT %LEVEL% *******************************
I have tried to ECHO %ERRORLEVEL% but it always print 0 too.
foo.exe is generating an error and it can be seen by ECHO %ERRORLEVEL% from the command prompt and the ERRORCOUNT is updated correctly.
I suppose your problem is not the errorlevel, it's your foo.exe.
A simple test with an errorlevel works.
(call) sets the errorlevel to 1
(call)
SET LEVEL=%ERRORLEVEL%
IF ERRORLEVEL 1 (
SET /A ERRORCOUNT=ERRORCOUNT+1
) ELSE (
SET /A OK=OK+1
)
ECHO/ >> logtemp.txt
ECHO ** EXIT %LEVEL% *******************************
Second sample:
if 1==1 (
call set level=%%errorlevel%%
call echo %%level%%
call echo %%errorlevel%%
echo %errorlevel%
)
Or with delayed expansion
setlocal EnableDelayedExpansion
if 1==1 (
set level=!errorlevel!
echo !level! !errorlevel!
)
ERRORLEVEL and %ERRORLEVEL% are not the same (see http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx).
The line
IF ERRORLEVEL 1
should be
IF %ERRORLEVEL% EQU 1
to yield the desire answer.
I have written following code
setlocal
set /A sample =1
:first
type C:\test.txt | find "inserted"
if %ERRORLEVEL% EQU 0 goto test
if %ERRORLEVEL% EQU 1 goto exam
:test
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1
if %sample% LEQ 4 goto first
:exam
echo "exam loop" >> C:\examloop.txt
endlocal
but it is going to lable "exam" even though error level is not equal to "1" plz help me
Your problem isn't goto, its that errorlevel requires special treatment, it's not like an ordinary environment variable. The only test you can do with errorlevel is to test whether it is greater than or equal to value.
so you have to test errorlevel values from highest to lowest because if errorlevel 1
then if errorlevel 1 will be true, but if errorlevel 0 will also be true
setlocal
set /A sample =1
:first
type C:\test.txt | find "inserted"
if errorlevel 1 goto exam
if errorlevel 0 goto test
:test
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1
if %sample% LEQ 4 goto first
:exam
echo "exam loop" >> C:\examloop.txt
endlocal
if you have command extensions enabled, and there is no environment variable called ERRORLEVEL (case insensitive). Then in theory you can use %ERRORLEVEL% like an ordinary environment variable. So this should also work
setlocal EnableExtensions
set /A sample =1
:first
type C:\test.txt | find "inserted"
if %errorlevel% EQU 1 goto exam
if %errorlevel% EQU 0 goto test
:test
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1
if %sample% LEQ 4 goto first
:exam
echo "exam loop" >> C:\examloop.txt
You need to list the error levels in descending order (errorlevel2, errorlevel1, errorlevel0...).
See this explanation and example.
You may want to consider using ERRORLEVEL as direct branching as follows:
setlocal
set /A sample =1
:first
type C:\test.txt | find "inserted"
**goto :Branch%ERRORLEVEL%**
:Branch0
echo "testloop" >> C:\testloop.txt
set /A sample = %sample% + 1
if %sample% LEQ 4 goto first
:Branch1
echo "exam loop" >> C:\examloop.txt
endlocal
May be use || instead of errorlevel for branching.
setlocal
set /a sample=1
:first
(Type c:\test.txt | find "inserted" >> c:\testloop.txt) || goto :branch1
set /a sample+=1
If %sample% leq 4 goto :first
:brabch1
Echo "exam loop" >> c:\examloop.txt
More simple way to use for loop.
For /l %%a in (1,1,4) do (
(Type c:\test.txt | find “inserted” >> c:\testloop.txt) || goto :done
)
:done
Echo “exam loop” >> c:\examloop.txt
Goto :eof