2

I've got lots of folders each with a number of HTML files in them some as many as 70 files in a folder and asked a friend to help me make a bulk edit of these files to change their background and font colors as well as add a link at the bottom to go to the next file in the folder and this is what he sent me .. it was a .vbs file

'Here are the settings
'Be warned this is old fashioned preHTML5 stuff no css. But well I guess it could be implemented as well
'I think most of the replacements are pretty straight forward
'Run this script in a folder with all the files for one story
'Running it more then once can have unforseen consequences :)
background="black"
foreground="white"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSuperFolder = objFSO.GetFolder(".")
Call ShowFiles (objSuperFolder)

WScript.Quit 0


Sub ShowFiles(fFolder)
Set objFolder = objFSO.GetFolder(fFolder.Path)
Set colFiles = objFolder.Files

Dim a(50000)
Dim b(50000)
i = 1
For Each objFile In colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "HTML" Then
    a(i) = objFile.Path
    b(i)=objFile.Name
    i = i + 1
End If
Next

j=i

For z = 1 To j-1
        Set objFile2 = objFSO.OpenTextFile(a(z), 1)
        strText = objFile2.ReadAll
        strText = Replace(strText, "<body>", "<body bgcolor=""" +background+""">")
        strText = Replace(strText, "<html>", "<html><font color=""" +foreground+""">")
        strText = Replace(strText, "</html>", "</font></html>")
        'Add the link to next chapter
        If z < j-1 Then
            strText = Replace(strText,"</body>","<a href="""+b(z+1)+""">Link to next chapter!</a></body>")
        End If
        objFile2.Close
        Set objFile2 = objFSO.OpenTextFile(a(z), 2)
        objFile2.Write strText
        objFile2.Close
Next
End Sub

and for the most part it works great except that it linked 1 to 10 then 11, 12, ... 19, 2, 20, 21 and so on i'm trying to figure out how to fix it so the links go from 1 to 2, 3, ... 9, 10, 11... the HTML file names are all the same in a given folder except the number at the end Name0.html Name1.html Name2.html ... Name9.html Name10.html Name11.html etc... BTW the html files are generated by a program i downloaded so i can recreate them easy enough if a mistake is made oh and i also wanted to add changing the font size as well but if it's to much trouble i can easily continue to use the zoom feature to work around that

Added in respond to the first answer:

no i can not control the output of the original programs numbering though if someone had a quick VBS script to change the files to a 3 digit format for the numbers that'd be a lovely workaround solution

http://helloacm.com/bubble-sort-in-vbscript/ This looks like it might be a step in the right direction maybe? Sorting arrays numerically and alphabetically(like Windows Explorer) without using StrCmpLogicalW or shlwapi.dll - ASP.NET VB or this?

Community
  • 1
  • 1

2 Answers2

0

The problem is that in the statement Set objFolder = objFSO.GetFolder(fFolder.Path) there is not really a quick and easy way to sort the results of the files that come out. See this other answer on this site.

What is happening is that when the files are being read by that command it is in fact the 1, 10, 11, 19, 2, 20 and etc like you are experiencing.

The program that you say produces these files. Is it possible such that they could be saved using a zero file. E.g. Name00.html, Name01.html, Name02.html? If so then that would be the way to go then your friends script will work as designed. Or if it won't hurt anything you could also rename the files after your program created these HTML files as well. But long story short you either need to modify the code so that it sorts the files for you, or you can rename the files such that they will come to the program in the order that you are expecting.

Community
  • 1
  • 1
Sean W.
  • 863
  • 5
  • 14
  • no i have no control over the numbering part of the output of the program how ever i'll take a look at the link you provided and see if i can figure out something from that – Chichi90504 Jul 04 '14 at 01:56
  • certainly looks like it might be on or at least nearly on topic but my ability to read and understand VBS is insufficient to turn that into a proper answer for my issue off i go to try and research more about VBS – Chichi90504 Jul 04 '14 at 02:20
  • is there a VBS library some where i can look up the different commands and their syntax in? – Chichi90504 Jul 04 '14 at 02:32
  • There are plenty of resources out there. A couple of things that you would be interested in especially for the purposes of this script is more about the Scripting.FileSystemObject here http://msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx and then learn more about its properties and methods. I have also been putting together a couple of tutorials on my blog as well. https://seanwoodward-public.sharepoint.com/Blog/Category/6/vbscript start at the earliest post "vbscript - writing and executing" and then work your way back up to go through all of the tutorials that I have put together. – Sean W. Jul 04 '14 at 02:40
  • the blog was more helpful than the microsoft library link though neither gives me enough to fix the problem so far thanks anyways though – Chichi90504 Jul 04 '14 at 03:21
0

Same friend finally got around to fixing the code up and here is the final working version

'Here are the settings
'Be warned this is old fashioned preHTML5 stuff no css. But well I guess it could be implemented as well
'I think most of the replacements are pretty straight forward
'Run this script in a folder with all the files for one story
'Running it more then once can have unforseen consequences :)
background="black"
foreground="white"
size="6" 'Setting go from 1 to 7

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSuperFolder = objFSO.GetFolder(".")
Call ShowFiles (objSuperFolder)

WScript.Quit 0


Sub ShowFiles(fFolder)
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    Set colFiles = objFolder.Files

    Set objRE = New RegExp

    With objRE
        .Pattern    = "(\d*)\.html"
        .IgnoreCase = True
        .Global     = False
    End With

    Dim a(50000)
    Dim b(50000)

    'From here on there is the rename part
    Dim arr(3)
    arr(0)="0000"
    arr(1)="000"
    arr(2)="00"
    arr(3)="0"
    For Each objFile In colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "HTML" Then
            Set objMatch = objRE.Execute( objFile.Name )
            If objMatch.Count = 1 Then
                Dim ll,sttt
                sttt=objMatch.Item(0).Submatches(0)
                ll=Len(sttt)
                'WScript.Echo "Old name" & objMatch.Item(0)
                strNewName = objRE.Replace( objFile.Name, arr(ll)&sttt&".html")
                'WScript.Echo "New name" & strNewName
                objFile.Name=strNewName
            End If
        End If
    Next
    ''The renaming ends here and we're on to business as usual

    i = 1
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "HTML" Then
            a(i) = objFile.Path
            b(i)=objFile.Name
            i = i + 1
        End If
    Next

    j=i

    For z = 1 To j-1
            Set objFile2 = objFSO.OpenTextFile(a(z), 1)
            strText = objFile2.ReadAll
            strText = Replace(strText, "<body>", "<body bgcolor=""" +background+""">")
            strText = Replace(strText, "<html>", "<html><font size="""+size+""" color=""" +foreground+""">")
            strText = Replace(strText, "</html>", "</font></html>")
            'Add the link to next chapter
            If z < j-1 Then
                strText = Replace(strText,"</body>","<p><a href="""+b(z+1)+""">Link to next chapter!</a></p></body>")
            End If
            objFile2.Close
            Set objFile2 = objFSO.OpenTextFile(a(z), 2)
            objFile2.Write strText
            objFile2.Close
    Next
End Sub