0

嘿伙计们,我的 vbs 脚本有问题。我的脚本打开一个文本文件读取它并删除一些条目。我的脚本中的某个地方一定是一个错误,因为源文件的最后一行在目标文件中写入了大约 30 次。我在我的代码中没有发现错误,也许你们中的某个人看到了它。是的,我知道我不应该发布大型代码部分。这是我的代码:

separator = " "
x = 0

strRawPath = "C:\xampp_neu\xampp\htdocs\tc_backup\stasknoheader.txt"
strRawPathW = "C:\xampp_neu\xampp\htdocs\tc_backup\stask.txt"

Set WSHShell = WScript.CreateObject("WScript.Shell") 
Set fs = CreateObject("Scripting.FileSystemObject")
Set fsw = CreateObject("Scripting.FileSystemObject")

        ' 2 = ForWriting
        Set f = fs.OpenTextFile(strRawPath,1)
        Set w = fsw.OpenTextFile(strRawPathW,2)

            Do While f.AtEndOfStream <> True
                x = x+1
                'Anlegen des Arrays
                ReDim Preserve myArray(x)
                strLine = f.Readline
                'Speichern in Array
                myArray(x) = strLine

                'Loop so that lines that contains Microsoft or TaskName are not written
                 If InStr(strLine, "Microsoft") = 0 Then
                    If InStr(strLine, "TaskName") = 0 Then
                        If InStr(strLine, "Restart System") = 0 Then
                            If InStr(strLine, "Scheduler-HSM-mig-TC11TDrive") = 0 Then

                        strNewLine = strNewLine & strLine & vbCrLf
                        'Replace blank with " AM, "
                        strNewLine =(Replace(strLine," ",separator,1,2))
                        'Removes \ from TaskName
                        strNewLine =(Replace(strNewLine,"\","",1,1))
                        'WScript.Echo strNewLine
                            End If
                        End If
                    End If
                End If                  

                w.write strNewLine & VbCrLf
                'WScript.Echo strNewLine

            Loop 

        f.Close
        w.Close
4

1 回答 1

1

虽然这不会解决您的问题,但会减少您的 If 条件。你不需要 4 if 条件。只需使用 And 运算符,如下所示

If InStr(strLine, "Microsoft") = 0 And InStr(strLine, "TaskName") = 0 And InStr(strLine, "Restart System") = 0 AndA  InStr(strLine, "Scheduler-HSM-mig-TC11TDrive") = 0 Then


                    strNewLine = strNewLine & strLine & vbCrLf
                    'Replace blank with " AM, "
                    strNewLine =(Replace(strLine," ",separator,1,2))
                    'Removes \ from TaskName
                    strNewLine =(Replace(strNewLine,"\","",1,1))
                    'WScript.Echo strNewLine

End If  
于 2013-03-22T09:58:40.247 回答