Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

Ignore locked files or invalid permissions when searching for string in files

$
0
0
VBScript 5.8 on Windows 2008 Server.

I copied some code from another source to look for certain strings in files, and whilst this code works, I wanted to open it up to search for all files within a given drive/directory.

This is the code that worked:

Code:

Dim FSO

Const START_DIR = "c:\"
Set FSO = CreateObject("Scripting.FileSystemObject")

set outFile = FSO.createTextFile( "c:\temp\findings.txt")

doFolder START_DIR

outFile.close



sub doFolder(path)
  ' WScript.echo "Doing folder " + path

  Dim folder, files, file, subfolders,subfolder

  set folder = FSO.getFolder(path)
  set files = folder.Files
  for each file in files
    doFile path, file.path
  next

  set subFolders = folder.subFolders
  For Each subfolder in subFolders
    doFolder subfolder.path
  Next

end sub

sub doFile( parent, path )
  ' WScript.echo "Found file " + path
  if right(lcase(path),4) = ".txt" then
    checklog parent, path
  end if
end sub

sub checklog(parent, path)
  ' Wscript.echo "log file " + path
  Set fl = FSO.opentextfile(path, 1)

  Do While fl.AtEndOfStream <> True
    line = fl.ReadLine
    if InStr(line, "searchstringhere" ) > 0 then
        output path
        Exit Do
    end if
  Loop
  fl.close
 
end sub

sub output(path)
  outFile.writeLine(path)
end sub

I edited the code to open up the search to check for all files:

Code:

sub doFile( parent, path )
  ' WScript.echo "Found file " + path
  ' if right(lcase(path),4) = ".txt" then
    checklog parent, path
  ' end if
end sub

Although this works to a point (until it errors on a file), it eventually bombs on file permissions errors and "Process cannot access a file because it is being used by another process" error for files such as .lck files.

Can anyone tell me how to code in an ignore/skip rule into this please?

Never touched VB before today, so be gentle!

Thanks in advance.

Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>