Batch Script to replace line in an .ini - batch-file

Please excuse me. I'm a complete noob.
I have 1 ini file with only 1 IP address and a version number.in this format:
xxx.xxx.xxx.xxx
abcde
I have 3 total IP's that I need to accommodate.
I'm looking to create a batch script that will ask a user for a location they'd like to connect to (each location would correspond to an IP address).
So for example: NY = 10.0.0.0 DC = 20.0.0.0 LA = 30.0.0.0
The batch would say "Where do you want to connect? 1= NY, 2= DC, 3= LA"
When a user selects 1 for "NY", the script will search the .ini file (which is always in the same location c:\sample) and change the IP to the correct one (10.0.0.0).
It would have an output of something like "You're now connected to NY!"
When a user selects "DC", the script will search the .ini file and change it to 20.0.0.0 etc.I'm able to do a simple find/replace script, but only with 2 IPs, and I had to make one for each location which was rather inconvenient.
Any help or guidance is much appreciated!

This example will:
Ask user to enter NY, DC or LA;
Create x.tmp which is a copy of x.ini, replacing all xxx.xxx... with argument;
Replace exisitng x.ini with x.tmp;
Code:
#Echo Off
:Begin
If Exist c:\sample\x.tmp Del c:\sample\x.tmp
Set /P "var=Choose location (NY, DC, LA):"
If /I "%var%"=="NY" Call :ReplaceIP 10.0.0.0
If /I "%var%"=="DC" Call :ReplaceIP 20.0.0.0
If /I "%var%"=="LA" Call :ReplaceIP 30.0.0.0
If Exist c:\sample\x.tmp (
Move /Y c:\sample\x.tmp c:\sample\x.ini 1>Nul
Echo Success!
) Else (
Echo Invalid option!
)
Pause
GoTo :Begin
:ReplaceIP
For /F "Tokens=1,2,3,4 Delims=." %%i In (c:\sample\x.ini) do If %%j.==. (
Echo %%i >> c:\sample\x.tmp
) Else (
Echo %1 >> c:\sample\x.tmp
)
GoTo :EOF

This should help you out.
#echo off
set "var="
set /p "var=Enter 1 for NY, 2 for DC or 3 for LA"
if "%var%" EQU 1 (
>c:\sample\file.ini echo 10.0.0.0
>>c:\sample\file.ini abcde
)
if "%var%" EQU 2 (
>c:\sample\file.ini echo 20.0.0.0
>>c:\sample\file.ini abcde
)
if "%var%" EQU 3 (
>c:\sample\file.ini echo 30.0.0.0
>>c:\sample\file.ini abcde
)
echo Good luck - I hope you entered the right number! :)

Related

How can i copy selected text file using command "FOR" in batch file?

Hello guys as you read in topic how can i make a selected file to copy to another location.
Im using my own Lithuanian language in this, if some one find it hard to understand tell me ill translate it. Right now it only creates new file when i start " name.bat test.txt" its not full code since there is lots of copy paste of same thing any ideas since im realy bad at .bat files.
And i need to use command "FOR"
:DEKUPAZAS
echo Pasirinkote Dekupazo kursus
echo Bendrine informacija apie si bureli
echo.
type dekupazas.txt
echo.
set U=dekupazas.txt
echo Ar norite:
echo 1) Irasyti sia informacija i naujai sukurta faila?
echo 2) Grizti kitu bureliu pasirinkimu?
echo 3) Grizti prie srities pasirinkimo?
choice /C 123 /N /M "Iveskite savo pasirinkima(1,2 arba 3): "
if errorlevel 255 goto KLAIDA
if errorlevel 3 goto SRITIS
if errorlevel 2 goto KT
if errorlevel 1 goto IRASYMAS
goto PRADZIA
:IRASYMAS
echo Pasirinkote irasyti sia informacija i jusu sukurto pavadinimo faila
echo Failas bus perkeltas i aplanka Pasirinkimas
if exist Pasirinkimas rd /S /Q Pasirinkimas
md Pasirinkimas
for %%U in (U) do dir %%U >>%1
move %1 Pasirinkimas >nul
goto PAB
The FOR /F statement reads each line of the file then does a directory listing of it captured to the first parameter to the .bat script.
I also took the liberty of creating a variable for the new directory name so that it does not need to be hardcoded so many times. "Hardcode" is two, four-letter words.
echo Pasirinkote irasyti sia informacija i jusu sukurto pavadinimo faila
echo Failas bus perkeltas i aplanka Pasirinkimas
SET NEW_DIR=Pasirinkimas
if exist "%NEW_DIR%" (RMDIR /S /Q "%NEW_DIR%")
MKDIR "%NEW_DIR%"
for /F %%a in (%U%) DO (DIR "%%~a" >>"%~1")
move "%~1" "%NEW_DIR%" >nul
goto PAB

What is wrong with goto command in for loop?

I have a list of computers that I want to make some things according to tcp connection status.
I'm trying to check tcp connection and if errorlog is "1" so write line to log and skip to next computer.
The problem is that when a computer has no tcp connection the goto skip_action command takes the script to the end and exit and the other computers in the list left unprocessed.
I have also tried to use goto :eof and it terminates the script unexpected.
ipst.txt file:
1 10.1.1.10
3 10.1.3.10
8 10.1.3.10
This is the batch file code:
#echo off
setlocal enabledelayedexpansion
set computerslist=ipst.txt
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
goto skip_action
)
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
:skip_action
echo end of working on !Station_Num!
)
echo end of script
The problem here is that GOTO cancels the for loop.
But you can simply enclose your action in an ELSE block
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
) Else (
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
)
echo end of working on !Station_Num!
)
General form of if - else statement
If condition (
Statements
) else (
Statements to be executed if condition is not met
)
Make your call from the loop
You can have a function ping .. and parse variable references to it .. the preceding code could also be rendered as %%a and %%b being the first and second argument respectively -- in your function call ...
Call :pingcomputers %%a %%b
This way you avoid set altogether
:pingcomputers
Rem Pinging computer %~1
Ping %~2 ¦ find "reply"
If errorlevel 1 goto failed
(Actions to be performed if ping wa successful)
Assuming that your script has a label failed and actions to.be performed if error level is not 0 ..
Not much of a script but hope it helps ...

(Windows Batch File) IF statement inside FOR LOOP

I'm trying to write manual document validation part of my program. It's basically opening all the pdf documents one by one in the same folder. When its open i would like to echo few possibilities for user. Here starts the problem. I have around 180 possible choices. I was thinking to ask for the first letter of choice. Then it will echo all choices with started with X letter and user has to simply enter the number of this choice. So for example we have :
1. Asomething
2. Asomename
3. Asomenametoo
4. Bname
5. Bname 2
6. Bname 3
I want user to choose first letter and print possible choices. When the choice is made program should add some string to txt file with the same name in the same folder. Here i have a problem with IF statement inside FOR loop. I wanted to use goto but i can't do it inside FOR loop.
I can set up all the strings for each number before. For example : When you choose 1 it will add SomeString to txt. It's important to use choice option to avoid any typo's. Does anybody knows any other way to do this inside FOR loop ?
CODE:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
I'm having some trouble understanding the problem you're having, but it looks like all of the statements following the IF should all be conditions of the IF, not just the ECHO statement. For that, you can put the entire block in parentheses like this:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" (
ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
) else (
goto :EOF REM Just an example of else
)
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
Were you having some problem using goto in the FOR loop?

In batch, How can i create an IF statement that checks if a variable includes a specific string or data?

I want to make a basic pinger in batch as my learning project and i came into a problem.Here is the code:
#echo off
color B
title Pinger v1.0
:OK
echo.
echo.
set /p t=Target I.P.:
echo.
echo.
echo This is not an IP address!
echo.
goto OK
set /p a=Packet size:
echo.
:start
set ifer=
set /p ifer=Start Ping (y/n):
if %ifer%==y goto 8
if %ifer%==Y goto 8
if %ifer%==n goto OK
if %ifer%==N goto OK
:8
echo.
echo.
echo.
echo.
ping %t% -t -l %a%
My question is, How can i check if t contains a valid IP address an not some random data?
and also, how can i make a code that translates a web address to an ip address(Don't have to answer this though)
I don't know exactly what you're trying to get at here. I made a program that will take your IP or host name and ping it with a user determined packet size, and amount of times to ping. I only tried 1.1.1.1 and www.google.com, but it should work for anything else.
#echo off
#title Pinger v1.1
color B
setlocal EnableDelayedExpansion
:ipInput
cls
set /p tryIP=Target IP:
:: pinging ip once (-n 1) with one byte of data (-l 1) to check if it's valid
ping -n 1 -l 1 !tryIP! >nul & if errorlevel 1 (
echo IP: !tryIP! is invalid, try correcting your input or using a different IP
pause
goto ipInput
)
cls
echo IP: !tryIP! is Valid
set /p pingCount=Enter number of pings:
set /p packets=Enter packet size:
echo Pinging !tryIP! with !packets! bytes for !pingCount! time^(s^)
:: You can either use this line (beneath)
REM ping -n !pingCount! -l !packets! !tryIP!
:: Or you can use this for loop from here.........
:: for /l indicates type of operation in this case it will take (start at, add or
subtract by, end at)
:: %%a in this case, based on /l will always equal the number
:: of times it's looped through..
:: (starting at 1, moving up by 1 each time, and ending after 32)
for /l %%a in (1,1,%pingCount%) do (
ping -n 1 -l !packets! !tryIP! >nul
cls
echo Pinging !tryIP! with !packets! bytes for !pingCount! time^(s^)
set /a percentage=%%a*10000/!pingCount!
if !percentage! LSS 100 echo ......!percentage:~0,0!.!percentage:~0,2!%%
if !percentage! GEQ 100 if !percentage! LSS 1000 echo ......!percentage:~0,1!.!percentage:~1,2!%%
if !percentage! GEQ 1000 echo ......!percentage:~0,2!.!percentage:~2,2!%%
)
:: the if statements are used to correct the formating on the percentages
:: since batch can't use floating point decimals,
:: you have to get a little creative sometimes.
:: dual % signs are so it doesn't think you're trying to set a variable
:: using ( or ) without that >>>^ (eg on lines 20 and 34)
:: will sometimes mess things up.
:: ........ to here. the benefit of a for loop is that you can see your progress,
:: and it's much much faster as the ping command will wait between pings.
echo Done
pause
goto ipInput

Multiple choices menu on batch file?

Hi I want to make a batch file menu, that asks 'Select app you want to install?' for example
App1
App2
App3
App4
App5
ALL Apps
Select what app:_
What I want is, for example I want to install App2, App3, and App5, so I can type on by App ID's 'Select what app:2,3,5' . And when user Select option 6, it will install all Applications!
I know this is possible on bash scripting, but Im not sure on batch scripting?
An example of batch menu is http://mintywhite.com/software-reviews/productivity-software/create-multiple-choice-menu-batchfile/
Answer
This will do what you want. Let me know if you have any questions. All you have to do is follow the two steps listed in the script.
Script
:: Hide Command and Set Scope
#echo off
setlocal EnableExtensions
:: Customize Window
title My Menu
:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=One"
set "App[2]=Two"
set "App[3]=Three"
set "App[4]=Four"
set "App[5]=Five"
set "App[6]=All Apps"
:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo. Menu Title
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
call echo %x%. %%App[%x%]%%
goto MenuLoop
)
echo.
:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"
:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%
:: Process Input
call :Process %Input%
goto End
:Validate
set "Next=%2"
if not defined App[%1] (
set "Message=Invalid Input: %1"
goto Menu
)
if defined Next shift & goto Validate
goto :eof
:Process
set "Next=%2"
call set "App=%%App[%1]%%"
:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "One" echo Run Install for App One here
if "%App%" EQU "Two" echo Run Install for App Two here
if "%App%" EQU "Three" echo Run Install for App Three here
if "%App%" EQU "Four" echo Run Install for App Four here
if "%App%" EQU "Five" echo Run Install for App Five here
if "%App%" EQU "All Apps" (
echo Run Install for All Apps here
)
:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof
:End
endlocal
pause >nul
you may use choice.exe see here : http://ss64.com/nt/choice.html
You want to use set /p Example below:
echo What would you like to install?
echo 1 - App1
echo 2 - App2
set /p whatapp=
if %whatapp%==1 (
codetoinstallapp1
) else if %whatapp%==2 (
codetoinstallapp2
) else (
echo invalid choice
)
Here's a trick I learned:
echo.1) first choice
echo.2) second choice
echo.3) third choice
echo.4) fourth choice
:: the choice command
set pass=
choice /c 1234 /n /m "Choose a task"
set pass=%errorlevel%
::the choices
if errorlevel 1 set goto=1
if errorlevel 2 set goto=2
if errorlevel 3 set goto=3
if errorlevel 4 set goto=4
goto %goto%
While I use only 1-4 it would be very easy to add more possible choices.
#echo off
:menu
cls
echo.
echo Select the case color you want to create:
echo ==========================================
echo.
echo App 1
echo App 2
echo App 3
echo App 4
echo.
echo ==========================================
echo Please answer Y/N to the following:
set /p App1= Install App 1?
set /p App2= Install App 2?
set /p App3= Install App 3?
set /p App4= Install App 4?
if /I "%App1%" EQU "Y" goto :Option-1
if /I "%App1%" EQU "N" goto :1
:1
if /I "%App2%" EQU "Y" goto :Option-2
if /I "%App2%" EQU "N" goto :2
:2
if /I "%App3%" EQU "Y" goto :Option-3
if /I "%App3%" EQU "N" goto :3
:3
if /I "%App4%" EQU "Y" goto :Option-4
if /I "%App4%" EQU "N" goto :End
:Option-1
App 1 Loc.
goto 1
:Option-2
App 2 Loc.
goto 2
:Option-3
App 3 Loc.
goto 2
:Option-4
App 4 Loc.
:End
Exit
Menu with analog of checkbox.
#echo off
set size=3
::preset
set chbox2=x
:prepare
for /L %%i in (0,1,%size%) do (
if defined chbox%%i (
set st%%i=Y
) else (
set chbox%%i=
)
)
:menu
cls
echo.
echo 1. [%chbox1%] name_1:
echo.
echo 2. [%chbox2%] name_2:
echo.
echo 3. [%chbox3%] name_3:
echo.
echo.
echo.
choice /C 1234567890qa /N /M "Select [1-9] >> [a]pply or [q]uit:"
echo.
set inp=%errorlevel%
if %inp%==11 (
exit
)
if %inp%==12 (
call :apply
)
::switch
if defined st%inp% (
set st%inp%=
set chbox%inp%=
) else (
set st%inp%=Y
set chbox%inp%=X
)
goto :menu
:apply
for /L %%i in (0,1,%size%) do (
if defined st%%i (
call :st%%i
echo.
)
)
echo.
pause
goto :menu
:st1
echo First Command
goto :eof
:st2
echo Second Command
goto :eof
:st3
echo Third Command
goto :eof
You can set lines checked as defaults under :preset label.
A batch file is a list of command prompt commands. The following code prints to the terminal:
echo whateveryouwant
print your menu using these echo statements in a batch file.
Getting user input can be found here: How to read input from console in a batch file?
The installing of applications is a little more complex - you need to know the requirements of your apps and where files should be moved - that should be simple, as well; use move on the appropriate files in the appropriate place.
Here's an example of a batch script menu I'm using:
#echo off
setlocal
:begin
cls
echo [LOCAL ACCOUNTS REMOTE ADMIN] --------------------------------------
echo 1 -- List local accounts on a remote machine
echo 2 -- Create a local account on a remote machine
echo 3 -- Change a local account password on a remote machine
echo 4 -- Delete a local account on a remote machine
echo;
echo 5 -- exit
echo;
set /P rmFunc="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3 4 5 x) do if #%rmFunc%==#%%I goto run%%I
goto begin
:run1
rem list local accounts code
goto begin
:run2
rem create local account code
goto begin
rem and so on, until...
:run5
:run9
:run99
:runx
endlocal
goto :EOF
The most relevant bits are the set /p line and the for...in lines. The for...in line basically compares the choice entered with every menu item number, and if match, goto run#; otherwise start over from the beginning.
I saw that none of the above answers completely answered his/her question. One feature that they have left out is selecting all the software it installs in one go (so to speak).
So I made this off the top of my head (extremely sorry if there is something wrong with it, I'll edit it if there is).
#echo off & setlocal enabledelayedexpansion
echo What would you like to install?
::Put your options here, preferably numbered.
set /p op=Type the numbers of the software you want to install (separated by commas with no spaces. E.g: 1,3,2):
for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next
:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%
:next
::Put some more stuff here...
So that was a bit excessive. Feel free to change some things around and such.
What the code is doing is getting the user input (such as if you put in "1,3,4"), putting each number into its own variable, checking if that variable is empty, and if it isn't, sending you to code that does whatever the option was. It does this a few times until all the variables have been assessed.
This is a proposed analysis for improvement on David Ruhmann's code relating to the "Validate Input" section above:
Testing the menu for special characters works a charm except for the following characters "^&<". When each are submitted for input the program closes.
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:<=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Escaping the ^ and & works, but something very peculiar going on with the parsing of "<" and ">" (escaping these doesn't appear to work). If we reverse the order of the two statements as in the above amendment we find "<" works, but now ">" doesn't.
However, shifting the second statement with "<" down thus, both redirection characters work but now ")" doesn't!!
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
set "Input=%Input:<=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Another great tutorial for batch menus is found here.
I'm using this
#echo off
:a
echo Welcome to a casual log-in (you are a idiot)
echo.
pause
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p c=Email:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p u=Password:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
msg * Welcome %c%.
goto a
There is actually an extremely easy way to do this.
#echo off
echo Which app do you want to install?
echo [APP 1]
echo [APP 2]
echo [APP 3]
echo.
echo Type 1, 2, or 3.
set /p "AppInstaller=>"
if %AppInstaller%==1 goto 1
if %AppInstaller%==2 goto 2
if %AppInstaller%==3 goto 3
:1
[INSTALL CODE]
:2
[INSTALL CODE]
:3
[INSTALL CODE]
The menu, when coded like this, will look like this:
Which app do you want to install?
[APP 1]
[APP 2]
[APP 3]
Type 1, 2, or 3.
>_
The code sets the variable AppInstaller to 1, 2, or 3. The file determines this and redirects you to an installer for each one.
This is fairly easy code that I use a lot in multiple choice games:
#echo off
color 0a
cls
:download
echo App 1
echo App 2
echo App 3
echo App 4
echo App 5
echo All Apps
echo
echo Select What App (1, 2, 3, ect.):
set /p apps=
if %apps%==1 goto 1
if %apps%==1 goto 2
if %apps%==1 goto 3
if %apps%==1 goto 4
if %apps%==1 goto 5
if %apps%==1 goto all
:1
(Your Code Here)
:2
(Your Code Here)
:3
(Your Code Here)
:4
(Your Code Here)
:5
(Your Code Here)
:all
(Your Code Here)

Resources