Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

n00b needs basic script! please help! :)

Status
Not open for further replies.

scanjam

Technical User
Oct 30, 2003
244
GB
hey hey list legends,

Im desperately seeking a basic script that will

trawl through a primary directory and all subdirectories for a variable (*.txt or *.ini)

for each of the * files found extract 2-3 lines of specific code

(they willbe like, FTP_Address= and JOE_FREDDY=)

Then get the path of the file; the extracted text and export to a new file for all matches


I though this was going to be easy but being a beginner to VBSCripting i have failed miserably..

anyone have anything similar they would care to share?
Sincerely and thankyou

SCANJAM
 
Someone else asked for something similar. This function traverses a directory and subdirectories for a certain extension. It returns an "itemized" string of all matching objects. From here, you can parse each line and extract the data that you need.

Code:
set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

function searchForExt(strDir, strExt)
   set objFolder = objFSO.GetFolder(strDir)
   for each objSubFolder in objFolder.SubFolders
      strResults = searchForExt (objSubFolder.Path, strExt)
   next
	
   for each objFile in objFolder.Files
      if (right(objFile.Path, len(strExt)) = strExt) then strResults = strResults & objFile.Path & vbNewLine
   next
  searchForExt = strResults
end function

strFiles = searchForExt("C:\windows", ".txt")

For each line in strFiles, parse out the file name and open the file for reading (objFOS.OpenTextFile). Traverse through the file content looking for a particular block of code.

-Geates

PS. This is one of my most visited VBS reference sites.
 
Geates, I'd replace this:
if (right(objFile.Path, len(strExt)) = strExt)
with this:
If UCase(Right(objFile.Path, Len(strExt))) = UCase(strExt)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
tragically all i hve is this


Dim oFSO, sFile1, sFile2, oFile1, oFile2, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile1 = "c:\test\file1.txt"
sFile2 = "c:\test\file2.txt"
Set oFile1 = oFSO.OpenTextFile(sFile1, 1)
Set oFile2 = oFSO.OpenTextFile(sFile2, 8)
Do While Not oFile1.AtEndOfStream
sText = Trim(ofile1.ReadLine)
If InStr(sText, "FTP_Server=") <> 0 Then
oFile2.WriteLine sText1
End If
Loop
 
tragically all i hve is this but need a variable on the file that it fnids (ie *.ini anywhere under d:
ftp) grrrrrr wish i was better at this.. :)


Dim oFSO, sFile1, sFile2, oFile1, oFile2, sText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile1 = "c:\test\file1.txt"
sFile2 = "c:\test\file2.txt"
Set oFile1 = oFSO.OpenTextFile(sFile1, 1)
Set oFile2 = oFSO.OpenTextFile(sFile2, 8)
Do While Not oFile1.AtEndOfStream
sText = Trim(ofile1.ReadLine)
If InStr(sText, "FTP_Server=") <> 0 Then
oFile2.WriteLine sText1
End If
Loop
 
you are definately on the right track! Ecapsulate your code with a loop.

Code:
set objOutput = oFSO.OpenTextFile(strOutputFile, 2, true) (the 3rd argument is to create the file if it doesn't already exist)
strFiles = searchForExt("C:\windows", ".txt")
do while len(strFiles)
   strFile = left(strFiles, inStr(strFiles, vbNewLine)
   strResults = strFile & ":" & vbNewLine
   set objFile = objFSO.OpenTextFile(strFile, 1)
   do while not objFile.AtEndOfStream
      strLine = objFile.ReadLine
      if (inStr(strLine, "FTP_Server=")) then
         strResults = strResults & strLine & vbNewLine
      end if
   loop
   objFile.close
   objResults.WriteLine strResults
   strFiles = mid(strFiles, len(strFile))
loop

objResults.close

This code is untested. Because searchForExt returns a string, parsing the string to traverse the items is important (strFiles = mid(strFiles, len(strFile))). Otherwise the loop is infinate. It will probably need some tweaking for your purposes.

-Geates
 
If i wanted to output

The Full files path, FTP_Server(Everything after on this line) then the same for FTP_User

Can it be as simple as:


if (inStr(strLine, "FTP_Server=", "FTP_User")) then
strResults = strResults & fso.GetAbsolutePathName() & strLine & vbNewLine

will this recurse subdirectories?

THank you so much for your help - sorry for the simple questions - this really isnt my forte - but with the 10,000 files I have to trawl through this really is the only way... sigh :)


THANK YOU THANK YOU THANK YOU
 
Notice that the searchForExt function compiles a list of files by using it's full path (objFile.Path). The main code chuck parses the results and returns the full path of the file in strFile. No need for fso.GetAbosultePathName.
Simply add strFile to strResults.

Also, the function inStr will not accept multiple comparisons (inStr(strLine, "FTP_Server=", "FTP_User")) so you'll have the make each comparison individually.

Code:
if (inStr(strLine, "FTP_Server=")) then strResults = strResults & strLine & vbNewLine
if (inStr(strLine, "FTP_User=")) then strResults = strResults & strLine & vbNewLine

Finally, notice that strResults is redefined just prior to traversing a file's content. Adding strLine to strResults is redundant - unless that's what you want. With all the code put together, your output file should look like this:

c:\temp\file.txt
FTP_Server=abc

c:\temp\file2.txt
FTP_User=def

c:\temp\file3.txt
FTP_Server=xyz
FTP_User=uvw

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top