This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 3 years ago.
so i have a problem with set a variable with FINDSTR value in it. I have to set the variable so i can output the value to text file that i wanted
i already tried to set the variable in for looping every findstr value that i get, but the output didnt like i wish too
set /p tanggal="Masukan Bulan/Tahun: "
echo.
set /p namaService="Masukan services path: "
setlocal enabledelayedexpansion
echo Services,tanggal,hit >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
FOR /L %%A IN (1,1,31) DO (
if %%A LEQ 9 (
set "jumlahHit = FINDSTR /R /N "0%%A/%tanggal%.*%namaService%" access* | FIND /C ":""
echo !jumlahHit! >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
) else (
set "jumlahHit = FINDSTR /R /N "%%A/%tanggal%.*%namaService%" access* | FIND /C ":""
echo !jumlahHit! >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
)
)
echo Total : >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
FINDSTR /R /N "%tanggal%.*%namaService%" access* | FIND /C ":" >> Summary_%tanggal:/=-%_%namaService:/=-%.txt
I expect the output to be some value with findstr result but the output in the file say echo is off
Services,tanggal,hit
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
Total :
152720
is that any other way to set the findstr result in looping?
You have two problems:
Variable Names
Variable names are exactly as you type them:
set variable = string
This sets variable{space} to the value {space}string. So to print that variable, you'd need to have
echo %variable %
This is why your lines are full of ECHO is off.
You can't set variable values from inline commands
An example of what I mean:
set variable=dir
echo %variable%
:: output is "dir"
See this question on how to set value of a variable to the output of a command.
Related
So I'm making, like, a fake messenger thing in batch to mess with people and I want to make it seem more realistic. Basically, I need to be able to search a variable to see if it contains a word? if that makes sense. Everyone that I've asked has said to use the findstr function, but I can't really do that because it's not a separate text file.
I was wondering if there was a way to do, like,
set /p input=thing (a variable for user input)
if %thing% contains "o" echo yes
I don't know if that makes sense. Sorry. I'm a beginner.
Thank you so much for reading this. I've been looking at stuff for this for like 3 hours
findstr works, but you have to consider everything. For instance:
findstr /irc:"hi"
will find the word Hi any string, but it will also find high, which etc. So it will take a lot of time and effort to get the responses close to 100%.. anyway, all just for fun. Give this a try.
#echo off
cls
set /p "you=What is your name? "
#echo off & setlocal enabledelayedexpansion
set "cn=0"
for %%i in (Mary Catlin John Steve Emma Candice) do (
set /a cn+=1
set "name[!cn!]=%%i"
echo !cn!. %%i
)
for /l %%a in (1,1,!cn!) do set "ucount=!ucount!%%a"
choice /c !ucount! /m "Hi %you%, Who'd you like to chat to?"
set name=!name[%errorlevel%]!
echo Please wait for !name! to join.. & timeout /t 6 /nobreak>nul 2>&1 & echo !name! is online.
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
:chat
set /p "string=%you%: %ESC%[91m"
(timeout /t 1)>nul 2>&1
call :strings
goto :chat
:hi
echo %ESC%[92m!name!: What's up?%ESC%[0m
goto :eof
:doing
echo %ESC%[92m!name!: I am well, thank you%ESC%[0m
goto :eof
:time
echo %ESC%[92m!name!: It is now %time%%ESC%[0m
goto :eof
:date
echo %ESC%[92m!name!: The current date is %date%%ESC%[0m
goto :eof
:ben_jerry
echo %ESC%[92m!name!: I love Ben ^& Jerry's!!%ESC%[0m
goto :eof
:dude
echo %ESC%[92m!name!: DUDE!!%ESC%[0m
goto :eof
:not_love
echo %ESC%[92m!name!: WOAH!!! You're going to fast, I'm out%ESC%[0m
goto :eof
:strings
echo "%string%" | findstr /irc:"\<hi\>">nul 2>&1 && call :hi
echo "%string%" | findstr /irc:"\<how\>">nul 2>&1 && call :doing
echo "%string%" | findstr /irc:"\<time\>">nul 2>&1 && call :time
echo "%string%" | findstr /irc:"\<date\>">nul 2>&1 && call :date
echo "%string%" | findstr /irc:"\<love\>">nul 2>&1 && call :not_love
echo "%string%" | findstr /irc:"\<ben & jerry's\>">nul 2>&1 && call :ben_jerry
echo "%string%" | findstr /irc:"ice cream">nul 2>&1 && call :dude && call :ben_jerry
if "%string%" == "/exit" color & cls & exit
goto :eof
Result, where me: would be what I typed and `Bot: being the responses.
You can do it within external commands, using CMD's set replace.
set /p "thing=thing (a variable for user input) "
if not "%thing%"=="%thing:o=_%" echo Contains 'o'
With that, you are replacing the content of %thing% temporary to _ and then check if both strings are equal.
If the replace matches 0 results, then strings will be equal and it will not show anything. If some string is replaced, strings will not match so it will contain the character.
How do I use a variable like %%i in a permanent variable?
(I don't really know the correct terms, so I hope anyone can figure out what I mean)
This is the code I am using:
#echo off
color 0f
goto number
:number
title number
cls
echo number of options?
set /p num=
goto option
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter a option
set /p n%%i=
echo %%i = %n%%i% >> log.txt
)
goto select
:select
cls
echo %n2%
pause >nul
the "%n2%" works for whatever you put in second, but when I try to print it into a file ( echo %%i = %n%%i% >> log.txt ) it doesn't work.
I know the "%n%%i%" is not correct, But I don't really know what to actually put there.
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter an option
set /p option%%i=
)
goto select
:select
set option>log.txt
cls
echo %option2%
You may like to consider this.
The command
set option
will show every environment variable that starts option in the format option1=Gido, which is why I changed n to option (there are other variables set that start n)
I'm creating a bat file that will write me a script that will be called by another bat.
What I wrote:
echo #echo off>>myscript-NTRE_TN_F
echo open 127.0.0.1>>myscript-NTRE_TN_F
echo user USER PASS>>myscript-NTRE_TN_F
echo prompt n>>myscript-NTRE_TN_F
echo ascii>>myscript-NTRE_TN_F
echo lcd C:\>>myscript-NTRE_TN_F
SET MAX_FILES=200
SET /S FILE_COUNT=0
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! LSS %MAX_FILES% (
echo mget %%a>>myscript-NTRE_TN_F
echo echo %%a>>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F
)
SET /A FILE_COUNT+=1
)
echo bye>>myscript-NTRE_TN_F
My issue is in the echo echo %%a part.
My output now is:
#echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1
mget FILE2
echo FILE2
bye
I would like an OUTPUT like this:
#echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1>>C:\Downloaded_Files.txt"
mget FILE2
echo FILE2>>C:\Downloaded_Files.txt
bye
I found something similar in the topic here:
Why gives my script an other output with the echo command?
I don't know how to manage the variable.
Thanks in advance for your help.
(example)
echo echo %%a^>^>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F
The caret ^ signifies that the following character be reproduced literally.
I suggest using this batch code:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
>"%ScriptFile%" echo #echo off
>>"%ScriptFile%" echo open 127.0.0.1
>>"%ScriptFile%" echo user USER PASS
>>"%ScriptFile%" echo prompt n
>>"%ScriptFile%" echo ascii
>>"%ScriptFile%" echo lcd C:\
SET "MAX_FILES=200"
SET "FILE_COUNT=0"
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
>>"%ScriptFile%" echo mget %%a
set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
>>"%ScriptFile%" echo !ScriptLine!
SET /A FILE_COUNT+=1
)
:Finish
>>"%ScriptFile%" echo bye
endlocal
The redirection operator can be also put at beginning of a line which makes it easier to read what is written into the file.
Inside the FOR loop the IF condition is changed to exit the loop on reaching value of MAX_FILES.
And the line to output is first assigned to a variable enclosed in double quotes to get >> interpreted as string and the value of this variable is output using delayed expansion with redirection to file.
Please read also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why using double quotes around variable=value.
See also the Microsoft article Using command redirection operators
Another even better solution is:
#echo on
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
(
echo #echo off
echo open 127.0.0.1
echo user USER PASS
echo prompt n
echo ascii
echo lcd C:\
)>"%ScriptFile%"
SET "MAX_FILES=200"
SET "FILE_COUNT=0"
for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
echo mget %%a
set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
echo !ScriptLine!
SET /A FILE_COUNT+=1
)>>"%ScriptFile%"
:Finish
echo bye>>"%ScriptFile%"
endlocal
All lines output to STDOUT within the two command blocks are redirected into the script file to create.
By the way: SET /S FILE_COUNT=0 resulted on execution in error message
The syntax of the command is incorrect.
The command SET does not support an option /S, just /A for an arithmetic expression and /P for prompting user for value if command extensions are enabled in current command process environment.
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.
Why does this batch code not work?
I certainly don't know why...
Download: http://tufda.net/downloads/explore/StackOverflow%20files/ultraworld.bat
#echo on
cls
set game=UltraWorld
echo Welcome to %game%!
PAUSE
cls
echo What's your name?:
set /p playername=""
cls
echo Welcome to %game%, %playername%!
PAUSE
:titlescreen
cls
echo Commands:
echo new
echo load
echo credits
echo.
set /p command1=Enter your command here:
IF command1==new (
set /p gamesave1=What name will you give this save?
)
IF command1==load (
echo No.
PAUSE
goto titlescreen
)
IF command1=="credits" (echo Everything - tufda & PAUSE & goto titlescreen)
PAUSE
When you test the variable, you need to either surround it in percentage marks, or surround it with exclamation marks, if
setlocal enabledelayedexpansion
is used.
For example,
IF %command1%==load (
When using the IF command in a batch file, you need to follow the proper syntax.
The correct way to write an IF command is:
IF /I "%command1%" EQU "new" (
The Quotes arround each side will account for an empty value, while the EQU is the comarison type which is equivelant to ==. The comparisons are as follows:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
I used a /I to indicate that the comparison is not case sensitive.