Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open a file with a wild card in VBS

Posted on 2010-09-08
5
Medium Priority
?
2,118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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