Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open a file with a wild card in VBS

Posted on 2010-09-08
5
Medium Priority
?
2,174 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

782 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