Torgeir
Mon Mar 14 07:10:49 CST 2005
ALI-R wrote:
> Sorry,but I think I didn't descibe the problem properly,I am trying
> to call your batch file from my VBScript code,dose what you sent me
> actually returns the name of the files to the caller of the batch code?
>
> My problem now is two things:
>
> 1) how to call this batch file from my vb script code
> 2) how to return the name of the files from batch file to the caller
> (vb script code)
Hi
Here you go:
'--------------------8<----------------------
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
sBatFile = "c:\Scripts\MySearch.bat"
sFilePattern = "c:\tst\*.txt"
sTextToSearchFor = "Jack and Jill went up the hill"
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FileExists(sBatFile) Then
MsgBox "Could not find batch file, quitting!", _
vbCritical + vbSystemModal, "Text search"
WScript.Quit
End If
sBatFileShort = oFSO.GetFile(sBatFile).ShortPath
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
oShell.Run "%comspec% /c " & sBatFileShort & " """ & sFilePattern _
& """ """ & sTextToSearchFor & """ >" & sTempFile, 0, True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)
' If text string is found several times in the same file, duplicate
' lines are created in the output file as well, using a dictionary
' object to avoid those duplicates
Set dicFileList = CreateObject("Scripting.Dictionary")
dicFileList.CompareMode = vbTextCompare
Do While fFile.AtEndOfLine <> True
sLine = fFile.ReadLine
If sLine <> "" Then
If Not dicFileList.Exists(sLine) Then
dicFileList.Add sLine, ""
End If
End If
Loop
fFile.Close
oFSO.DeleteFile(sTempFile)
If dicFileList.Count = 0 Then
WScript.Echo "No files found!"
Else
' enumerate the dictionary object, echoing the key value:
For Each sFile In dicFileList
WScript.Echo sFile
Next
End If
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx