views:

58

answers:

5

How can I read only X lines from a a.txt file?

The file contains all the names of a directory, I would like to read only x lines. X can be a number that can varies from 1 to 99

A: 

You'll need to modify this based on your needs, but the script below will loop through the file 'directories.txt', and ECHO the contents of the line until you hit the maximum number of lines set in maxlines.

@ECHO OFF
setlocal enabledelayedexpansion

SET /A maxlines=1
SET /A linecount=0

FOR /F %%A IN (directories.txt) DO ( 
  IF !linecount! GEQ %maxlines% GOTO ExitLoop
  ECHO %%A 
  SET /A linecount+=1
)

:ExitLoop

PAUSE
LittleBobbyTables
Thank you very much, I think is what I needed, I just have to try it
aemme
The linecount is not incremented. It remains at the initial value
aemme
Works for me in XP; what version of Windows are you using?
LittleBobbyTables
I'm using windows server 2003
aemme
If I write in the dos command set /a linecount+=1 is incremented, but it is not from inside the batch file :((
aemme
Can you edit your question and paste in what you have so far? This works on Windows Server 2003 R2 also, so I'm not sure what's wrong.
LittleBobbyTables
What I have writte is this: @echo off setlocal enabledelayedexpansion dir /b /o:-d /t:c "D:\backup e ckpdb EPT-ICD\test\unload\*.rar" > "D:\backup e ckpdb EPT-ICD\test\unload\lista_files_rar.txt" set /a maxlines = 5 set /a linecount = 1 for /f "usebackq tokens=*" %%a in ("D:\backup e ckpdb EPT-ICD\test\unload\lista_files_rar.txt") do ( if %linecount% GEQ %maxlines% goto exitloop echo %%a %linecount% %maxlines% set /a linecount+ =1 ) :exitloop pause
aemme
Batch files are very unforgiving. I see a few things wrong. 1. You should have `SET /A linecount=1` without spaces before or after the equal sign. 2. I had `!linecount!`, while you have `%linecount%`. 3. I had `set /a linecount+=1`, you have a space between the plus sign and the equal sign. I strongly suggest you copy my example *as is*, and make sure it works before you modify it.
LittleBobbyTables
A: 

you can use vbscript. Here's an example

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strNum = objArgs(0)
strFile=objArgs(1)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
    If CInt(objFile.Line) > CInt(strNum) Then
      Exit Do  
    End If 
    strLine=objFile.ReadLine
    WScript.Echo strLine    
Loop

save as myscript.vbs and

c:\test> cscript //nologo myscript.vbs 99 file

Or if have the luxury to install tools, you can download sed or gawk for windows . Then on the command line

sed.exe "99q" file
gawk.exe "NR>2{exit}1" file
ghostdog74
A: 

the command:

set/a linecount+=1 into the FOR loop is not incremented.

If I run the command line set/a linecount+=1 from the command prompt then it works, otherwise from the batch file it doesn't

aemme
A: 

I wrote it exactly as you did, but the counter doesn't increment itself.

I have seen that the command set /a linecount+=1 doesn't increment itself when is inside a FOR loop, but I haven't found a solution yet.

aemme
A: 

Ok it works,

it's only that if I want to echo linecount during the for loop i have to use echo !linecount!.

Thank you very much and sorry for the incomprehension

aemme
Glad I could help -- if you found my answer helpful, it would be considerate of you to check the green checkbox next to my answer.
LittleBobbyTables