1.

Solve : with code i need to use!?

Answer»

Hello,
i don't kon't how to read only the first/second/thirt/fourth/etc
i know a code to read only the first line:
Quote

@echo off
for /f "delims=" %%k in ( ' type "something.txt" ' ) do (
set A=%%k
goto 2
)

:2
echo %A%
pause

and something.txt list look like this:
Quote
1
2
3
4
5
6
7
8
9

if you know a code to do this,
pleas answere

-lukeIf you want to use GnuSED than this will work for any line - this example uses line 4

Code: [Select]@echo off
for /f "delims=" %%a in (' SED 4!d "file.txt" ') do set "var=%%a"
echo "%var%"

This will do the same but it has issues with blank lines, and lines starting with ; by default.

Code: [Select]@echo off
for /f "skip=3 delims=" %%a in (' type "file.txt" ') do (
set "var=%%a"
goto :2
)
:2
echo %var%

I tend to use a counter, and an if statement. I guess it's just personal prefferance after awhile.

Code: [Select]@echo off
set a=0
setlocal EnableDelayedExpansion
for /f "delims=" %%A in (file.txt) do (
set /a a+=1
if "!a!"=="4" echo %%A
)

You don't need to have 'type' in your for loop. (FILENAME.txt) works the same way.

EDIT: Example also uses line 4.Quote from: Lemonilla on January 12, 2013, 07:21:17 AM
I tend to use a counter, and an if statement. I guess it's just personal prefferance after awhile.

You don't need to have 'type' in your for loop. (FileName.txt) works the same way.

You do need to use 'type' if there are long filename elements in the filename. Or usebackq

And yes, it is personal PREFERENCE in a lot of the things you can script. You can modify the code to get the Nth line by skipping N-1 lines if you use SKIP in the options block for example to get the 4th line you skip the first 3

@echo off
for /f "skip=3 delims=" %%k in ( ' type "something.txt" ' ) do set MyLine=%%k & goto next
:next
echo %MyLine%
pause

if you want to avoid the FOR behaviour mentioned by foxidrive you can use the old trick of FINDSTR with:

/N - which prefixes each line with a number and a colon
/R "^" - use the regular expression "^" which means "all lines"

and then by using "tokens=1* delims=:" separate the prefix and use an IF test to get the line you want

set num=4
for /f "tokens=1* delims=:" %%A in ('findstr /N /R "^" "something.txt"') do if "%%A"=="%num%" set Myline=%%B
echo %MyLine%

Quote from: Salmon Trout on January 12, 2013, 08:35:32 AM
if you want to avoid the FOR behaviour mentioned by foxidrive you can use the old trick of FINDSTR with:

That technique then has issues with leading : characters.

Sed is a tool that Microsoft should have included as it's built for editing text.
I guess Powershell has ways to do it too - and there's always VBS to fall back on.

There's more than one way to skin a cat!

Quote from: foxidrive on January 12, 2013, 08:46:30 AM
There's more than one way to skin a cat!

I guess it comes down to WHATEVER tool you feel happiest with. I tend to use VBScript these days - for the task mentioned above I would just read the file into an ARRAY and get the desired line.Thank you all for your help,
i use this code to set languages
so i can make a list of translated words/phrases

if i ready with this file i will post the finaly version if i am proud about it

-luke


Discussion

No Comment Found