Why this code says echo is off? - windows

What is wrong with this code? It says ECHO is off.
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo %var2%
set var1=%var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del result
pause
Any help is appreciated.

If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.
To avoid this, you should replace all your echo commands with something like this:
echo var2: %var2%
That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".

As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.
In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.
But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.
It must be enabled, as per default the delayed expansion is disabled.
#ECHO off
setlocal EnableDelayedExpansion
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo(!var2!
set var1=!var2:~5,3!
echo(!var1! > test.txt
echo(!var1!
) else (
echo error
)
del result
I used here the construct echo( instead of echo as this will ensure echoing an empty line even if the variable is empty.

Not sure, if this post is still read, but nevertheless.
You should try the following:
On top of the code right after #echo off you have to put in
setlocal enabledelayedexpansion
Additionally anywhere you want to use variables changed in a block of brackets (like For-Loops or If's) you have to change the %into ! to get
!varname!
This should be helping...
Greetings
geisterfurz007

First create a file a.txt in the same directory u have this batch file ... write some text in that...Note: only Windows 2000
Windows ME
Windows XP
Windows Vista
Windows 7 supports FINDSTR
set /p pattern=Enter id:
findstr %pattern% a.txt > __query.tmp
set /p result=<__query.tmp
if %errorlevel%==0 (
set var2= %result%
echo %var2%
set var1= %var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del __query.tmp
pause
run this bath file .. you will find a substring(start=5,length=3) of the first line of string you have in a.txt in a newly created file test.txt. Finally got it working !

The solution for your problem is to put the "echo"s after the if block is completed.
Try this:
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
set var1=%var2:~5,3%
goto print
) else (
echo error
goto result
)
:print
echo %var2%
echo %var1% > test.txt
echo %var1%
:result
del result
pause
This way you can see the solution as you wanted.
Cheers! ;]

Related

Reference element by index in a list using Batch Script

Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)

"ECHO is on" is setting in variable

I have a batch file which takes three parameters [log:path], [logok:path], [logerr:path]. The values of the respective parameter is log:c:\logs\install.log, logok:c:\logs\installok.log,
logerr:c:\logs\installerr.log.
I need to process these 3 parameters and set it on respective variable log=c:\logs\install.log, logok=c:\logs\installok.log, logerr=c:\logs\installerr.log so that I can use them in next step to create a file in those paths.
I have written below script but somehow each variable is printing "ECHO is on". It should actually print the location path. Any idea how to achieve this?
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> C:\LOGS\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ENDLOCAL
ECHO %LOG% >> C:\LOGS\TEST2.TXT
ECHO %LOGOK% >> C:\LOGS\TEST3.TXT
ECHO %LOGERR% >> C:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> %LOGOK%
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST %LOGOK% ECHO FAILED >> %LOGERR%
Output of TEST1.TXT:
log:c:\logs\install.log
logok:c:\logs\installok.log
logerr:c:\logs\installerr.log
Output of TEST1.TXT,TEST2.TXT,TEST3.TXT:
ECHO is on.
Try this:
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> c:\Logs\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ECHO !LOG! >> c:\LOGS\TEST2.TXT
ECHO !LOGOK! >> c:\LOGS\TEST3.TXT
ECHO !LOGERR! >> c:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> !LOGOK!
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST !LOGOK! ECHO FAILED >> !LOGERR!
ENDLOCAL
Basically this:
Extends the local scope so that the variables which were assigned inside the FOR loop will be available in the rest of the script
Moves variables to the !delayed! variable syntax in order to cause them to be evaluated as late as possible. See the documentation for "EnableDelayedExpansion" for more details
Note that "ECHO is on." is the output that you get when calling echo with no parameters:
C:\>type echo_test.bat
echo
echo %no_such_variable%
C:\>echo_test.bat
C:\>echo
ECHO is on.
C:\>echo
ECHO is on.

How can I use batch to output a block of text using variables read in from a txt file?

TIA for any help or advice.
I have a requirement to import over 10k connection strings into MTPuTTY and the only way to do so is via the import function using an xml.
I have a connectionstrings.txt which has a list of all the strings and I'll be using each value as an entry several times in each block of text.
So the strings.txt file would look like;
username1#servername1
username1#servername2
username2#servername1
username2#servername2
etc
The batch file should take each of these as a variable and enter them into the block before outputting the block to an xml.
The standard block should look like this;
<Node Type="1">
<SavedSession>username1#servername1 <SavedSession>
<DisplayName>username1#servername1 </DisplayName>
<ServerName>username1#servername1 </ServerName>
<PuttyConType>0</PuttyConType>
<Port>22</Port>
<UserName></UserName>
<Password></Password>
<PasswordDelay>0</PasswordDelay>
<CLParams>-load username1#servername1 "username1#servername1 " -P 22</CLParams>
<ScriptDelay>0</ScriptDelay> )
So far I've tried to compile an IF statement using %%I as the variable and building the block with that but I get various errors such as Echo not expected. I assume I'm being daft and missing basic syntax.
FOR /F %%i in (connection strings.txt) echo (
<Node Type="1">
<SavedSession>%%i</SavedSession>
<DisplayName>%%i</DisplayName>
<ServerName>%%i</ServerName>
<PuttyConType>0</PuttyConType>
<Port>22</Port>
<UserName></UserName>
<Password></Password>
<PasswordDelay>0</PasswordDelay>
<CLParams>-load %%i "%%i" -P 22</CLParams>
<ScriptDelay>0</ScriptDelay> ) >> connection strings.xml
Ideally, it'd be great if the batch could generate separate XML files per user if that's possible?
This can be achieved with the correct application of For loop and escaping of redirection characters.
#ECHO OFF
Call :find "connection strings.txt"
ECHO completed
pause
exit
:find
For /F "USEBACKQ delims=" %%a in ("%~1") DO (
For %%i in (%%~a) DO (
CALL :create "%%~i"
)
) 2>nul
GOTO :EOF
:create
(
ECHO ^<Node Type="1"^>
ECHO ^<SavedSession^>%~1^</SavedSession^>
ECHO ^<DisplayName^>%~1^</DisplayName^>
ECHO ^<ServerName^>%~1^</ServerName^>
ECHO ^<PuttyConType^>0^</PuttyConType^>
ECHO ^<Port^>22^</Port^>
ECHO ^<UserName^>^</UserName^>
ECHO ^<Password^>^</Password^>
ECHO ^<PasswordDelay^>0^</PasswordDelay^>
ECHO ^<CLParams^>-load %~1 "%~1" -P 22^</CLParams^>
ECHO ^<ScriptDelay^>0^</ScriptDelay^>
)>>%~1.xml
GOTO :EOF

Windows batch file timing bug

I've used %time% for timing previously - at least I think I have. I have this weird
IF NOT "%1" == "" (
echo Testing: %1
echo Start: %time%
sqlcmd -S MYSERVER -i test_import_%1.sql -o "test_%1%.log"
sleep 3
echo End: %time%
)
I run this, and it prints:
Testing: pm
Start: 13:29:45.30
End: 13:29:45.30
In this case, my sql code is failing (different reason), but I figure the sleep 3 should make the time increment by 3 at least. Any ideas?
tx,
tff
This has something to do with (not) delayed expansion, but I don't remember how that works exactly. You can work around it by using a "subroutine" like this:
#echo off
if "%1" == "" (
call :doit
)
echo done
goto :eof
:doit
echo %time%
sleep 1
echo %time%
goto :eof
Output:
C:\temp>q
19:46:36.43
19:46:37.45
done
The "proper" way of doing this is probably something like (from this entry in Raymon Chen's blog):
setlocal enabledelayedexpansion
if "%1" == "" (
echo !time!
sleep 2
echo !time!
)
To "see" the problem with immediate expansion, just run this (without echo off):
if "%1" == "" (
echo %time%
sleep 2
echo %time%
)
Output (on Windows 7):
C:\temp>if "" == "" (
echo 19:48:31.95
sleep 2
echo 19:48:31.95
)
19:48:31.95
19:48:31.95
The variables are all expanded at the same time, when the if is parsed.
Pause doesn't accept a parameter at all. It won't fail, but it won't continue either. If all is right, you should see a prompt to press a key.
The time you get, is the time the script is started. Apparently the %time% environment variable is not updated during the execution of the script.
DOS doesn't have a "sleep" function. You need to add this to the end of your batch file (or something like this):
#ECHO off
TITLE Initial title
SET TITLETEXT=Sleep
:: start of script
CALL :sleep 5
:: rest of script
GOTO :END
:: Function
:sleep ARG
ECHO Pausing...
FOR /l %%a in (%~1,-1,1) do (TITLE %TITLETEXT% -- time left %%as&PING.exe -n 2 -w 1 127.0.0.1>nul)
EXIT /B 0
:END
pause
::this is EOF
I tested this code, it should work fine.

What is wrong with this batch script?

I need a batch that reads a number from a file, increments it and saves it back into this file... This is what I came up with:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
IF EXIST script\BUILDVERSION (
SET /p input = <script\BUILDVERSION
SET /a result=%input%+1
ECHO %result% > script\BUILDVERSION
) ELSE (
ECHO 0 > script\BUILDVERSION
)
At first it worked in a strange way, the result from reading the number from the file seemed to be a small random number, the result of the sum seemed random too... I don't know what I did, but now it doesn't even read the number from file into the variable...
Thanks in advance for help!
Instead of %input% and %result%, try using !input! and !result!. This seems to work better when using delayed expansion. Also, make sure you don't have any unnecessary spaces when reading from the file. You'll end up with:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
IF EXIST script\BUILDVERSION (
SET /p input=<script\BUILDVERSION
SET /a result=!input!+1
ECHO !result! > script\BUILDVERSION
) ELSE (
ECHO 0 > script\BUILDVERSION
)

Resources