[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 代码换位则无法正常输出

如下是正常的代码
  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

返回列表