Okay so I basically need a code that will do something like this:
#echo off
start:
echo Type what you want me to remember!
set /p newline=
<add %newline% to Line 4 in safe.txt>
goto start
I don't want to replace the existing Line 4 in safe.txt.
If you want to append the input to the end of the file:
#echo off
start:
echo Type what you want me to remember!
set /p newline=
Echo %newline% >> safe.txt
goto start
If you want to add the line after the 4th line:
#echo off
start:
echo Type what you want me to remember!
set /p newline=
setlocal enabledelayedexpansion
set count=0
ren safe.txt safe.tmp
for /f "delims=" %%a in (safe.tmp) do (
set /a count+=1
Echo %%a >> safe.txt
if !count!==4 Echo !newline! >> safe.txt
)
del safe.tmp
goto start
And one of these codes should help you with your problem.
Mona
Related
Simple question: why is ECHO is OFF/ON being printed in a .txt file instead of the users' input? I tried various solutions - none of them worked. Any and all help will be appreciated.
Code
#echo off
:start
del "test.txt"
cls
set /p test_test = "> "
echo %test_test% >> test.txt
for /f "delims=" %%a in (test.txt) do (
set file = %%a
)
echo %file%
pause
The ECHO command with no parameters outputs the status of ECHO, which in this case you've set to off. Because your %test_test% variable is empty, (due to you setting a variable named %test_test %), the ECHO command is being entered with no parameters.
#echo off
:start
cls
set /p "test_test= > "
(echo %test_test%)>test.txt
for /f "delims=" %%a in (test.txt) do (
set "file=%%a"
)
echo %file%
pause
As you may have noted you werre also setting a variable named %file % too.
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.
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
This is my batch script:
#echo off
title
setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs
start a.vbs
exit
For every time that this program runs, it overwrites the a.vbs file with the new code as variable c. Is it possible to have "wscript.createobject("sapi.spvoice").speak "!c!"" preexisting in a VBScript and simply have batch assign the variable and execute it instead of overwriting and then executing?
With the help of aphoria, I tweaked my scripts to this:
VBScript:
wscript.createobject("sapi.spvoice").speak wscript.arguments(0)
Batch Script:
#echo off
title
setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
cscript //nologo b.vbs !c!
exit
May I suggest you another solution?
JScript language is similar to VBScript, but have an advantage in this case: the JScript code can be placed inside the Batch file itself via a very simple trick. This way, it is not necessary to create a separated file with the JScript code:
#if (#CodeSection == #Batch) #then
:: Previous line is:
:: - in Batch: a valid IF command that does nothing
:: - in JScript: a conditional compilation IF statement that is false
:: so the following code is omitted until the next atSign-end
#echo off
title
setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
rem Execute this Batch file as a JScript one:
cscript //nologo //E:JScript "%~F0" !c!
exit
#end
WScript.CreateObject("sapi.spvoice").Speak(WScript.Arguments(0));
In this case the original VBScript code is so simple that the JScript translation is immediate; just note that the uppercase letters are needed in JScript. However, I am not entirely sure that spvoice execute the same in JScript than in VBScript; you must do a test...
Create a script file SpeakNumber.vbs (call it whatever you want).
Put this inside it:
Set args = Wscript.Arguments
WScript.CreateObject("sapi.spvoice").Speak args(0)
Then, change your batch file like this:
#echo off
title
setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
START SpeakNumber.vbs !c!
exit
This is my batch script:
#echo off title setlocal enabledelayedexpansion :a set /p a= !d!
for %%G in (%a%)
do (set /a b+=1
if !b! neq 1
(set c=!c!-%%G)
else
(set c=%%G))
echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs
start a.vbs
exit
You dont have use a hybrid as you can embed the vbs file inside the batch file.
#echo off
set /p "Voice= Enter what you would like to say : "
echo dim speechobject >> sapi.vbs
echo set speechobject=createobject("sapi.spvoice") >> sapi.vbs
echo speechobject.speak "%Voice%" >> sapi.vbs
start sapi.vbs
timeout /t 1 /nobreak
del sapi.vbs
I'm looking for a DOS batch program that takes a file:
First input line
Second input line
Third input line...
And outputs "First input line"
you can just get the first line like this
set /p firstline=<file
echo %firstline%
Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:
#echo off
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (infile.txt) do (
if !first!==1 echo %%i
set first=0
)
endlocal
With an input file of infile.txt as:
line 1
line 2
line 3
this will output:
line 1
This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
echo %%i
goto :endfor
)
:endfor
endlocal
Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):
#echo off
setlocal enableextensions enabledelayedexpansion
if x%1x==xx goto :usage
if x%2x==xx goto :usage
set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
if !linenum! geq %2 goto :break1
echo %%i
set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish
:usage
echo.winhead ^<file^> ^<numlines^>
echo. ^<file^>
echo. is the file to process
echo. (surround with double quotes if it contains spaces).
echo. ^<numlines^>
echo. is the number of lines to print from file start.
goto :finish
:finish
endlocal
why not use the more +1 command via a pipe?
e.g.
type something | more +1