Solved

Open a file with a wild card in VBS

Posted on 2010-09-08
5
1,890 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now