Batch scripting not assigning variable as expected

后端 未结 1 666
你的背包
你的背包 2021-01-25 19:11

I\'m working on a batch script which will assign a variable string and then trim it. I\'m facing two issues:

  1. The variables are not assigning properly. It is taking
1条回答
  •  -上瘾入骨i
    2021-01-25 20:15

    You fell into the delayed expansion trap -- try this:

    @echo off
    setlocal EnableDelayedExpansion
    for /f "tokens=*" %%a in ('findstr "W3SVCWINSRVRHOSTS" "C:\Data\SiebelAdmin\Commands\file.txt"') do (
    for /f "tokens=2 delims==" %%b in ("%%a") do (
    for %%c in (%%b) do (
    echo in loop
    set str=%%c
    echo %%c
    echo.!str!
    set str=!str:~-6!
    echo.!str!
    )))
    endlocal & set str=%str%
    

    In between the setlocal/endlocal block, delayed variable expansion is active. To actually use this feature enclose the variables by !! rather than %%.
    Since setlocal sets up a new namespace for variables, the compound endlocal & set statement is required to transfer the value of str beyond the block.

    0 讨论(0)
提交回复
热议问题