JubileeAnalyst
Technical User
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 = fspenTextFile(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
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 = fspenTextFile(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