What delimiter to use in FOR loop for reading lines? - windows

I have a txt file that contains the following lines
jfo3 93jfl
lvls 29fdj
nskd jfuwe
xlkw eklwe
I'm trying to read the file line by line, and do something with it. What delimiter should I use?
The delim I'm using here reads each word separately.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (lines.txt) do (
echo %%x
)

This reads line by line for me:
for /f "delims=" %x in (lines.txt) do echo %x

The problem is not related to delims, but to tokens:
for /f "tokens=*" %%x in (lines.txt) do echo %%x

If this is your input file:
abc,def
ghi,jkl
mno,pqr
then use
FOR /F "tokens=1,2,3 delims=," %%i in (test.txt) do (whatever u want)

Related

How to read file line by line using batch

Source file is present in below manner:-
abc
dfc
adbc
I am using below code to print the each line in the file.
for /f "tokens=* delims=" %%a in ('type input.txt') do (
set line=%%a
echo %line%
)
but the output is
adbc
adbc
adbc
What to do? Required output is:
abc
dfc
adbc
Unless you specifically need to manipulate the line or save the content of the last line in a variable there is absolutely no need to use a For loop:
Type input.txt
#Squashman provided the answer really, but here it is written out:
setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('type input.txt') do (
set line=%%a
echo !line!
)

Batch file not accepting spaces in for /f

This command does not work it wont accept spaces in the path name and I cant figure out how to fix it please help
for /f "tokens=* delims=" %%x in (E:\NON-school stuff\space space\a.txt) do echo %%x
I have tried everything please help!
Easy fix.
set "sourceFile=E:\NON-school stuff\space space\a.txt"
for /f "usebackq tokens=* delims=" %%x in ("%sourceFile%") do echo %%x
Anytime you have spaces in file paths you need to quote them and if you read the help for the FOR /F command you will see that the usebackq option allows you to use quotes when you have spaces in file names. If you don't use the usebackq option it treats the file name as a string.
one trick is to transform your filename into a short filename, so it may be accepted by any command without problems
for %%a in ("\some dir\some text.txt") do echo %%~sa
in your case
for %%a in ("E:\NON-school stuff\space space\a.txt") do for /f "tokens=* delims=" %%x in (%%~sa) do echo %%x

pass the output of a batch file into a for loop

I am trying to pass the output of a process into a for loop by using pipes
type %1% | findstr /R /V "Test" | for /F "tokens=*" %%i IN ('more') DO #echo %%i
but I do not know what to put in place of ('more') so that it reads the output from the findstr command. Is this even possible? Or do I have to save the output to a file and then read in the file in an entirely different batch program? Please help.
for /f "delims=" %%a in ('findstr /rv "Test" "%1%" ^| more') do echo %%a
for loops cannot read from STDIN, so you need to put the command whose output you want to process into the parantheses:
for /F "tokens=*" %%i IN ('type %1% ^| findstr /R /V "Test"') DO #echo %%i
Note that pipes must be escaped in the subshell (^|).

Batch script skipping blank entries in a .CSV when delim is ','

I have a .CSV that I am trying to sort through to create another file from the data, but when I run it through, it skips blank entries. For example, if a line is
value,value,value,,,value
and I try to get the 4th column, it would spit out 6th. Presumably because it is the next valid value. I don't want it to skip the blank entry as it can mess up the tables I'm trying to make. Anyone know how to resolve this? (Any tips are welcome as I suck at batch scripts)
Here is my script:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14 delims=," %%a in (file.csv) DO (
echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n
)
pause
This is the standard behaviour of the FOR/F loop, consecutive delims only used as one delimiter.
But you can use a workaround with a second FOR/F.
Prefix each column with another character, split the line at the delim and remove the prefix.
setlocal EnableDelayedExpansion
FOR /F "delims=" %%L in (test.bat) DO (
set "line=%%L,,,,,,,,"
set "line=#!line:,=,#!"
FOR /F "tokens=1,2,3,4 delims=," %%a in ("!line!") DO (
set "param1=%%a"
set "param2=%%b"
set "param3=%%c"
set "param4=%%d"
set "param1=!param1:~1!"
set "param2=!param2:~1!"
set "param3=!param3:~1!"
set "param4=!param4:~1!"
echo !param1! !param2! !param3! !param4!
)
)
As jeb already mentiones in his answer, for /F treats consecutive delimiters as one. To avoid that, you could replace each delimiter , by "," and enclose each full line by "", so each field appears as being enclosed within "", which can easily be removed finally by the ~ modifier of the for /F variable; so there is no need to do any more string manipulations (like sub-string expansion) later on:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "usebackq delims=" %%# in ("file.csv") do (
set "LINE=%%#"
setlocal EnableDelayedExpansion
for /F "tokens=1-4 delims=," %%A in (^""!LINE:,="^,"!"^") do (
endlocal
echo Field 1: %%~A
echo Field 2: %%~B
echo Field 3: %%~C
echo Field 4: %%~D
setlocal EnableDelayedExpansion
)
endlocal
)
endlocal
This might not work properly if the data contain , characters by themselves (remember that , is not considered as delimiter in case a field is enclosed within "" in the original CSV file).
The toggling of delayed expansion is done to not lose any exclamation marks in the data.

BAT file – use bracket as delim

I'm trying to use brackets (and space character) as delimiters in BAT file loop, but result is only error message on command line – ^(^)"" was unexpected at this time.
for /f "tokens=1,2 delims=" ^(^)"" %%a in ('status') do if "%%b"=="my_text" echo %%a
Can someone elaborate on how to do this?
Try this:
for /f "tokens=1,2 delims=() " %%a in ('status') do if "%%b"=="my_text" echo %%a

Resources