Computer Hope

Microsoft => Microsoft DOS => Topic started by: xcharge50 on February 15, 2009, 08:04:07 PM

Title: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 08:04:07 PM
I know this project look stupid, but it do help user who don't know the ipconfig. Here is my problem.

1. I have bug in my batch, since some command fail an don't go to the error page.

2. (I don't expect much in this one  :)) Since windows xp don't come with choice, I insert choice.com an choice.exe in the same folder but, I want to set choice.com as default to run in the same folder of the batch (for max compatibility) but if the batch encounter a 64 bit xp, vista (won't work with choice.com I think) it will run choice.exe

3. Feel free to improve my code  :) Maybe is going to be a open source batch  ;D

p.s. Sorry for my bad English! an feel free to use but lets me have some credit  ;)

Code: [Select]
:: turn echo off an change color
@echo off
color 1f

:: Main page
:start
cls
echo ----------------------------------------------------------------------
echo  ____     __                                         ______   ____
echo /\  _`\  /\ \                                       /\__  _\ /\  _`\
echo \ \ \/\_\\ \ \___      __     _ __    __      __    \/_/\ \/ \ \ \L\ \
echo  \ \ \/_/_\ \  _ `\  /'__`\  /\`'__\/'_ `\  /'__`\     \ \ \  \ \ ,__/
echo   \ \ \L\ \\ \ \ \ \/\ \L\.\_\ \ \//\ \L\ \/\  __/      \_\ \__\ \ \/
echo    \ \____/ \ \_\ \_\ \__/.\_\\ \_\\ \____ \ \____\     /\_____\\ \_\
echo     \/___/   \/_/\/_/\/__/\/_/ \/_/ \/___L\ \/____/     \/_____/ \/_/
echo                                       /\____/
echo                                       \_/__/
echo.---------------------------------------------------- Make by: xcharge50
echo.
echo Main menu
echo =========
echo 1.Simple mode
echo 2.Pro mode (show all info)
echo 3.Winsock reset (Run as admin + use if option 1 or 2 don't work)
echo 4.Special thanks!
echo 5.Quit  to command prompt
echo 6.Quit
echo.
echo (1, 2, 3, 4, 5 or 6)
choice /n /c:654321
if errorlevel 6 goto simple
if errorlevel 5 goto pro
if errorlevel 4 goto winsock
if errorlevel 3 goto thx
if errorlevel 2 goto procmd
if errorlevel 1 goto quit

:: Simple. Ask to run ip release (1 option)
:simple
cls
echo Do you want to run Ip Release (recomended)
echo.
echo you need to right click on you icon
echo an run as administrator
echo to run this function
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto cren
if errorlevel 1 goto release

:: Run ip release
:release
cls
echo Ip is gona be release in 5 sec
echo (If operation take more than 45 sec close the windows)
ping localhost -n 6 1>NUL 2>&1
echo.
ipconfig /release 1>NUL 2>&1
if errorlevel 1 goto norel
echo.
echo.----------
echo Ip Release
echo.----------
echo.
pause

:: Ask to run ip renew
:cren
cls
echo Do you want to run Ip Renew (The repair)
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto start
if errorlevel 1 goto renew

:: Run ip renew
:renew
cls
echo Ip is gona be renew in 5 sec
echo (If operation take more than 45 sec close the windows)
echo.
ping localhost -n 6 1>NUL 2>&1
ipconfig /renew 1>NUL 2>&1
if errorlevel 1 goto noren
echo.
echo.--------
echo Ip Renew
echo.--------
echo.
echo you ip is supposed to be ok
echo.
pause
goto start

:: If ip release fail ask
:norel
cls
echo ------------------------------
echo Ip release fail!
echo ------------------------------
echo Did you run as administrator?
echo The option release is optional
echo.
echo Do you want to run Ip Renew instead? (Y ot N)
echo or press R to retry Ip release
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto start
if errorlevel 2 goto renew
if errorlevel 1 goto release

:: If ip renew fail ask
:noren
cls
echo ------------------------------
echo Ip renew fail!
echo ------------------------------
echo For some reason Ip renew fail!
echo (Cable unplug? No wireless signal? winsock corrupted?)
echo.
echo Do you want to try again? (Y or N)
echo.
echo (Y or N)
choice /n /c:yr
if errorlevel 2 goto start
if errorlevel 1 goto renew

:: Pro. Ask to run ip release (Option 2)
:pro
echo.
echo Do you want to run Ip Release
echo You may need to run as administrator
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto crenpro
if errorlevel 1 goto releasepro

:: Pr. Run ip release
:releasepro
echo.
@echo on
ipconfig /release
@echo off
if errorlevel 1 goto norelpro
pause
echo.
echo.----------
echo Ip Release
echo.----------

:: Pr. Run ip renew
:crenpro
echo.
echo Do you want to run Ip Renew
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto start
if errorlevel 1 goto renewpro

:: Pro. Run ip renew
:renewpro
echo.
@echo on
ipconfig /renew
@echo off
if errorlevel 1 goto norenpro
pause
echo.
echo.--------
echo Ip Renew
echo.--------
echo.
echo you ip is supposed to be ok
goto quitpro

:: Pro. If ip release fail ask
:norelpro
echo.
echo ------------------------------
echo Ip release fail!
echo ------------------------------
echo Did you run as administrator?
echo.
echo Do you want to run Ip Renew instead?
echo or press R to retry Ip release
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto start
if errorlevel 2 goto renewpro
if errorlevel 1 goto releasepro

:: Pro. If ip renew fail ask
:norenpro
echo.
echo ------------------------------
echo Ip renew fail!
echo ------------------------------
echo For some reason Ip renew fail!
echo (Maybe) a winsock repair can solve this problem
echo.
echo Do you want to try again?
echo or press R to restart the program
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto quitpro
if errorlevel 2 goto renewpro
if errorlevel 1 goto start

:: Winsock reset
:winsock
cls
echo windsock is going to be reset!
pause
@echo on
netsh winsock reset
@echo off
if errorlevel 1 goto winfail
echo windsock reset!
pause
goto start

:: If winsock reset fail
:winfail
echo.
echo ------------------------------
echo Winsock fail!
echo ------------------------------
echo For some reason Winsock fail!
echo did you run in admin?
echo if you did = bad news!
echo.
echo Do you want to try again?
echo (Y or N)
echo.
choice /n /c:ny
if errorlevel 2 goto winsock
if errorlevel 1 goto start

:: Special thanks
:thx
cls
echo ================
echo Special thanks!
echo ================
echo.
echo http://www.computerhope.com
echo.
echo an to the following member:
echo.
echo Sidewinder
echo BatchFileCommand
echo.
echo press any key to go back to main menu
pause 1>NUL 2>&1
goto start

:: Pro. ask Exit
:quitpro
echo Do you want to (G)o to command prompt or (C)lose all?
echo or press M to go back in main menu
echo.
echo (G, C, or M)
echo.
choice /n /c:mcg
if errorlevel 3 goto procmd
if errorlevel 2 goto quit
if errorlevel 1 goto start

:: Exit pro
:procmd
echo.
ipconfig /?
echo ------------------------
echo type exit to quit cmd
echo or ChargeIp to go back
echo ------------------------
cmd

:: exit program (3 option)
:quit
exit

Title: Re: Help me with a batch file
Post by: BatchFileCommand on February 15, 2009, 08:07:58 PM
Could you clarify your problem more thoroughly?
Title: Re: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 08:12:31 PM
I think my error level are wrong in the release an renew section since they failing but they still jump on the line saying it was a succes. But I want to make them jump in the line telling the command fail. Anyway is more for improving purpose than helping purpose i think  :P

but for the helping part is more about the nul>2&1 if my syntax was correct or my error level.
Title: Re: Help me with a batch file
Post by: BC_Programmer on February 15, 2009, 08:14:55 PM
yes, why all the 1's and 2's and ampersands? Why not just:
Code: [Select]
Pause > NUL

Just a redirection, rather then try to send to output to the number 1, whatever that could mean.
Title: Re: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 08:23:09 PM
Well is my second batch file i made an im not even sure why I insert the 1 inside. My first 1 was hello world. But im a newby at batch an I though adding this void will redirect any error (also hide the error message) to the error level or stop making a void file in my folder. Is more experimenting an im trying to learn.  ??? Sometime I'm not too sure myself what I add in my code =_='. Anyway I have posting this for feedback so it will help me to learn more about batch. p.s. for me making this useless batch file was kinda hard but I try to make it right.
Title: Re: Help me with a batch file
Post by: BC_Programmer on February 15, 2009, 08:27:23 PM
ok, so you can probably replace them with a single redirection to NUL, without the 1's and 2's, - the redirection to NUL should eliminate the display of any error. In fact- the ampersand executes another command and in this case your executing "1"- this is likely to change the errorlevel, as well.
Title: Re: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 08:36:28 PM
ok, so you can probably replace them with a single redirection to NUL, without the 1's and 2's, - the redirection to NUL should eliminate the display of any error. In fact- the ampersand executes another command and in this case your executing "1"- this is likely to change the errorlevel, as well.

Thank you very much! I think it solve the errorlevel (or most) bug in it (I also need more testing to be sure...) Anyway here the update version. My objective is to make a installer to help people without knowledge to run ipconfig an also for myself  :P (Im to lazy to run cms in run). Also i want to make it open an make this batch better.

Code: [Select]
:: turn echo off an change color
@echo off
color 1f

:: Main page
:start
cls
echo ----------------------------------------------------------------------
echo  ____     __                                         ______   ____
echo /\  _`\  /\ \                                       /\__  _\ /\  _`\
echo \ \ \/\_\\ \ \___      __     _ __    __      __    \/_/\ \/ \ \ \L\ \
echo  \ \ \/_/_\ \  _ `\  /'__`\  /\`'__\/'_ `\  /'__`\     \ \ \  \ \ ,__/
echo   \ \ \L\ \\ \ \ \ \/\ \L\.\_\ \ \//\ \L\ \/\  __/      \_\ \__\ \ \/
echo    \ \____/ \ \_\ \_\ \__/.\_\\ \_\\ \____ \ \____\     /\_____\\ \_\
echo     \/___/   \/_/\/_/\/__/\/_/ \/_/ \/___L\ \/____/     \/_____/ \/_/
echo                                       /\____/
echo                                       \_/__/
echo.---------------------------------------------------- Make by: xcharge50
echo.
echo Main menu
echo =========
echo 1.Simple mode
echo 2.Pro mode (show all info)
echo 3.Winsock reset (Run as admin + use if option 1 or 2 don't work)
echo 4.Special thanks!
echo 5.Quit  to command prompt
echo 6.Quit
echo.
echo (1, 2, 3, 4, 5 or 6)
choice /n /c:654321
if errorlevel 6 goto simple
if errorlevel 5 goto pro
if errorlevel 4 goto winsock
if errorlevel 3 goto thx
if errorlevel 2 goto procmd
if errorlevel 1 goto quit

:: Simple. Ask to run ip release (1 option)
:simple
cls
echo Do you want to run Ip Release (recomended)
echo.
echo you need to right click on you icon
echo an run as administrator
echo to run this function
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto cren
if errorlevel 1 goto release

:: Run ip release
:release
cls
echo Ip is gona be release in 5 sec
echo (If operation take more than 45 sec close the windows)
ping localhost -n 6 >NUL
echo.
ipconfig /release >NUL
if errorlevel 1 goto norel
echo.
echo.----------
echo Ip Release
echo.----------
echo.
pause

:: Ask to run ip renew
:cren
cls
echo Do you want to run Ip Renew (The repair)
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto start
if errorlevel 1 goto renew

:: Run ip renew
:renew
cls
echo Ip is gona be renew in 5 sec
echo (If operation take more than 45 sec close the windows)
echo.
ping localhost -n 6 >NUL
ipconfig /renew >NUL
if errorlevel 1 goto noren
echo.
echo.--------
echo Ip Renew
echo.--------
echo.
echo you ip is supposed to be ok
echo.
pause
goto start

:: If ip release fail ask
:norel
cls
echo ------------------------------
echo Ip release fail!
echo ------------------------------
echo Did you run as administrator?
echo The option release is optional
echo.
echo Do you want to run Ip Renew instead? (Y ot N)
echo or press R to retry Ip release
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto start
if errorlevel 2 goto renew
if errorlevel 1 goto release

:: If ip renew fail ask
:noren
cls
echo ------------------------------
echo Ip renew fail!
echo ------------------------------
echo For some reason Ip renew fail!
echo (Cable unplug? No wireless signal? winsock corrupted?)
echo.
echo Do you want to try again? (Y or N)
echo.
echo (Y or N)
choice /n /c:yr
if errorlevel 2 goto start
if errorlevel 1 goto renew

:: Pro. Ask to run ip release (Option 2)
:pro
echo.
echo Do you want to run Ip Release
echo You may need to run as administrator
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto crenpro
if errorlevel 1 goto releasepro

:: Pr. Run ip release
:releasepro
echo.
@echo on
ipconfig /release
@echo off
if errorlevel 1 goto norelpro
pause
echo.
echo.----------
echo Ip Release
echo.----------

:: Pr. Run ip renew
:crenpro
echo.
echo Do you want to run Ip Renew
echo.
echo (Y or N)
choice /n /c:yn
if errorlevel 2 goto start
if errorlevel 1 goto renewpro

:: Pro. Run ip renew
:renewpro
echo.
@echo on
ipconfig /renew
@echo off
if errorlevel 1 goto norenpro
pause
echo.
echo.--------
echo Ip Renew
echo.--------
echo.
echo you ip is supposed to be ok
goto quitpro

:: Pro. If ip release fail ask
:norelpro
echo.
echo ------------------------------
echo Ip release fail!
echo ------------------------------
echo Did you run as administrator?
echo.
echo Do you want to run Ip Renew instead?
echo or press R to retry Ip release
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto start
if errorlevel 2 goto renewpro
if errorlevel 1 goto releasepro

:: Pro. If ip renew fail ask
:norenpro
echo.
echo ------------------------------
echo Ip renew fail!
echo ------------------------------
echo For some reason Ip renew fail!
echo (Maybe) a winsock repair can solve this problem
echo.
echo Do you want to try again?
echo or press R to restart the program
echo.
echo (Y, N or R)
choice /n /c:ryn
if errorlevel 3 goto quitpro
if errorlevel 2 goto renewpro
if errorlevel 1 goto start

:: Winsock reset
:winsock
cls
echo windsock is going to be reset!
pause
@echo on
netsh winsock reset
@echo off
if errorlevel 1 goto winfail
echo windsock reset!
pause
goto start

:: If winsock reset fail
:winfail
echo.
echo ------------------------------
echo Winsock fail!
echo ------------------------------
echo For some reason Winsock fail!
echo did you run in admin?
echo if you did = bad news!
echo.
echo Do you want to try again?
echo (Y or N)
echo.
choice /n /c:ny
if errorlevel 2 goto winsock
if errorlevel 1 goto start

:: Special thanks
:thx
cls
echo ================
echo Special thanks!
echo ================
echo.
echo http://www.computerhope.com
echo.
echo an to the following member:
echo.
echo Sidewinder
echo BatchFileCommand
echo BC_Programmer
echo.
echo press any key to go back to main menu
pause >NUL
goto start

:: Pro. ask Exit
:quitpro
echo Do you want to (G)o to command prompt or (C)lose all?
echo or press M to go back in main menu
echo.
echo (G, C, or M)
echo.
choice /n /c:mcg
if errorlevel 3 goto procmd
if errorlevel 2 goto quit
if errorlevel 1 goto start

:: Exit pro
:procmd
echo.
ipconfig /?
echo ------------------------
echo type exit to quit cmd
echo or ChargeIp to go back
echo ------------------------
cmd

:: exit program (3 option)
:quit
exit
Title: Re: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 08:40:19 PM
anyway I also need to master the <<if>> combined to the <<ver|find>> To check the os version an run the appropriate choice. (Gona be a challenge for a newbie like me  :o)
Title: Re: Help me with a batch file
Post by: BC_Programmer on February 15, 2009, 08:52:00 PM
hmm, I suppose that could be done using

Code: [Select]
ver | find "98" > NUL
if errorlevel EQU 0 goto WIN98
ver | find "XP" > NUL
if errorlevel EQU 0 goto WinXP

etcetera...

a more robust method would be to, instead of search for key strings like 98 and XP, to search for entire version numbers. in the case of windows 98, "4.10" would find both 4.10.1998 (First edition) and 4.10.2222 (Second edition). Additional numbers (for your info)  :)

http://support.microsoft.com/kb/158238


good luck!
Title: Re: Help me with a batch file
Post by: xcharge50 on February 15, 2009, 09:05:54 PM
I guess is google time  ;D Im still going check this tread if some people post they feedback or subjection- what to add or what to remove. Until then I do some search an make modification to my code an repost it someday day. I also need to learn how to force the batch to use choice.exe or .com in the same folder of is location.
Title: Re: Help me with a batch file
Post by: BC_Programmer on February 15, 2009, 09:08:41 PM
if you want to force usage in the current directory, you can specify the current directory using "."

IE:

Code: [Select]
.\choice.com
Title: Re: Help me with a batch file
Post by: Helpmeh on February 16, 2009, 08:45:24 AM
Just a question...why do you NEED choice? For the code, couldn't you just use set /p???
Title: Re: Help me with a batch file
Post by: BatchFileCommand on February 16, 2009, 08:47:58 AM
set /p is so much easier to use. Choice is a waste of time in my opinion. You have to get all the errorlevels right. It's just a mess.
Title: Re: Help me with a batch file
Post by: BC_Programmer on February 16, 2009, 09:08:41 AM
If he's examining windows versions, there is a chance he wants the batch to run on windows 98, in which case SET /P is not available.
Title: Re: Help me with a batch file
Post by: xcharge50 on February 16, 2009, 09:20:00 AM
Because for me choice is more compatible with older os (maybe i can be wrong!) Because I actually don't know if the set, set /p an if/else command are working on older os. Can windows 3.1 handle it? Dos? Anyway my batch is getting outdated thx to BC_Programmer tip  ;) After making a batch that will work with windows command, i'm gona  attempt to make a standalone batch to show how much is powerful. I think the set /p command was introduce in the nt family (I can still be wrong). Im gona attempt to make a batch file that will run in many os! (is stupid since each os got is own command, but the main purpose of this batch is to learn an attempt to make it fail less)

Also i need to find if I can insert all the required command in a installer an deploy it without copyright trouble (or insert open command, like freedos or else)

Example it will use the build it command if possible (for better compatibility) or use it own command. (maybe it will work in the first version of dos  ;D)

(Don't trust my information because this is what I think! If im wrong (example about the choice, ect....compatibility) let me know since I want to learn)
Title: Re: Help me with a batch file
Post by: xcharge50 on February 16, 2009, 09:21:22 AM
If he's examining windows versions, there is a chance he wants the batch to run on windows 98, in which case SET /P is not available.

You right! In other word : I'm attempting to make my batch the more compatible possible  :)
Title: Re: Help me with a batch file
Post by: GuruGary on February 16, 2009, 11:42:23 AM
If you are making your script backwards compatible with Win9x, then be sure to modify your IPCONFIG commands because the Win9x version of IPCONFIG is very different from the Windows 2000 / Windows XP version of IPCONFIG
Code: [Select]
C:\Win98>ipconfig /?
Windows 98 IP Configuration
Command line options:
 /All - Display detailed information.
 /Batch [file] - Write to file or ./WINIPCFG.OUT
 /renew_all    - Renew   all adapters.
 /release_all  - Release all adapters.
 /renew   N    - Renew   adapter N.
 /release N    - Release adapter N.

Code: [Select]
C:\WindowsXP>ipconfig /?
USAGE:
    ipconfig [/? | /all | /renew [adapter] | /release [adapter] |
              /flushdns | /displaydns | /registerdns |
              /showclassid adapter |
              /setclassid adapter [classid] ]

where
    adapter         Connection name
                   (wildcard characters * and ? allowed, see examples)

    Options:
       /?           Display this help message
       /all         Display full configuration information.
       /release     Release the IP address for the specified adapter.
       /renew       Renew the IP address for the specified adapter.
       /flushdns    Purges the DNS Resolver cache.
       /registerdns Refreshes all DHCP leases and re-registers DNS names
       /displaydns  Display the contents of the DNS Resolver Cache.
       /showclassid Displays all the dhcp class IDs allowed for adapter.
       /setclassid  Modifies the dhcp class id.

The default is to display only the IP address, subnet mask and
default gateway for each adapter bound to TCP/IP.

For Release and Renew, if no adapter name is specified, then the IP address
leases for all adapters bound to TCP/IP will be released or renewed.

For Setclassid, if no ClassId is specified, then the ClassId is removed.

Examples:
    > ipconfig                   ... Show information.
    > ipconfig /all              ... Show detailed information
    > ipconfig /renew            ... renew all adapters
    > ipconfig /renew EL*        ... renew any connection that has its
                                     name starting with EL
    > ipconfig /release *Con*    ... release all matching connections,
                                     eg. "Local Area Connection 1" or
                                         "Local Area Connection 2"
Title: Re: Help me with a batch file
Post by: xcharge50 on February 16, 2009, 01:43:02 PM
Thx for the notice GuruGary  :)  I will make some modification to my script.