Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS Script to read a file

Status
Not open for further replies.

tobjob

IS-IT--Management
Sep 17, 2009
10
US
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 = ObjFso_OpenTextFile(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 = ObjFso_OpenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
strLine = ObjReadFile.ReadLine
If strLine = FileContents Then
WScript.Echo "Yes"
Else
WScript.Echo "No"
End If
Loop
0
 
The problem is that your "You should defragment this volume." message that appears in your c:\dskanalyze.txt file will actually have about 4 or 5 spaces tagged onto its end. So it will really be something like "You should defragment this volume. " Therefore your string comparison test is always failing.


In order to understand recursion, you must first understand recursion.
 
You could also use the InStr() function instead of
If strLine = FileContents Then
 
use inStr() to see if FileContents is found anywhere in strLine or use trim() to clear whitespace.

instr(strHaystack, strNeedle)
Code:
if (inStr(strLine, FileContents)) then
   wscript.echo "yes"
else
   wscript.echo "no"
end if

trim(string)
Code:
strLine = "You should defrag this volume      "
wscript.echo trim(strLine)

//output (without the quotes)
"You should defrag this volume"

-Geates
 
Thanks for the help. I am still having a bit of trouble with it. I used the inStr function and added the additional spaces at the end of the file. If I kept the Wscript.Echo in to pop up a message to say yes or no, it stayed in a constant loop saying "no" until I killed the wscript.exe. So I can tell it keeps reading and reading the file, but that it also doesn't see the "You should defrag this volume. ", even when it is in the file. So I wasn't sure how to stop that. Ultimately, I would like it to run a forced defrag if the FileContents variable is found in the file. I have included that in the updated version. I appreciate all the help.

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 = ObjFso_OpenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
If (inStr(strLine, FileContents)) Then
ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -f")
Else

End If
Loop
 
two things, it's constantly looping because your code does not iterate the file. strLine is never set to anything so the first condition is always false.

you need to add
Code:
strLine = objReadFile.ReadLine

secondly, get rid of the spaces! They make the If..Then condition likely to be false.

Code:
FileContents = "You should defragment this volume."
.
.
.
Set ObjFso = WScript.CreateObject("Scripting.Filesystemobject")
Set ObjReadFile = ObjFso.OpenTextFile(DefragAnalyze, ForReading)
Do Until ObjReadFile.AtEndOfStream
   [b]strLine = objReadFile.ReadLine[/b]
   If (inStr(strLine, FileContents)) Then
      ObjShell.Run("%comspec% /c %systemroot%\system32\defrag.exe c: -f")
   End If
Loop
-Geates
 
If you felt so inclined you could collapse all that down into:

If InStr(WScript.CreateObject("Scripting.Filesystemobject").OpenTextFile(DefragAnalyze, ForReading).Readall, FileContents) Then
ObjShell.Run ("%comspec% /c %systemroot%\system32\defrag.exe c: -f")
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top