Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to write a batch that will do the following:
Look for line 2 in all the text files in a specific directory (about 100 files), then write line 2 into an output text file.I know how to search for a specific string in a text file using the Find command and output that, but the text I want to parse is always different, but always on line2.

Specify your operating system exactly.
DOS is generic; if you are running Windows NT/2K/XP, it may be done quite easily.
Otherwise under plain DOS and Win 9X/ME it is far more complex and may be unpracticable.

I hope I understood your need; the following script extracts the second line of text from all files stored in the specified directory and outputs them into a result text file that can be processed later.
The batch is coded using the extended language belonging to the NT-kernel operating systems (NT/2K/XP) where DOS is emulated via NTVDM. I tested it under Win 2K
and worked fine, but if you get any problem or need more support post again.@Echo Off
:: RF.BAT Syntax: RF Path_Name File_Out
:: Look for line #2 in all the text files
:: in the Path_Name folder and write the
:: lines into File_Out. Win NT/2K/XP neededIf not %1.==[].(
Cmd /V:On /C %0 [] %1 %2 & GoTo :EOF )
Echo.
If %2.==. ( Echo Folder missing & GoTo :EOF )If not exist %~f2.\Nul (
Echo Folder %~f2 missing or not found & GoTo :EOF )If not exist %~f2.\*.txt (
Echo No .txt files in Folder %~f2 & GoTo :EOF )If %3.==. ( Echo Output file missing & GoTo :EOF )
For %%A in (%~f2.\*.txt) Do (
Set Flag=True
For /F "tokens=* skip=1 delims=" %%B in (%%A) Do (
If !Flag!==True ( Set Flag=False & Echo %%B>> %3 )
)
)
Echo Processing of Folder %~f2 completed
Echo Output stored in %~f3Have a nice day

In my previous post be sure to put a blank between =[]. and ( as that seems not clear from what showed.
The script works fine if there are no leading carriage returns before the first line, otherwise the first row is interpreted as #2.

Thanks for you quick response.
Looks like what I need however a couple of issues (which I should have mentioned first)from the file name format of the files in the directory:
File format is as follows:
"File Number One"
(Note the spaces and no file extension even though they just contain plain text.
Files are autocreated so I have no control over what they are called.If I take the .txt parameter out of the batch file and point it to my directory and run, I get "file not found File. and so on ..." (in other words it doesn't seem to see long file names).
I did notice that if I renamed the file:
"File1.txt" the batch file ran and said output was created in specified output.txt file, but no file was created.Any help you can give would be great!

I read your post and I am walking throu the problems you got. Be patient, I'll correct the flaws as soon as possible, but now I'm busy, you have to wait for a while.
I'll be back.

Here I post you an improved version of the previous script; now it can process file names without extension in long format with blanks embedded. The folder however must be in classical 8.3 Dos format.
The syntax is:
RF Folder Output [Extension]
where Extension may be omitted and if expressed is without the leading . e.g.
RF C:\MyDir\MyFolder OutFile
RF C:\MyDir\MyFolder OutFile txtI tested it quite extensively and it worked fine, but you have to say the last word.
@Echo Off
:: RF.BAT Syntax: RF Folder Output [Extension]
:: In Folder look for line #2 in all the files
:: with the selected Extension, then write the
:: lines into Output. Windows NT/2K/XP needed.If not %1.==[]. (
Cmd /V:On /C %0 [] %* & GoTo :EOF )Echo.
If %2.==. ( Echo Folder missing & GoTo :EOF )
If not exist %~f2.\Nul (
Echo Folder %~f2 not found & GoTo :EOF )
If %3.==. ( Echo Output file missing & GoTo :EOF )Set RFExt=
If not %4.==. Set RFExt=%4
Dir %~f2.\*.!RFExt! /A:-D | Find "byte" > Nul
If ErrorLevel 1 (
Echo No *.!RFExt! files in Folder %~f2 & GoTo :EOF )Echo Processing Folder %~f2 for Files *.!RFExt!
Echo.
For %%A in (%~f2.\*.!RFExt!) Do ( Set Flag=True
For /F "tokens=* skip=1 delims=" %%B in (%%~sA) Do (
If !Flag!==True ( Set Flag=False & Echo %%B>> %3 )
)
)
Echo Processing of Folder %~f2 completed
Echo Output stored in %~f3
Set RFExt=

Thanks for all your help. The batch appears to be working, but I can't seem to get it to generate an output file. I've searched all my drives in case the batch file was putting the output somewhere else - but nothing.
If it is working for you then I must be doing something wrong. I am trying to work through it ...
Thanks again!

Pay great attention in setting the variable "flag" to the value True!
If you add a space after the "e" ending true, the script acts as it is working, but no resukt is sent to output.For %%A in (%~f2.\*.!RFExt!) Do ( Set Flag=TrueCR/LF AND NO SPACES!
Batch scripts must be handled like tiny glass creatures!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |