Batch scripting not assigning variable as expected -
i'm working on batch script assign variable string , trim it. i'm facing 2 issues:
- the variables not assigning properly. taking last value from variable file.
- the variables not assigning first time run script. need run script second time see if variables have been assigned. on third run, can see trim working.
my script looks this:
@echo off /f "tokens=*" %%a in ('findstr "w3svcwinsrvrhosts" "c:\data\siebeladmin\commands\file.txt"') ( /f "tokens=2 delims==" %%b in ("%%a") ( %%c in (%%b) ( echo in loop set str=%%c echo %%c echo.%str% set str=%str:~-6% echo.%str% )))
the output looks on third run:
> c:\users\parthod\desktop>b.bat in loop xsj-uvapoms72 7.2.27 7.2.27 in loop xsj-uvapoms82 7.2.27 7.2.27 in loop 172.17.2.26 7.2.27 7.2.27 in loop 172.17.2.27 7.2.27 7.2.27
you fell delayed expansion trap -- try this:
@echo off setlocal enabledelayedexpansion /f "tokens=*" %%a in ('findstr "w3svcwinsrvrhosts" "c:\data\siebeladmin\commands\file.txt"') ( /f "tokens=2 delims==" %%b in ("%%a") ( %%c in (%%b) ( echo in loop set str=%%c echo %%c echo.!str! set str=!str:~-6! echo.!str! ))) endlocal & set str=%str%
in between setlocal
/endlocal
block, delayed variable expansion active. use feature enclose variables !!
rather %%
.
since setlocal
sets new namespace variables, compound endlocal & set
statement required transfer value of str
beyond block.
Comments
Post a Comment