Board logo

标题: [问题求助] 代码换位则无法正常输出 [打印本页]

作者: securite    时间: 2015-2-8 19:04     标题: 代码换位则无法正常输出

如下是正常的代码
  1. Rem MaximeSkipLineToLookFor3GB.vbs
  2. Dim strSearch
  3. Dim strText
  4. Dim strFile
  5. Dim objFSO
  6. Dim objFile
  7. Const ForReading=1
  8. Const ForWriting=2
  9. Const ForAppending=8
  10. strFile="c:\kugou.ini"
  11. subLook
  12. Function funLookUp(strText,strSearch)
  13. Const blnInsensitive=1
  14. If Instr(1,strText,strSearch,blnInsensitive) Then
  15. funLookUp=strSearch & " was found"
  16. Else
  17. funLookUp=strSearch & " was not found"
  18. End If
  19. End Function
  20. Sub subLook
  21. Dim strLine
  22. Dim strText
  23. Dim intLine
  24. strSearch="["
  25. Set objFSO=CreateObject("Scripting.FileSystemObject")
  26. Set objFile=objFSO.OpenTextFile(strFile,ForReading)
  27. Do Until objFile.AtEndOfStream
  28. strText=objFile.ReadLine
  29. strLine=funLookUp(strText,strSearch)
  30. If InStr(strLine,"not") Then
  31. intLine=objFile.Line-1
  32. WScript.Echo intLine & " not at the beginning of the ini"
  33. Else
  34. intLine=objFile.Line-1
  35. Wscript.Echo intLine & " is the beginning of the ini"
  36. objFile.Close
  37. Exit Do
  38. End If
  39. Loop
  40. WScript.Echo "Opening the file a second time..."
  41. Set objFile=objFSO.OpenTextFile(strFile,ForReading)
  42. Do Until objFile.AtEndOfStream
  43. If objFile.Line < intLine Then
  44. objFile.SkipLine
  45. ElseIf objFile.Line=intLine Then
  46. WScript.Echo "The beginning of the ini file is: "
  47. WScript.Echo objFile.ReadLine
  48. Else
  49. WScript.Echo "The script is over"
  50. objFile.Close
  51. WScript.Quit
  52. End If
  53. Loop
  54. End Sub
复制代码
正确的输出是:
Microsoft (R) Windows Script Host Version 5.8
版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。

1 not at the beginning of the ini
2 not at the beginning of the ini
3 not at the beginning of the ini
4 not at the beginning of the ini
5 not at the beginning of the ini
6 not at the beginning of the ini
7 is the beginning of the ini
Opening the file a second time...
The beginning of the ini file is:
[ActiveTab]
The script is over

但是换一下,就是把sub函数中的第二个until循环放到外面,无法正常输出
  1. Rem MaximeSkipLineToLookFor3GB.vbs
  2. Dim strSearch
  3. Dim strText
  4. Dim strFile
  5. Dim objFSO
  6. Dim objFile
  7. Const ForReading=1
  8. Const ForWriting=2
  9. Const ForAppending=8
  10. strFile="c:\kugou.ini"
  11. subLook
  12. Set objFSO=CreateObject("Scripting.FileSystemObject")
  13. Set objFile=objFSO.OpenTextFile(strFile,ForReading)
  14. WScript.Echo "Opening the file a second time..."
  15. Do Until objFile.AtEndOfStream
  16. If objFile.Line < intLine Then
  17. objFile.SkipLine
  18. ElseIf objFile.Line=intLine Then
  19. WScript.Echo "The beginning of the ini file is: "
  20. WScript.Echo objFile.ReadLine
  21. Else
  22. WScript.Echo "The script is over"
  23. objFile.Close
  24. WScript.Quit
  25. End If
  26. Loop
  27. Function funLookUp(strText,strSearch)
  28. Const blnInsensitive=1
  29. If Instr(1,strText,strSearch,blnInsensitive) Then
  30. funLookUp=strSearch & " was found"
  31. Else
  32. funLookUp=strSearch & " was not found"
  33. End If
  34. End Function
  35. Sub subLook
  36. Dim strLine
  37. Dim strText
  38. Dim intLine
  39. strSearch="["
  40. Set objFSO=CreateObject("Scripting.FileSystemObject")
  41. Set objFile=objFSO.OpenTextFile(strFile,ForReading)
  42. Do Until objFile.AtEndOfStream
  43. strText=objFile.ReadLine
  44. strLine=funLookUp(strText,strSearch)
  45. If InStr(strLine,"not") Then
  46. intLine=objFile.Line-1
  47. WScript.Echo intLine & " not at the beginning of the ini"
  48. Else
  49. intLine=objFile.Line-1
  50. Wscript.Echo intLine & " is the beginning of the ini"
  51. objFile.Close
  52. Exit Do
  53. End If
  54. Loop
  55. End Sub
复制代码
错误的输出
Microsoft (R) Windows Script Host Version 5.8
版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。


1 not at the beginning of the ini
2 not at the beginning of the ini
3 not at the beginning of the ini
4 not at the beginning of the ini
5 not at the beginning of the ini
6 not at the beginning of the ini
7 is the beginning of the ini
Opening the file a second time...
The script is over

其中kugou.ini文件内容如下
Activate
Activate=1
DeepActivate=1
NewActivate=1
PlayCnt=12
[ActiveTab]
TabName=musicclub
[Collect]
NetConnectivity=1
[Crash]
LastCrashExe=
[DBCheck]
CommitError=0




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2