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
)
Related
I'm trying to strip spaces and commas from %%~ni. I've managed to do it by putting it into my new variable !url!. It echos fine, but does anyone know why I can't use it here: set "line=!line:REPLACE=!url!!" ?
#echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
set "line=%%f"
echo %%~ni
set url=%%~ni
set url=!url: =-!
set url=!url:,=!
echo !url!
set "line=!line:REPLACE=!url!!"
echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
Answer for anyone else having this problem:
#echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
set "line=%%f"
set url=%%~ni
set "url=!url: =-!"
set "url=!url:,=!"
set "url=!url:(=!"
set "url=!url:)=!"
for /F "delims=" %%e in ("!url!") do set "line=!line:REPLACE=%%e!"
echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
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
#ECHO OFF
#cls
#set wpisane1=
#for /f "tokens=1" %%a in ('type C:\zadanie\1.txt ') do #echo %%a
#echo ma
#set wpisane2=
#for /f "tokens=3" %%b in ('type C:\zadanie\1.txt ') do #echo %%b
#echo lat
#echo.
#set wpisane3=
#for /f "tokens=5" %%c in ('type C:\zadanie\1.txt ') do #echo %%c
#echo ma
#set wpisane4=
#for /f "tokens=7" %%d in ('type C:\zadanie\1.txt ') do #echo %%d
#echo lat
#echo.
#set a1=29
#set a2=35
#set /a suma=%a1%+%a2%
#echo Razem maja %suma% lat(a)
#pause
#exit
My question is: How to set %%b and %%d as a1 and a2? How can I do this? Is it possible ? Please answer my question as fast as it possible. The text file contains that:
radek ma 29 lat jarek ma 35 lat
As soon as possible? Pay a little of your time to learning, start with results of next code snippet.
#ECHO OFF >NUL
#SETLOCAL enableextensions enabledelayedexpansion
for /f "tokens=1-8*" %%g in ('type C:\zadanie\1.txt') do (
rem set /a "a1=%%i"
rem set /a "a2=%%m"
rem set /a "suma=!a1!+!a2!"
set /a "suma=%%i+%%m"
#echo g=%%g h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o suma=!suma!
)
#ENDLOCAL
goto :eof
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
I'm using findstr to search for a string in a file in the following manner:
findstr "test" file.txt
This is returning the line where test is found, but I would like to return the 3 lines above and below the matching line. I've had a look and it doesn't seem like there are any in-built options to findstr to return surrounding lines.
Here you go
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=:" %%a in ('findstr /n "hello" file.txt') do (
set /a line=%%a
)
set /a num=0
for /l %%b in (3,-1,1) do (
set /a lines[!num!]=!line!-%%b
set /a num+=1
)
for /l %%c in (1,1,3) do (
set /a lines[!num!]=!line!+%%c
set /a num+=1
)
set /a count=1
for /f "tokens=* delims=" %%d in (file.txt) do (
for /l %%e in (%lines[0]%,1,%lines[5]%) do (
if !count!==%%e if not %%e==!line! echo %%d
)
set /a count+=1
)
pause >nul