.vbs帮助我通过一个目录循环

时间:2010-09-01 17:51:13

标签: vbscript windows-scripting

我写了一个有效的脚本。它现在做的是通过目录查找给定文件并返回第二行第四个选项卡(RXC193)上的内容,并将文件重命名为它从以下文件中找到的文件:

@Program @RxBIN @RXPCN @RxGroup @MemberID @WebsiteE @WebsiteS @VerticalLogo @TextLogo
RXCUT 013824 RXCUT RXC193 RXC5FHXF9 www.rxcut.com/HBG www.rxcut.com/HBG/es P:\ RxCut \ In Design Implementation \ RXC193

我需要这个脚本能够循环遍历目录并通过此RXC重命名所有文件#####。这是脚本:

Call TwoDimensionArrayTest

Sub TwoDimensionArrayTest
' Version 1.0
' Writtem by Krystian Kara
' Dated 25-Jan-2009


    Dim fso
    Dim oFile
    Dim arrline
    Dim arrItem
    Dim objFolder
    Dim i
    Dim arrMain()
    Dim sFileLocation, strResults

    Const forReading = 1

' The file contains on each line:
    ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
    ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
'etc etc


    Set fso = CreateObject("Scripting.FileSystemObject")
        sFileLocation = "file 2.txt"

        Set oFile = fso.OpenTextFile(sFileLocation, forReading, False)

    Do While oFile.AtEndOfStream <> True
        strResults = oFile.ReadAll
    Loop

' Close the file
    oFile.Close

' Release the object from memory
    Set oFile = Nothing

' Return the contents of the file if not Empty
    If Trim(strResults) <> "" Then

        ' Create an Array of the Text File
        arrline = Split(strResults, vbNewLine)
    End If

    For i = 0 To UBound(arrline)
        If arrline(i) = "" Then
            ' checks for a blank line at the end of stream
            Exit For
        End If 

        ReDim Preserve arrMain(i)

            arrMain(i) = Split(arrline(i), vbTab)

    Next

    fso.MoveFile "file 2.txt", arrMain(1)(3) & ".txt"

End Sub ' TwoDimensionArrayTest

提前致谢, 乔

2 个答案:

答案 0 :(得分:0)

一种方法是在子程序中参数化文件名,以便可以为不同的文件多次调用它,如下所示:

Sub TwoDimensionArrayTest(fileName) 'you may want a more descriptive name

    ' ...
    sFileLocation = fileName
    ' ...

End Sub

然后,写一个遍历你的目录的循环,每次调用你的sub:

Dim fso, folder

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("Your Folder Name")
For Each file In folder.Files
    TwoDimensionArrayTest file.Path
Next

答案 1 :(得分:0)

这是最终错误免费代码!最后让它搜索我的Tab-delimited.txt文件目录并从第二行抓取第三个选项卡(组号),然后将文件重命名为其相应的组号! YAY!

heres最终错误免费代码!:

Call TwoDimensionArrayTest

Sub TwoDimensionArrayTest

Dim fso     朦胧的     昏暗的arrline     昏暗的arrItem     朦胧我     昏暗的arrMain()     Dim sFileLocation,strResults

Const forReading = 1

strFolder =“C:\ Documents and Settings \ jmituzas.NMCLLC \ Desktop \ desktop2 \ New Folder(2)\ datafiles” 设置objFSO = CreateObject(“Scripting.FileSystemObject”) 对于每个objFile在objFSO.GetFolder(strFolder).Files     如果正确(LCase(objFile.Name),4)= LCase(“。txt”)那么

    ' The file contains on each line:
' Text1 (tab) Text2 (tab) Text3 (tab) Text4
' Text5 (tab) Text6 (tab) Text7 (tab) Text8

'等等

设置fso = CreateObject(“Scripting.FileSystemObject”)         sFileLocation = objFile.Name

    Set oFile = fso.OpenTextFile(objFile.Name, forReading, False)

Do While oFile.AtEndOfStream <> True
    strResults = oFile.ReadAll
Loop

' Close the file
oFile.Close

'从内存中释放对象     设置oFile = Nothing

'如果不是空,则返回文件的内容     如果修剪(strResults)&lt;&gt; “”那么

    ' Create an Array of the Text File
    arrline = Split(strResults, vbNewLine)
End If

For i = 0 To UBound(arrline)
    If arrline(i) = "" Then
        ' checks for a blank line at the end of stream
        Exit For
    End If 

    ReDim Preserve arrMain(i)

        arrMain(i) = Split(arrline(i), vbTab)

Next

  fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"

结束如果  下一个 结束子'TwoDimensionArrayTest

相关问题