Link to home
Start Free TrialLog in
Avatar of jrmn
jrmn

asked on

Read/Save TXT files

Hi Experts,

Using an editbox to enter a valid directory path, how will I do it in Delphi6 that will scan this directory for all occurance of .TXT files. For each TXT file, will then scan for a given word. If found, will then save the filename to another TXT file.
Say if directory is 'C:\temp\', and there are 10 TXT files found here, it will then scan each of these files for any occurance of the word say "Delphi". For each TXT file that contains the given word, it will then write it to a TXT file (say mytext.txt) the filename and the line where the word "Delphi" was first found.
It will be something like this:

If only 5 TXT files contains the word "Delphi", MYTEXT.TXT, when you open it, the contains will be:

TEXT1.TXT, line 2
TEXT4.TXT, line 40
TEXT5.TXT, line 23
TEXT8.TXT, line 12
TEXT9.TXT, line 100

Can this be done?

Thanks,
jrmn
Avatar of shaneholmes
shaneholmes

Yes it can be done!

Shane
lol yes quite easy to do, but it would be about 15 - 20 minutes worth of work if nobody does it. i might later on.
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
object Form1: TForm1
  Left = 216
  Top = 114
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Check and Cure *.HTML and *.HTM files from "Happy Time" Virus'
  ClientHeight = 446
  ClientWidth = 632
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefaultPosOnly
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label_List: TLabel
    Left = 144
    Top = 8
    Width = 126
    Height = 13
    Caption = 'List of scanned ( *.txt ) files'
  end
  object Button_Scan: TButton
    Left = 4
    Top = 0
    Width = 128
    Height = 25
    Caption = 'Scan *.txt'
    TabOrder = 0
    OnClick = Button_ScanClick
  end
  object ListBox_List: TListBox
    Left = 4
    Top = 24
    Width = 620
    Height = 416
    ItemHeight = 13
    TabOrder = 1
  end
  object Edit_Count: TEdit
    Left = 496
    Top = 7
    Width = 128
    Height = 17
    BorderStyle = bsNone
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    ReadOnly = True
    TabOrder = 2
  end
  object Checkbox_List: TCheckBox
    Left = 272
    Top = 6
    Width = 71
    Height = 17
    Caption = 'Show list'
    Checked = True
    State = cbChecked
    TabOrder = 3
  end
  object Edit_Path: TEdit
    Left = 352
    Top = 1
    Width = 128
    Height = 21
    TabOrder = 4
    Text = 'C:\Temp\'
  end
end
That seems a little overkill but someone did it :)
> That seems a little overkill but someone did it :)
Yes, it seems a little overkill, because I used a source code of an AntyVirus programm which scans all HDD's finds all .htm and .html files and Cures them. I modifyed it to work:
1. with a path
2. with .txt files
3. searh (find) internal subtext 'DELPHI'
As often happens to me I forgot the Caption property of Form1 as it is in the original project: <SMILE>

emil
Avatar of jrmn

ASKER

Hi Guys,

So sorry, wasn't able to follow this one up ASAP because I was out of town.
Anyways, the code above works but is there something much simplier?
If no one will put his/her 2 cents worth, I'll give the points to esoftbg.

Thank you so much,
jrmn