How can I echo nested variables inside a for loop? - batch-file

Here is a for loop to save each line of a text file to its own variable:
#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a count=!count! + 1
set var_!count!=%%a
)
endlocal
I would like to save the content of each variable to a separate new text file. How can I do this? I tried the following. But it does not work, because !var_!count!! is a variable inside a variable.
#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a count=!count! + 1
set var_!count!=%%a
echo !var_!count!!>file_!count!.txt
)
endlocal

#echo off
setlocal enableextensions enableDelayedExpansion
set count=0
for /f "tokens=*" %%a in (file.txt) do (
set /a "count+=1"
set "var_!count!=%%a"
for %%b in (!count!) do >file_!count!.txt echo !var_%%b!
)
endlocal

Related

loop througt through a comma separated string in batch file

Why this code:
#echo OFF
SETLOCAL EnableDelayedExpansion
SET /A i=0
FOR /F "USEBACKQ tokens=* delims=," %%a IN (`echo aaa,bbb,ccc`) DO (
SET /A i+=1
echo !i! %%a
)
gives me this output?
1 aaa bbb ccc
when instead I want this:
1 aaa
2 bbb
3 ccc
EDIT: the echo aaa,bbb,ccc is to simulate a command, I need to parse the output of a command
Here's some examples for you to peruse, (each, IMO, progressively more robust):
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "i=0"
For %%G In (aaa,bbb,ccc) Do (
Set /A i+=1
Echo !i! %%G
)
Pause
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"
For %%G In (aa!a,b!bb,!ccc!) Do (
Set /A i+=1
SetLocal EnableDelayedExpansion
For %%H In (!i!) Do Endlocal & Echo %%H %%G
)
Pause
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"
For %%G In ("aa|a","b&bb","!ccc!") Do (
Set /A i+=1
SetLocal EnableDelayedExpansion
For %%H In (!i!) Do Endlocal & Echo %%H %%~G
)
Pause
[Edit /]Based upon your poor modification and lack of supporting information, the following is all I'm currently willing to offer in return:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Result="
For /F Delims^=^ EOL^= %%G In ('Echo aaa,bbb,ccc') Do Set "Result=%%G"
If Not Defined Result GoTo :EOF
Set "i=0"
For %%G In (%Result%) Do (
Set /A i+=1
SetLocal EnableDelayedExpansion
For %%H In (!i!) Do Endlocal & Echo %%H %%G
)
Pause
Based on your limited information this is could fix your problem.
#echo OFF
SETLOCAL EnableDelayedExpansion
SET /A i=0
FOR /F "tokens=1-3 delims=," %%G IN ('"echo aaa,bbb,ccc"') DO (
SET /A i+=1
echo !i! %%G
SET /A i+=1
echo !i! %%H
SET /A i+=1
echo !i! %%I
)

Find a string containing a substring in windows batch file

I have a textfile (filename.txt) which contains
ProductABC_Test.txt
ProductDEF_Test.txt
ProductHIG_Test.txt
ProductIJK_Test.txt
I will be getting a variable passed (ex: product=ABC which will be substring of ProductABC_Test.txt). So I need to fetch the correct test name (ProductABC_Test.txt) from the filename.txt.
I have tried the following code -
SETLOCAL ENABLEEXTENSIONS
#echo off
set product=ABC
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do
(
set str=%%A
if NOT %str% == !%str:product=%
(
set test_suite=%%A
)
)
ENDLOCAL
echo %test_suite%
But I am not getting the right result.
You can use the following code to look for the line that contains your substring:
#echo off
SETLOCAL ENABLEEXTENSIONS
set product=ABC
set test_suite="not found"
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do (
set str=%%A
Echo.!str! | findstr /C:"!product!">nul
if !errorlevel!==0 set test_suite=!str!
)
echo %test_suite%
pause

How to handle ! with enabledelayedexpansion in batch

I am trying to read a file using enabledelayedexpansion .File contains special character.Because of enabledelayedexpansion ! and text after that is ignored .Please suggests some way by which this issue can be resolved .
note :- i have to use enabledelayedexpansion .
`
#echo off
set "search1='"
set "search2=""
set "search3=&"
set "search4=<"
set "search5=>"
set "search6=!"
set "replace1=&apos;"
set "replace2=""
set "replace3=&"
set "replace4=<"
set "replace5=>"
set "replace6=^!"
setlocal enabledelayedexpansion
set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!
break>"!newfile!"
(for /f "delims=" %%i in (!textfile!) do (
setlocal enabledelayedexpansion
set "line=%%i"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search3!"$"!replace3!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search4!"$"!replace4!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search5!"$"!replace5!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search2!"$"!replace2!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search1!"$"!replace1!"') do set "line=!line:%%~a=%%~b!"
setlocal enabledelayedexpansion
echo(!line!
endlocal
))>>"!newfile!"
Above is just part of script. textfile will contain file path.rest all substitution are working fine only in case of ! its causing issue
To read content from a file without destroying it you need to use the delayed toggling technic or reading it with set /p.
setlocal DisableDelayedExpansion
(
for /f "delims=" %%i in (textfile.txt) do (
set "line=%%i" -- This must be done while delayed expansion is disabled
setlocal EnableDelayedExpansion
set "line=!line:<=<!"
echo(!line!
endlocal
)
) > outfile.txt
The toggeling is necessary, because the expansion of a FOR loop variable is only safe in the disabled delayed expansion mode, else exclamation marks are removed/interpreted.
Reading a file via SET /P is another way to avoid the problems related to Delayed Expansion that may be simpler than FOR /F. I also show you a simpler way to define the set of replacements via an array
#echo off
setlocal EnableDelayedExpansion
rem Define the set of replacements
set "replacA[&]=&"
set "replace[']=&apos;"
set replace["]="
set "replace[<]=<"
set "replace[>]=>"
REM set "replace[!]=^!" // Not needed
set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!
call :ProcessFile < "!textfile!" > "!newfile!"
goto :EOF
:ProcessFile
set empty=0
set "line="
:emptyLine
set /P line=
if not defined line (
set /A empty+=1
if !empty! gtr 2 exit /B
echo/
goto emptyLine
)
for /F "tokens=2,3 delims=[]=" %%a in ('set replac') do set "line=!line:%%a=%%b!"
echo !line!
goto ProcessFile
The most complex part in :ProcessFile subroutine is just used to preserve a maximum of 2 empty lines together (that may easily be increased to more lines, if needed).
Edit: completely overwritten. I could either keep an ! exclamation mark in output or replace it by ^^! (or ^^^^!, ^^^^^^! etc.) with even number of ^^ carets as follows:
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "textfile=files\31130282textfile.txt"
set "newfile=files\31130282textfileNew.txt"
set "line="
set "quote=""
setlocal DisableDelayedExpansion
set "exclamation=!"
set "exclamationNew=%~1!"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
SETLOCAL enabledelayedexpansion
set "line=!line:&=&!"
set "line=!line:<=<!"
set "line=!line:>=>!"
call set "line=%%line:!quote!="%%"
set "line=!line:'=&apos;!"
call set "line=%%line:!exclamation!=!exclamationNew!%%"
echo(!line!
ENDLOCAL
))>"%newfile%"
endlocal
type "%newfile%"
Output:
==>type files\31130282textfile.txt"
<'abc'>&"cde"T
"<x!y!z!>"
==>D:\bat\SO\31130282.bat ""
<&apos;abc&apos;>&"cde"T
"<x!y!z!>"
==>D:\bat\SO\31130282.bat "^"
<&apos;abc&apos;>&"cde"T
"<x^^!y^^!z^^!>"
==>D:\bat\SO\31130282.bat "^^"
<&apos;abc&apos;>&"cde"T
"<x^^^^!y^^^^!z^^^^!>"

Loop through list, set variable

I'm trying to make a loop that goes through a file with filenames on each line, set the first filename as a variable and execute the rest if the script. Then take the second line and do the same.
etc. etc.
The problem is that it only does the first line of filenames.txt
#echo off
for /F "tokens=*" %%G in (filenames.txt) do (
set filename=%%G
script
script
script
)
pause
It has be a batch file.
The whole script:
#ECHO OFF
for /F "tokens=*" %%G in (filenames.txt) do (
SET FileName=%%G
SET Word1="ts_confirmImplicitSAMM.gram"
SET Word2="SWIrcnd"
for /f "tokens=3" %%f in ('find /c /i %Word1% %FileName%') do set PairsToShow=%%f
SET /a Lines1=0, Lines2=0
FOR /f "delims=" %%a IN ('findstr "%Word1%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines1+=1
SETLOCAL enabledelayedexpansion
SET "$1!Lines1!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$1"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
FOR /f "delims=" %%a IN ('findstr "%Word2%" "%FileName%"') DO (
SET "str=%%a"
SET /a Lines2+=1
SETLOCAL enabledelayedexpansion
SET "$2!Lines2!=!str!"
FOR /f "tokens=1*delims==" %%b IN ('set "$2"') DO (IF "!"=="" endlocal)&SET "%%b=%%c"
)
SET /a Lines=Lines1+Lines2
ECHO(%Lines% lines read from %FileName%.
IF %Lines1% leq %Lines2% (SET /a MaxPairs=Lines1) ELSE SET /a MaxPairs=Lines2
IF %PairsToShow% gtr %MaxPairs% (
ECHO only text for %MaxPairs% pairs NOT %PairsToShow% :/
GOTO :END
)
(FOR /l %%a IN (1,1,%PairsToShow%) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
CALL SET "Line1=%%$1%%a%%"
CALL SET "Line2=%%$2%%a%%"
<NUL SET /p "=!Line1!"
ECHO !Line2!
ENDLOCAL
))>> result1.txt
ENDLOCAL
TYPE result1.txt| FINDSTR /V EVNT=SWIgrld >> result.txt
DEL result1.txt
PAUSE
)
Without seeing the rest of your script... you probably need to do 1 of 2 things:
Use SETLOCAL ENABLEDELAYEDEXPANSION (as 2nd line of your script) and then reference the variable filename as !filename! instead of %filename% to use the run-time value instead of the load-time value. But that could cause other problems, depending on what goes on in "script".
Just use %%G instead of filename

how to get the output in column wise in a below batch file

how to get the output in column wise in a below batch file
#echo off
setlocal enableextensions enabledelayedexpansion
set Counter=0
for /f "usebackq tokens=2,5,6 delims= " %%a in (`findstr /c:"Cod "
1231.txt`) do (
set x=%%b
set x=!x:~3!
set y=%%c
if %%c LSS 10 set y=!y:~1!
set item!Counter!=%%a-!x!#!y!
set /a Counter+=1
)
set result=%item0%
for /l %%i in (1,1,!Counter!) do set result=!result!!item%%i!
FOR /F %%A IN ('CHCP') DO SET CHCP=%%A
echo %result% >>result.txt
endlocal
Looks like you are concatenating the values into the result variable.
Rather than:
... do set result=!result!!item%%i!
Why not output the value directly to your output file:
... do echo !item%%i!>>result.txt

Resources