1.

Solve : problem with writting batch file?

Answer»

Hello, i have written batch file but it doesn't work properly. I put here only a part of it, where in my opinion the problem might be. So when the batch file run in prompt line and I get the QUESTION "Would you like it to delete and create new ONE?", I press N and then I get the message "File name.txt is not deleted." and in the next line shows "File name.txt is deleted." I don't understand why it goes first to RUBBER and then go to BEGINNING instead of going only to BEGINNING. So what is the problem? I appreciate help very much...

:error2
echo %1 already exists.
echo.
echo Would you like it to delete and create new one?
echo.
echo If yes, press T, if no, press N.
choice /C:TN
if ERRORLEVEL 2 echo File %1 is not deleted.
if errorlevel 1 goto RUBBER
goto beginning

:RUBBER
del /F /Q %1
if not %ERRORLEVEL% == 0 goto error3
echo File %1 is deleted.
goto beginningYou need to study the way the old MS-DOS if errorlevel test works. It is different from if %errorlevel%==%something% (which came first with Windows 2000) You have mixed these 2 different syntaxes.

if errorlevel X [do something] means do something if the errorlevel is equal to or greater than X.

When you hit N choice.exe returns an errorlevel of 2 (because N is choice number 2). "if errorlevel 2" checks for an errorlevel of 2 or greater so the test "if errorlevel 2" is satisfied, therefore "echo File %1 is deleted" is executed. Next, the batch goes to the next line and because 2 is greater than 1 the command "goto RUBBER" is executed.

So use "if errorlevel 2 goto SOMEWHERE".



THANK you. If you let me I ask you for more tips. In the part of RUBBER if file can not be deleted (for example another program uses it) I have written line "if not %ERRORLEVEL% == 0 goto error3", is it right or is there any better solutions for that problem?
About error levels I have read but actually haven't understand well, especially about %errorlevel%Actually I understand what you wrote about errorlevel but before I didn't find very much information about errorlevels.Quote from: infogirl on March 06, 2011, 08:23:42 AM

if file can not be deleted (for example another program uses it) I have written line "if not %ERRORLEVEL% == 0 goto error3", is it right or is there any better solutions for that problem?

If you use the %errorlevel% syntax you can do this (which I prefer)

if %errorlevel% neq 0 goto error3






Discussion

No Comment Found