what am i doing wrong (if, errorlevel, ren)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
linzzer
Posts: 3
Joined: 13 Jul 2020 04:11

what am i doing wrong (if, errorlevel, ren)

#1 Post by linzzer » 13 Jul 2020 04:29

this should be so easy, yet I'm super lost. What am i doing wrong? :S
why is the errorlevel code being 50% ignored?

https://i.postimg.cc/SsCfTgmB/zinger.png
https://i.postimg.cc/5ymq1jYp/Clipboard02f.png

Code: Select all

if exist zzzplaces.sqlite ren places.sqlite zpirplaces.sqlite
echo %errorlevel%
::pause
SAME RESULT if not errorlevel 0 goto :ren1 // if errorlevel 0 goto :ren1
if errorlevel 1 goto :ren2

:ren1
ren zzzplaces.sqlite places.sqlite
goto end

::if exist zpirplaces.sqlite ren places.sqlite zzzplaces.sqlite
::if errorlevel 0 goto :ren2

:ren2
ren places.sqlite zzzplaces.sqlite
ren zpirplaces.sqlite places.sqlite

:end

pause

penpen
Expert
Posts: 1846
Joined: 23 Jun 2013 06:15
Location: Germany

Re: what am i doing wrong (if, errorlevel, ren)

#2 Post by penpen » 13 Jul 2020 05:04

linzzer wrote:
13 Jul 2020 04:29
this should be so easy, yet I'm super lost. What am i doing wrong? :S
why is the errorlevel code being 50% ignored?
All is working as intended (by Microsoft).
The command "if errorlevel <number>" specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number.
Here you seeem to have changed your code:

Code: Select all

:: from
if errorlevel  0 goto :ren1
if errorlevel  1 goto :ren2

:: to
if errorlevel  1 goto :ren1
if errorlevel  0 goto :ren2
Herre the errorlevel changed from '1' to '0'.


penpen

linzzer
Posts: 3
Joined: 13 Jul 2020 04:11

Re: what am i doing wrong (if, errorlevel, ren)

#3 Post by linzzer » 13 Jul 2020 07:15

>Here you seeem to have changed your code:

yea i know because I was changing things around. so how do I jump to :ren2 ? if one file exists?

penpen
Expert
Posts: 1846
Joined: 23 Jun 2013 06:15
Location: Germany

Re: what am i doing wrong (if, errorlevel, ren)

#4 Post by penpen » 14 Jul 2020 02:10

You could do the following:

Code: Select all

if exist "filename" goto :ren2

penpen

linzzer
Posts: 3
Joined: 13 Jul 2020 04:11

Re: what am i doing wrong (if, errorlevel, ren)

#5 Post by linzzer » 14 Jul 2020 15:35

yea, that's what I ended up doing.

thanks :)

Post Reply