Find in text file using wildcards

stapuff

Well-known Member
Joined
Feb 19, 2004
Messages
1,126
I want to apologize in advance...I am not in a position to get to my computer or I would have either wrote this myself or atleast posted code I had tried.

I am looking for a script that loops through a text file looking for each function { and number each one of them.

Could look like
function {
function a() {
function (a) {
function () {
function a(b,c,d) { etc

so when it finds the first
function { it add ('1') after the {
second function { it add ('2') after the {

Any help would be greatly appreciated.

Stapuff
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
this is the long drawn out way of doing it and only goes to five currently and only looks for "{" not sure this is going to work for what ever you are trying to do but it might push you in the right direction for creating a script in VBA.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"{","{"&1,1),"{","{"&2,2),"{","{"&3,3),"{","{"&4,4),"{","{"&5,5)
 
Upvote 0
It's a curious question, stapuff. Wouldn't something like that ruin all those functions with bad syntax?
 
Upvote 0
Drrellik - thanks for the suggestion. Did it with a nested if statement, but not exactly what I am after.

Xenou - it has been awhile. To your question yes it would as is, but I didn't have the correct syntax at the time of the post. Should have been console.log('1'); Basically I need to add a sequential console.log to each function in a javascript file so when I run it through the developers console I can see what functions are being used and remove the ones that are not.

Thanks,

stapuff
 
Upvote 0
Okay, lightly tested but try this. I have an "Excel vba" version first. But for preference maybe use the VBScript version, which you can just save as a file with .vbs extension in the folder with the javascript files.

You must edit the code to give it the right folder to work in. It will create a temp folder with copies of all the functions with the logging, so you can see what the result is. Of course back up everything first!!
Code:
[COLOR="Navy"]Sub[/COLOR] foo()
[COLOR="Navy"]Dim[/COLOR] FSO [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'Scripting.FileSystemObject[/COLOR]
[COLOR="Navy"]Dim[/COLOR] ts(1) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'Scripting.TextStream[/COLOR]
[COLOR="Navy"]Dim[/COLOR] fldrSrc [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'Scripting.Folder[/COLOR]
[COLOR="Navy"]Dim[/COLOR] fldrDest [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'Scripting.Folder[/COLOR]
[COLOR="Navy"]Dim[/COLOR] f [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'Scripting.File[/COLOR]
[COLOR="Navy"]Dim[/COLOR] re [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR] [COLOR="SeaGreen"]'RegExp[/COLOR]
[COLOR="Navy"]Dim[/COLOR] fName [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] s [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] SearchInFolder [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]

    
    [COLOR="SeaGreen"]'//SET THIS VARIABLE TO THE NAME OF THE FOLDER WITH THE JS FILES IN THEM[/COLOR]
    SearchInFolder = "C:\MyTemp\Test"
    
    
    [COLOR="SeaGreen"]'//Create Objects[/COLOR]
    [COLOR="Navy"]Set[/COLOR] FSO = CreateObject("Scripting.FileSystemObject")
    [COLOR="Navy"]Set[/COLOR] re = CreateObject("VBScript.RegExp")
    [COLOR="Navy"]With[/COLOR] re
        .Global = False
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "^\s*function.*\{"
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
    
    
    [COLOR="SeaGreen"]'//Set Folders[/COLOR]
    [COLOR="Navy"]Set[/COLOR] fldrSrc = FSO.GetFolder(SearchInFolder)
    [COLOR="Navy"]Set[/COLOR] fldrDest = FSO.CreateFolder(SearchInFolder & "\" & Replace(FSO.GetTempName, ".tmp", ""))
    
    
    [COLOR="SeaGreen"]'//Search for files, make replacements, and write new files in Temp folder[/COLOR]
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] f [COLOR="Navy"]In[/COLOR] fldrSrc.Files
        
        [COLOR="Navy"]If[/COLOR] FSO.GetExtensionName(f.Path) = "js" [COLOR="Navy"]Then[/COLOR]
            
            [COLOR="SeaGreen"]'//Note: Set last argument of these next two methods to -1 for Unicode[/COLOR]
            [COLOR="Navy"]Set[/COLOR] ts(0) = FSO.OpenTextFile(f.Path, 1, False, -2)
            [COLOR="Navy"]Set[/COLOR] ts(1) = FSO.CreateTextFile(fldrDest.Path & "\" & FSO.GetFileName(f.Path), False, -2)
            [COLOR="Navy"]With[/COLOR] ts(0)
                [COLOR="Navy"]Do[/COLOR] [COLOR="Navy"]While[/COLOR] [COLOR="Navy"]Not[/COLOR] ts(0).AtEndOfStream
                    s = .ReadLine
                    [COLOR="Navy"]If[/COLOR] re.Test(s) [COLOR="Navy"]Then[/COLOR]
                        i = i + 1
                        s = re.Replace(s, Left(s, Len(s) - 1) & "{ console.log(" & i & ");")
                    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
                    ts(1).WriteLine s
                [COLOR="Navy"]Loop[/COLOR]
            [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
            ts(0).Close
            ts(1).Close
        
        [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
    
    [COLOR="Navy"]Next[/COLOR] f

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]



Or, you can just drop this into a file with a vbs extension and run it directly without using Excel:
Code:
Call Foo()


'------------------------------------------------------------------------------
'Create Console Logging statements for each function in current folder JS files
'Functions nested in functions are ignored
Sub Foo()

    Dim ts(1)

    '//SET THIS VARIABLE TO THE NAME OF THE FOLDER WITH THE JS FILES IN THEM
    SearchInFolder = "C:\MyTemp\Test"
    
    
    '//Create Objects
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set re = CreateObject("VBScript.RegExp")
    With re
        .Global = False
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "^\s*function.*\{"
    End With
    
    
    '//Set Folders
    Set fldrSrc = FSO.GetFolder(SearchInFolder)
    Set fldrDest = FSO.CreateFolder(SearchInFolder & "\" & Replace(FSO.GetTempName, ".tmp", ""))
    
    
    '//Search for files, make replacements, and write new files in Temp folder
    For Each f In fldrSrc.Files
        
        If FSO.GetExtensionName(f.Path) = "js" Then
            
            '//Note: Set last argument of these next two methods to -1 for Unicode
            Set ts(0) = FSO.OpenTextFile(f.Path, 1, False, -2)
            Set ts(1) = FSO.CreateTextFile(fldrDest.Path & "\" & FSO.GetFileName(f.Path), False, -2)
            With ts(0)
                Do While Not ts(0).AtEndOfStream
                    s = .ReadLine
                    If re.Test(s) Then
                        i = i + 1
                        s = re.Replace(s, Left(s, Len(s) - 1) & "{ console.log(" & i & ");")
                    End If
                    ts(1).WriteLine s
                Loop
            End With
            ts(0).Close
            ts(1).Close
        
        End If
    
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top