Solved

Open a file with a wild card in VBS

Posted on 2010-09-08
5
1,980 Views
Last Modified: 2012-05-10
Below is my VBScript to open and do something to a file.
Since VBS does not easily support wild cards, how would I be able to code around that problem in this instance. The file name that I need to open is POD2_h837.20100902.04365 and it changes daily.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("*h837*", 1)
strSearchString = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True  
objRegEx.Pattern = "BHT"
strNewText = objRegEx.Replace(strSearchString,VBCrlf & "BHT" & VBCrLf)

Set objOutFile = objFSO.CreateTextFile("837a.txt", 2)
objOutFile.WriteLine strNewText
objOutFile.Close
0
Comment
Question by:tonydemarco
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:MaxSoullard
ID: 33628034
Is there any constant on the file name? Give a few other examples of other names
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33628058
Max,

Yes, "h837"
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33628076
Max,

Longer answer:

POD1_h837.20100902.04365
POD2_h837.20100903.04366
POD3_h837.20100904.04367
POD4_h837.20100905.04368 . . . etc
0
 
LVL 5

Accepted Solution

by:
MaxSoullard earned 500 total points
ID: 33628319
OK, try this.

Instantiate these variables with the correct paths

strFolderName = "SOURCE FOLDER"
strTargetFile = "TARGET PATH AND FILE NAME"

This script assumes that you have only one file in the source folder with "h837" in it. If you have more files containing "h837" let me know.

Const ForReading = 1
Const ForWriting = 2

strFolderName = "SOURCE FOLDER"
strTargetFile = "TARGET PATH AND FILE NAME"
strFileNameConstant = "h837"	
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")

arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
    strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"
 
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
    
    If InStr(Lcase(objFile.Name),LCase(strFileNameConstant)) Then
    
		Set objFile = objFSO.OpenTextFile(objFile.Name, 1)
		strSearchString = objFile.ReadAll
		objFile.Close
		Set objRegEx = CreateObject("VBScript.RegExp")
		objRegEx.Global = True  
		objRegEx.Pattern = "BHT"
		strNewText = objRegEx.Replace(strSearchString,VBCrlf & "BHT" & VBCrLf) 

    End If
Next

'Writes Target file
Set objFile = objFSO.OpenTextFile(strTargetFile, ForWriting, true)
objFile.WriteLine(strNewText)
objFile.Close

Open in new window

0
 
LVL 9

Author Closing Comment

by:tonydemarco
ID: 33629276
Excellent !!!

Thank You, Max
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question