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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditionally moving files based on text string within file 2

Status
Not open for further replies.

JubileeAnalyst

Technical User
Jun 24, 2011
5
GB
Hi

My first thread here...

This is a similar thread to the one recently posted about searching for text within a text file and then executing a line of code or not depending on whether the text string was found.

I want to achieve something similar.

Basically I want to test all files in a given folder for 3 or 4 possibilities of text string. Then (only on the ones which match) CUT and paste the files into another folder for archiving. The files not containing matches to any of the text strings are to be untouched.

I have ZERO experience with VBS. Only VBA. My main area of expertise is T-SQL.

However I have managed to cobble together something which works but doesn't quite do what I want.

The code below
Searches a SPECIFIC file in the desired location. I need to search all files.
It cuts and pastes only that one file. I need to cut and paste all files which match the text string.
It tests for only ONE text string. I need to test for 3 or 4.
It names the file "DestinationFile.txt". I need it to leave the filename untouched.

Any help on this is greatly appreciated :)

Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
Dim StrDestinationFileName

Const ForReading = 1
Dim sReadAll, sInputFile, sSearchString
sInputFile = "\\servername\foldername\TestFile1.txt"
sSearchString = "looking for this text string"

Dim fso, f, objShell
Set objShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile(sInputFile, ForReading)
sReadAll = f.ReadAll

StrSourceLocation = "\\servername\foldername"
StrDestinationLocation = "C:\LogTesting"
StrSourceFileName = sInputFile
StrDestinationFileName = "DestinationTest.txt"

'Creating the file system object
Set fso = CreateObject("Scripting.FileSystemObject")

If InStr(sReadAll, sSearchString) > 0 Then
fso.CopyFile StrSourceFileName, StrDestinationLocation & "\" & StrDestinationFileName, False
Else
wscript.echo "String was not found."
End If
 
For starters, you could use fso.MoveFile instead of fso.CopyFile. This will resolve the file naming issue and you could eliminate the strDestinationFileName stuff.


Beyond that you will need to add a loop to check all the files and another loop (or robust regex) to catch all the search term variations. The structure of the script in the other thread is a good start.
 
Something like this:
It iterates all files found in "sInputFolder", and if any of the search strings are found, the file is moved to StrDestinationLocation.

Code:
Option Explicit
Const ForReading = 1

Dim StrDestinationLocation
StrDestinationLocation = "C:\MyFolder"

Dim sInputFolder
sInputFolder = "\\servername\foldername"

Dim sSearchString1, sSearchString2, sSearchString3, sSearchString4
sSearchString1 = "search string 1"
sSearchString2 = "search string 2"
sSearchString3 = "search string 3"
sSearchString4 = "search string 4"

Dim fso, oFolder, oFile, fil, sReadAll
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(sInputFolder)
For Each fil in oFolder.Files
   Set oFile = fso.OpenTextFile(fil.Path, ForReading) 
   sReadAll = oFile.ReadAll
   If InStr(sReadAll, sSearchString1) > 0 Or _
      InStr(sReadAll, sSearchString2) > 0 Or _
      InStr(sReadAll, sSearchString3) > 0 Or _
      InStr(sReadAll, sSearchString4) > 0 Then	
      fso.MoveFile fil.Path & ", " & StrDestinationLocation
   End If
Next
 
Hey Guitarzan

thanks for the code!

I had to alter it slightly to work.

I had to change the
StrDestinationLocation = "C:\MyFolder"
to
StrDestinationLocation = "C:\MyFolder\"

and change
fso.MoveFile fil.Path & ", " & StrDestinationLocation
to
fso.MoveFile fil.Path, StrDestinationLocation

Any thoughts on why this is so?

kind regards and thanks again!
Paul
 
Glad it worked... regarding the backslash on the folder... I guess MoveFile interprets the second parameter as a file, unless you include the backslash.

As for the comma, that another oversight on my part... I threw that in when I was testing and forgot to remove it. Glad you figured out the proper syntax though :)
 
Hey everyone

I thought I got everything working. But alas no...

Upon execution an error appear saying "Access Denied". I tried just the same script but with CopyFile instead of MoveFile. It works fine. I assumed that when IT applied elevated privileges then it would work. But no, same "Access Denied" message for them! :-/

Looked on Dr Google for answers, found mention of "Move" instead of "MoveFile". So I tried that, still didn't work but a different message this time "Object doesn't support this property or method - Move"

So I'm back here looking for help.

Hope you can

kind regards
Paul
 
...
sReadAll = oFile.ReadAll
[!]oFile.Close[/!]
If InStr(sReadAll, sSearchString1) > 0 Or _
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top