【问题标题】:sorting in visual basic?在visual basic中排序?
【发布时间】:2014-07-03 21:31:29
【问题描述】:

我有很多文件夹,每个文件夹中都有许多 HTML 文件,一个文件夹中有多达 70 个文件,我请朋友帮我对这些文件进行批量编辑,以将它们的背景和字体颜色更改为以及在底部添加一个链接以转到文件夹中的下一个文件,这是他发给我的 .. 这是一个 .vbs 文件

'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

并且在大多数情况下它工作得很好,除了它将 1 链接到 10 然后 11、12、... 19、2、20、21 等等我试图弄清楚如何修复它,所以链接从 1 到 2, 3, ... 9, 10, 11... 除了末尾的数字外,给定文件夹中的 HTML 文件名都相同 名称0.html 名称1.html 名称2.html ... 名称9.html 名称10.html 名称11.html ETC... 顺便说一句,html文件是由我下载的程序生成的,所以如果出现错误,我可以很容易地重新创建它们哦,我也想添加更改字体大小,但如果太麻烦,我可以轻松地继续使用缩放解决该问题的功能

在回应第一个答案时添加:

不,我无法控制原始程序编号的输出,但如果有人有一个快速 VBS 脚本将文件更改为 3 位数格式的数字,那将是一个可爱的解决方案

http://helloacm.com/bubble-sort-in-vbscript/ 这看起来可能是朝着正确方向迈出的一步? Sorting arrays numerically and alphabetically(like Windows Explorer) without using StrCmpLogicalW or shlwapi.dll - ASP.NET VB 还是这个?

【问题讨论】:

    标签: sorting vbscript html4


    【解决方案1】:

    问题在于,在声明Set objFolder = objFSO.GetFolder(fFolder.Path) 中并没有真正快速简便的方法来对输出文件的结果进行排序。见this other answer on this site

    发生的情况是,当该命令读取文件时,它实际上是 1、10、11、19、2、20 等,就像您正在经历的那样。

    您所说的程序会生成这些文件。是否有可能使用零文件保存它们。例如。名称00.html、名称01.html、名称02.html?如果是这样,那将是要走的路,那么您的朋友脚本将按设计工作。或者,如果它不会伤害任何东西,您也可以在您的程序创建这些 HTML 文件之后重命名这些文件。但长话短说,您要么需要修改代码以便它为您对文件进行排序,要么您可以重命名文件,以便它们按照您期望的顺序进入程序。

    【讨论】:

    • 不,我无法控制程序输出的编号部分,我将如何查看您提供的链接,看看我是否能从中找出一些东西
    • 当然看起来它可能是或至少接近主题,但我阅读和理解 VBS 的能力不足以将其转化为我的问题的正确答案我去尝试和研究更多关于VBS
    • 是否有一个 VBS 库,我可以在其中查找不同的命令及其语法?
    • 那里有很多资源。对于此脚本,您会特别感兴趣的几件事是更多关于 Scripting.FileSystemObject 此处msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx,然后了解有关其属性和方法的更多信息。我也在我的博客上整理了一些教程。 seanwoodward-public.sharepoint.com/Blog/Category/6/vbscript 从最早的帖子“vbscript - 编写和执行”开始,然后按原路返回,完成我整理的所有教程。
    • 这个博客比微软图书馆的链接更有帮助,尽管到目前为止都没有给我足够的解决问题的方法,不过还是谢谢
    【解决方案2】:

    同一位朋友终于开始修复代码,这是最终的工作版本

    '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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 2016-07-10
      • 1970-01-01
      相关资源
      最近更新 更多