I got 2 files bot.bat
#echo off & setlocal EnableDelayedExpansion
:botconfig
set botname=Unknow
set botcity=unknow
set botadress=unknow
set botage=unknow
for /f "delims=*" %%x in (botfeel.txt) do set botfeel=%%x
:askme
set /p how=
echo I feel %botfeel%
goto askme
and botfeel.txt
good
bad
sick
What i want is instead of showing just "i feel sick" it shows randomly one of the 3 words in botfeel.txt
Make your batch file look like this:
#echo off
:botconfig
set botname=Unknow
set botcity=unknow
set botadress=unknow
set botage=unknow
:askme
set /p how=
SET /a line=%RANDOM% * 3 / 32768
if %line%==0 goto firstline
for /F "skip=%line% delims=" %%i in (botfeel.txt) do set "botfeel=%%i"&goto nextline
:nextline
echo I feel %botfeel%
goto askme
:firstline
set /p botfeel= <botfeel.txt
goto nextline
Related
Below is the code I am working on
setlocal enabledelayedexpansion
echo on
IF NOT EXIST "D:\Deployments\Parameter_Build*.txt" (
exit
)
set SCRIPTDIRECTORY=D:\Deployments\
PUSHD %SCRIPTDIRECTORY%
FOR %%A in (Parameter_Build*.txt) DO (
echo %%A
set ANT_HOME=C:\tibco\ant\apache-ant-1.9.13
set string=
set /p string=< D:\Deployments\%%A
echo string is !string!
echo before for loop
call :myInnerloop !string!
)
POPD
GOTO:EOF
rem *************************************************************
:myInnerloop
FOR /F "tokens=2,4,6,8,10 delims==:" %%G IN ("!string!") DO (
echo inside for loop
set COMPONENTDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\components"
set CONFIGDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\configuration\components"
set ADAPTER=%%K
echo %%G %%H %%I %%J %%K
echo value taken from file is !string! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !COMPONENTDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !CONFIGDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
echo Starting with deployment with parameters %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
%ANT_HOME%\bin\xanteai deploy %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
echo Deployment completed >> D:\Deployments\logs\Deployment-%%J.log 2>&1
move D:\Deployments\Parameter_Build-%%J.txt D:\Deployments\archive\Parameter_Build-%%J.txt
RENAME D:\Deployments\logs\Deployment.txt Deployment-%%J.log
)
:next
GOTO:EOF
endlocal
Parameter_Build file contains text in below format :-
Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
Here I am trying to perform deployment for each Component
Component=xyz.zip|abc.zip|jkl.zip|efg.zip
by taking SVN update for each Adapter
xyz|abc|jkl|efg
I need to separate Component and Adapter consecutively and pass it to deploy command one by one. Also before triggering deployment I need to use each components respective adapter for taking SVN update (for eg: If component xyz.zip is triggered for deployment Adapter xyz should get updated first by SVN Update utility)
This creates the variables a[0], a[1], ... a[n]
#echo off
setlocal EnableDelayedExpansion
set "var=xyz|abc|jkl|efg"
set cnt=0
set "a[0]=%var:|="&set /a cnt+=1&set "a[!cnt!]=%"
echo cnt=!cnt!
set a[
Output:
cnt=3
a[0]=xyz
a[1]=abc
a[2]=jkl
a[3]=efg
Explanation
The code simply replaces all occurences of the delimiter with "&set /a cnt+=1&set "a[!cnt!]=.
That looks strange but as example it looks like
set "a[0]=xyz" & set /a cnt+=1 & set "a[!cnt!]=abc" & set /a cnt+=1 & set "a[!cnt!]=jkl" & set /a cnt+=1 & set "a[!cnt!]=efg"
When unrolled to multiple lines it looks like
set "a[0]=xyz"
set /a cnt+=1
set "a[!cnt!]=abc"
set /a cnt+=1
set "a[!cnt!]=jkl"
set /a cnt+=1
set "a[!cnt!]=efg"
In other words, it creates several commands per one delimiter.
This technic can be used even with string delimiters like <->
set "a[0]=%var:<->=...
Thanks to #Aacini, who introduced this technic, see split string into substrings based on delimiter
I still don't understand what you really want to do, but perhaps this may help you:
#echo off
setlocal
set "Component=xyz.zip|abc.zip|jkl.zip|efg.zip"
for %%a in ("%Component:|=" "%") do (
echo With extension: %%~a
echo Without extension: %%~Na
)
EDIT: New method added
Please, review this code:
#echo off
setlocal EnableDelayedExpansion
rem Parameter_Build file contains text in below format :-
rem Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
rem Read the file:
set /P "line=" < Parameter_Build.txt
echo Line read:
echo !line!
rem Separate variables:
set "%line::=" & set "%"
rem Separate Component and Adapter in *matching* parts
set cnt=1
set "Component[1]=%Component:|=" & set /A cnt+=1 & set "Component[!cnt!]=%"
set cnt=1
set "Adapter[1]=%Adapter:|=" & set /A cnt+=1 & set "Adapter[!cnt!]=%"
rem Ok:
for /L %%i in (1,1,%cnt%) do echo Component[%%i]=!Component[%%i]!, Adapter[%%i]=!Adapter[%%i]!
Output example:
Line read:
Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
Component[1]=xyz.zip, Adapter[1]=xyz
Component[2]=abc.zip, Adapter[2]=abc
Component[3]=jkl.zip, Adapter[3]=jkl
Component[4]=efg.zip, Adapter[4]=efg
I have a batch file that contains a structured array. The problem is that it is adding a space to the end of the variable. The code below will output:
Name=Joe zzzzzz
Value=1 zzzzzz
...
Where is the space after the name and value coming from, and how can I get rid of it?
Thanks!
#echo off
set len=3
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
set i=0
:loop
if %i% equ %len% goto :eof
set cur.Name=
set cur.ID=
for /f "delims==. tokens=1-3" %%j in ('set obj[%i%]') do (
set cur.%%k=%%l
)
echo Name=%cur.Name%zzzzzz
echo Value=%cur.ID%zzzzzz
set /a i=%i%+1
goto loop
so heres my code the first part executes perfectly but the second doesnt it just displays each variable as blank. im not sure why as its formatted the same also would it be possible to put the :readprofiles part in a working for variable?:
purpose of the program: to list out a directory as profiles along with numbered choices to select. basically a menu.
#echo off
setlocal enabledelayedexpansion
set Counter=1
for /f "DELIMS=" %%i in (test.txt) do (
set "Line_!Counter!=%%i"
set /a Counter+=1
)
set /a NumLines=Counter - 1
:: this part is a test
echo %Line_1%
echo %Line_2%
echo %Line_3%
echo %Line_4%
echo %Line_5%
echo %Line_6%
:: end test
set Counter=1
:readprofiles
if %Counter%==%NumLines% goto pause
echo %Counter%. %Line_!Counter!%
set /a Counter+=1
goto readprofiles
:pause
pause
#echo off
setlocal enabledelayedexpansion
set Counter=0
for /f "DELIMS=" %%i in (test.txt) do (
set /a Counter+=1
set "Line_!Counter!=%%i"
)
For /L %%C in (1,1,%Counter%) Do echo %%C. !Line_%%C!
Pause
I need to get a batch file running, which helps our employees to change text files without trouble.
As you can see, there are a lot of whit space in this file and they need to be there, otherwise it could not be imported in other programs.
My question: Is it possible to search for a specific column and replace this value with an user input?
I've tried to get something done with help of google and this was the result:
#echo off
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
set "search=R_sph"
set "replace=R_sph_new"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
pause
PS: My batch experience is worse and it would be great to explain every step in the code you send me.
Edit: We generate a lot of files like this every day and only some of them need so be edited. The construction of this files is always the same but with other values so it's important so search via column.
Change the main loop in your batch file so that:
it only changes the 6th line;
it only changes the 5 characters from position 126 to 130.
And just to make the batch script more robust, you can also validate user input to ensure that they've entered exactly 5 characters as the replacement string.
Here's the whole file with suggested changes:
#echo off
setlocal enabledelayedexpansion
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
rem Validate user input - ensure it's exactly 5 characters
set "replace=%R_sph_new% "
set "replace=%replace:~0,5%"
if not "%R_sph_new%" == "%replace%" (
echo Don't be silly.
exit /b 1
)
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,126!%replace%!line:~131!"
echo(!line!
))>"%newfile%"
rem It might be safer to rename %textfile% to %textfile%.BAK instead of deleting it.
del %textfile%
rename %newfile% %textfile%
endlocal
pause
set "search=R_sph"
set "replace=R_sph_new"
should
set "search=%R_sph%"
set "replace=%R_sph_new%"
that is, set the values of search and replace to the contents of the variables. Your code sets search to the string R_sph.
? Are you sure that the value in R_sph will be unique in your file?
Thank you very much Klitos Kyriacou, that's exactly what I was looking for!
But unfortunately this batch does not change anything in this text file, the whole code looks like this:
#echo off
:R_sph
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~49,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
set "replace=%R_sph_new%"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
setlocal enabledelayedexpansion
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,49!%replace%!line:~54!"
echo(!line!
))>"%newfile%"
endlocal
:replace file
del %textfile%
rename %newfile% %textfile%
pause
Edit: I was in hurry and dind't get how to set it up. Now I changed the code and it's working!
i need to generate list of dummy users. no of users will be provided as a input.. like 5 or 500 or 5000.
all i want is to have a standard text like usr and append a number and generate the list like usr1, usr2, usr3 etc. I thought i can do this in batch file quickly. but stuck with loop and appending the number to the string.. can some help?
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=%name1%%numb2%
echo %name1%
echo %name2%
)
with a slight modification of your code...
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=!name1!!numb2!
echo !name1!
echo !name2!
)
endlocal
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
>q28342811.txt ECHO tstusr%random%
:loop
set "name=tstusr%random%"
FINDSTR /x "%name%" q28342811.txt >NUL
IF ERRORLEVEL 1 SET /a numb -=1&>>q28342811.txt ECHO %name%
IF %numb% gtr 1 GOTO loop
TYPE q28342811.txt
GOTO :EOF
Produces q28342811.txt
generate a random username to the output file, then repeat the operation numb-1 times, checking that the new candidate doesn't already exist in the file first.