Selecting Text from one file and copying to a different file 
Author Message
 Selecting Text from one file and copying to a different file

I am new to scripting and have the following issue. I have a number of text
files all in the same directory. I want to search each of the files in this
folder looking for a particular string eg "teststring" and copy the contents
from where this string occurs to the end of the file. The string only
appears once in each file. Then copy the contents to a new file. This has to
happen for each file in the directory and each copy is appended to a new
file.

Thanks in advance

Kenneth



Thu, 02 Dec 2004 17:11:59 GMT  
 Selecting Text from one file and copying to a different file


Quote:
>I am new to scripting and have the following issue. I have a number of text
>files all in the same directory. I want to search each of the files in this
>folder looking for a particular string eg "teststring" and copy the contents
>from where this string occurs to the end of the file. The string only
>appears once in each file. Then copy the contents to a new file. This has to
>happen for each file in the directory and each copy is appended to a new
>file.

>Thanks in advance

>Kenneth

Ok - I threw together two functions, ScanFolder() and ScanText(). Call
ScanFolder with the path of the folder you wish to look at each file
in, the path where you want the outputted text files to be saved (this
path must exist prior to calling the function), and the search text.
ScanFolder will call ScanText. It will output the files that contain
the searchstring into the directory you specify with a XXX before the
file name.

Anyway, if you copy the text below into a file with a .VBS extension
and run it, you will see the results. I used InputBoxes at the last
line to test the function(s), but you will probably call it with
constants when you are finished debugging this. I haven't tested this
beyond a simple run in my "My Documents" directory so its your job to
debug it and modify it to your needs.

Note that this is using a vbTextCompare search - which is needed if
you need results regardless of the case
("SearchString"=="searchstring"). However, you can speed the search up
if you specify a vbBinaryCompare (=0) instead of a vbTextCompare (=1).

Quote:
>>>>>>>>>>>>>>Code Follows<<<<<<<<<<<<<<<<<<<<<<<<<<<

Function ScanText(FSO, strFilePath, strSearch, strOut)
    Dim boolRet
    Dim strFileText
    Dim ts As TextStream
    Dim intPos
    Const ForReading = 1
    Const TristateFalse = 0
    Const vbTextCompare = 1
    boolRet = False

    If FSO.FileExists(strFilePath) Then
        Set ts = FSO.OpenTextFile(strFilePath, _
          ForReading, False, TristateFalse)
        If Not ts.AtEndOfStream Then
            strFileText = ts.ReadAll
            intPos = InStr(1, strFileText, strSearch, vbTextCompare)
            If intPos > 0 Then
                boolRet = True
                strOut = Mid(strFileText, intPos)
            End If
        End If
        ts.Close
        Set ts = Nothing
    End If
    ScanText = boolRet
End Function

Sub ScanFolder(strFolderPath, strFindPath, strSearch)
    Dim FSO
    Dim folA
    Dim filA
    Dim tsOut
    Dim strText

    Set FSO = Wscript.CreateObject("scripting.FileSystemObject")
    If FSO.FolderExists(strFolderPath) Then
        Set folA = FSO.GetFolder(strFolderPath)
        For Each filA In folA.Files
            If ScanText(FSO, filA.Path, strSearch, strText) Then
                Set tsOut = FSO.CreateTextFile( _
                  FSO.BuildPath(strFindPath, "XXX" & filA.Name), _
                  True, False)
                tsOut.Write strText
                tsOut.Close
            End If
        Next
    End If
    Set folA = Nothing
    Set filA = Nothing
    Set tsOut = Nothing
    Set FSO = Nothing
End Sub

ScanFolder InputBox("Search Folder"), _
  InputBox("Output Directory"), _
  InputBox("Search Text")



Thu, 02 Dec 2004 22:54:45 GMT  
 Selecting Text from one file and copying to a different file


Quote:


>>>>>>>>>>>>>>>Code Follows<<<<<<<<<<<<<<<<<<<<<<<<<<<

>Function ScanText(FSO, strFilePath, strSearch, strOut)
>    Dim boolRet
>    Dim strFileText
>    Dim ts As TextStream

Change the last line above to:

     Dim ts

I wrote it in my VB6 IDE and forgot to take out the TextStream before
I posted it.



Thu, 02 Dec 2004 22:58:40 GMT  
 Selecting Text from one file and copying to a different file
Thanks for this. If the file I want to open is a word doc or an XML file
this piece of code fails. Is there a differnet method that needs to be used
to open XML or Word files?

cheers

Ken

Quote:




> >>>>>>>>>>>>>>>Code Follows<<<<<<<<<<<<<<<<<<<<<<<<<<<

> >Function ScanText(FSO, strFilePath, strSearch, strOut)
> >    Dim boolRet
> >    Dim strFileText
> >    Dim ts As TextStream

> Change the last line above to:

>      Dim ts

> I wrote it in my VB6 IDE and forgot to take out the TextStream before
> I posted it.



Fri, 03 Dec 2004 23:55:22 GMT  
 Selecting Text from one file and copying to a different file
For /word/ doc I replied in another thread of yours. I comment only on xml.

Show one piece of your xml. And point the target string from which will be
selected. And clarify whether you want result text as well formed xml or as
any plain text.

--
Han Pohwan, Seoul/Korea


Quote:
> Thanks for this. If the file I want to open is a word doc or an XML file
> this piece of code fails. Is there a differnet method that needs to be
used
> to open XML or Word files?

> cheers

> Ken






> > >>>>>>>>>>>>>>>Code Follows<<<<<<<<<<<<<<<<<<<<<<<<<<<

> > >Function ScanText(FSO, strFilePath, strSearch, strOut)
> > >    Dim boolRet
> > >    Dim strFileText
> > >    Dim ts As TextStream

> > Change the last line above to:

> >      Dim ts

> > I wrote it in my VB6 IDE and forgot to take out the TextStream before
> > I posted it.



Sat, 04 Dec 2004 11:36:19 GMT  
 Selecting Text from one file and copying to a different file
You can use the Word object to search word documents. The scripts
should actually work for XML documents as they are text documents. The
script below is intended for text documents only.



Quote:
>Thanks for this. If the file I want to open is a word doc or an XML file
>this piece of code fails. Is there a differnet method that needs to be used
>to open XML or Word files?

>cheers

>Ken






>> >>>>>>>>>>>>>>>Code Follows<<<<<<<<<<<<<<<<<<<<<<<<<<<

>> >Function ScanText(FSO, strFilePath, strSearch, strOut)
>> >    Dim boolRet
>> >    Dim strFileText
>> >    Dim ts As TextStream

>> Change the last line above to:

>>      Dim ts

>> I wrote it in my VB6 IDE and forgot to take out the TextStream before
>> I posted it.



Sun, 05 Dec 2004 09:38:38 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Combining files with different header content into one file

2. Copying files from different locations to different locations

3. Creating word Macro to copy text and create new files from existing one

4. Help: Checking if *no* files in ListView are selected for file copying

5. Rich Text File (.RTF) -Can't copy multiple .rtf files

6. Rich Text File (.RTF) -Can't copy multiple .rtf files (VB 6.0 Enterprise)

7. Merge Text From Many Files To One File

8. Help storing text files in one .dat file

9. Merge Text From Many Files To One File

10. Find various text and copy the found text into different document

11. Copying a text file into a large text box

12. ONE included file in 30 different domains ...

 

 
Powered by phpBB® Forum Software