Computing.Net > Forums > Disk Operating System > Batch to find line of text

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to find line of text

Reply to Message Icon

Name: peteywheatstraw
Date: July 25, 2003 at 09:50:37 Pacific
OS: DOS
CPU/Ram: Intel
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: July 25, 2003 at 13:37:55 Pacific
Reply:

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.



0

Response Number 2
Name: peteywheatstraw
Date: July 25, 2003 at 14:23:59 Pacific
Reply:

Sorry, first time using the forum ...
OS is Win2K
Thanks!


0

Response Number 3
Name: IVO
Date: July 26, 2003 at 08:20:50 Pacific
Reply:

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 needed

If 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 %~f3

Have a nice day


0

Response Number 4
Name: IVO
Date: July 26, 2003 at 08:32:09 Pacific
Reply:

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.


0

Response Number 5
Name: peteywheatstraw
Date: July 28, 2003 at 09:30:43 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: IVO
Date: July 28, 2003 at 09:54:21 Pacific
Reply:

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.


0

Response Number 7
Name: IVO
Date: July 29, 2003 at 06:35:48 Pacific
Reply:

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 txt

I 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=



0

Response Number 8
Name: peteywheatstraw
Date: July 30, 2003 at 08:24:28 Pacific
Reply:

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!


0

Response Number 9
Name: IVO
Date: July 30, 2003 at 08:59:08 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch to find line of text

adding date to each line of text www.computing.net/answers/dos/adding-date-to-each-line-of-text-/13265.html

extract n last line from text file www.computing.net/answers/dos/extract-n-last-line-from-text-file/13852.html

Adding Text To Line In File www.computing.net/answers/dos/adding-text-to-line-in-file/14462.html