I have a script that I have created to test a drive to see if it has to be defragged. I am able to run the defrag to analyze the disk without an issue. I would like to then read the text file output to see if the drive must be defragged. The file that is output contains the correct string I am searching for, but I can't get to recognize it. I have included 2 scripts, the first just verifies it and reads through line by line of the text file. The second is to test if the drive needs to be defragged. When I run it through a debugger it chooses NO 7 times for each line it reads. Yes and No are just temporary to test it. Any help would be appreciated.
Option Explicit
On Error Resume Next
'declare variables
Dim DefragAnalyze
Dim ObjShell
Dim ObjFso
Dim ObjReadFile
Dim FileContents
Dim strLine
Dim i
Const TimeOut = 60000
Const ForReading = 1
i = 0
DefragAnalyze = "c:\dskanalyze.txt"
FileContents = "You should defragment this volume."
'analyze disk to see if defragmenting is needed
set ObjShell = WScript.CreateObject("Wscript.Shell")
ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -a >c:\dskanalyze.txt")
'pauses the script
WScript.Sleep TimeOut
'checks file and then runs defrag if necessary
Set ObjFso = WScript.CreateObject("Scripting.Filesystemobject")
Set ObjReadFile = ObjFspenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
i = i + 1
strLine = ObjReadFile.ReadLine
WScript.Echo "Line " & i & ": " & strLine
Loop
------------------------------------------------------
Option Explicit
On Error Resume Next
'declare variables
Dim DefragAnalyze
Dim ObjShell
Dim ObjFso
Dim ObjReadFile
Dim FileContents
Dim strLine
Dim i
Const TimeOut = 60000
Const ForReading = 1
i = 0
DefragAnalyze = "c:\dskanalyze.txt"
FileContents = "You should defragment this volume."
'analyze disk to see if defragmenting is needed
set ObjShell = WScript.CreateObject("Wscript.Shell")
ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -a >c:\dskanalyze.txt")
'pauses the script
WScript.Sleep TimeOut
'checks file and then runs defrag if necessary
Set ObjFso = WScript.CreateObject("Scripting.Filesystemobject")
Set ObjReadFile = ObjFspenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
strLine = ObjReadFile.ReadLine
If strLine = FileContents Then
WScript.Echo "Yes"
Else
WScript.Echo "No"
End If
Loop
0
Option Explicit
On Error Resume Next
'declare variables
Dim DefragAnalyze
Dim ObjShell
Dim ObjFso
Dim ObjReadFile
Dim FileContents
Dim strLine
Dim i
Const TimeOut = 60000
Const ForReading = 1
i = 0
DefragAnalyze = "c:\dskanalyze.txt"
FileContents = "You should defragment this volume."
'analyze disk to see if defragmenting is needed
set ObjShell = WScript.CreateObject("Wscript.Shell")
ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -a >c:\dskanalyze.txt")
'pauses the script
WScript.Sleep TimeOut
'checks file and then runs defrag if necessary
Set ObjFso = WScript.CreateObject("Scripting.Filesystemobject")
Set ObjReadFile = ObjFspenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
i = i + 1
strLine = ObjReadFile.ReadLine
WScript.Echo "Line " & i & ": " & strLine
Loop
------------------------------------------------------
Option Explicit
On Error Resume Next
'declare variables
Dim DefragAnalyze
Dim ObjShell
Dim ObjFso
Dim ObjReadFile
Dim FileContents
Dim strLine
Dim i
Const TimeOut = 60000
Const ForReading = 1
i = 0
DefragAnalyze = "c:\dskanalyze.txt"
FileContents = "You should defragment this volume."
'analyze disk to see if defragmenting is needed
set ObjShell = WScript.CreateObject("Wscript.Shell")
ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -a >c:\dskanalyze.txt")
'pauses the script
WScript.Sleep TimeOut
'checks file and then runs defrag if necessary
Set ObjFso = WScript.CreateObject("Scripting.Filesystemobject")
Set ObjReadFile = ObjFspenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
strLine = ObjReadFile.ReadLine
If strLine = FileContents Then
WScript.Echo "Yes"
Else
WScript.Echo "No"
End If
Loop
0