Link to home
Avatar of thesurg3on
thesurg3on

asked on

.vb Script to Dump Defrag -a -v results to a Text File

I want a .vb script that will run the windows command

defrag [drive letter] -a -v

and dump the results to a text file.

the defrag switch needs a drive letter. i want the drive letters in an array. psuedo code

If drive letter exists, run degrag DRIVELETTER: -a -v redirect to text file, go to next drive letter, run defrag DRIVELETTER: -a -v append to text file....

Do you know how to code this? It should be very easy to do, i just dont know how to write it.
Avatar of rettiseert
rettiseert

You can dump the output of any comand to a file like this:

thecommand options > outputfile.txt

for example:

dir /w > out.txt
Avatar of thesurg3on

ASKER

yes, i know that. however, i have many servers will many drives on them. the reason i want to script it is so that i double click a file, get my output automatically placed into a text file.

Thanks, but i need automation.
Avatar of sirbounty
Should still work to redirect though...try this, name it dfrg.vbs
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
If WScript.Arguments.Count = 0 Then
  wscript.echo "Usage: dfrg.vbs x:" & vbnewline & "where x: is the valid drive letter to defrag"
  wscript.quit
Else
 strDrive=WScript.Arguments.Item(0)
End If
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.DriveExists(strDrive) Then 
  wscript.echo "That drive does not exist!"
  wscript.quit
End If
 
If len(strDrive) < 2 Then strDrive = strDrive & ":"
objShell.Run "defrag " & strDrive & " -a -v >> c:\DefragReport.txt"

Open in new window

Otherwise, you can use the exec method...


Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
If WScript.Arguments.Count = 0 Then
  wscript.echo "Usage: dfrg.vbs x:" & vbnewline & "where x: is the valid drive letter to defrag"
  wscript.quit
Else
 strDrive=WScript.Arguments.Item(0)
End If
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.DriveExists(strDrive) Then 
  wscript.echo "That drive does not exist!"
  wscript.quit
End If
 
If len(strDrive) < 2 Then strDrive = strDrive & ":"
 
strcmd = "%comspec% /C defrag " & strDrive & " -a -v"
set objExec=objShell.Exec(strcmd)
 
Dim objOutput : Set objOutput = objFSO.CreateTextFile("C:\DefragReport.txt")
strData = objExec.StdOut.ReadAll
objOutput.WriteLine strData

Open in new window

did you try to run this on your computer. i ran it, and i didnt get a report or anything. I just got the prompt that says:

where x: is the valid drive letter to defrag
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
hi,

i really appreciate your help, but creating dfrg.vbs in notepad from your last example and then running on my worstation, does not create the Defrag.txt file.
I don't believe it works.

I am using your exact code:

Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
If WScript.Arguments.Count = 0 Then
  wscript.echo "Usage: dfrg.vbs x:" & vbnewline & "where x: is the valid drive letter to defrag"
  wscript.quit
Else
 strDrive=WScript.Arguments.Item(0)
End If
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.DriveExists(strDrive) Then
  wscript.echo "That drive does not exist!"
  wscript.quit
End If
 
If len(strDrive) < 2 Then strDrive = strDrive & ":"
 
strcmd = "%comspec% /C defrag " & strDrive & " -a -v"
set objExec=objShell.Exec(strcmd)
 
Dim objOutput : Set objOutput = objFSO.CreateTextFile("C:\DefragReport.txt")
strData = objExec.StdOut.ReadAll
objOutput.WriteLine strData
objOutput.Close
Does your account have rights to write to the root of the C drive?
When I first ran this, it bombed on me due to permissions, but after pointing the output report to my profile's desktop folder, it worked...
i have rights to my local machine, which i am running it on. i am an administrator.
Works fine here. Make sure you're passing a drive-letter argument. Example:

dfrg.vbs c:\

Thanks. I actually ended up using another solution which works very very well.

https://www.experts-exchange.com/questions/23607527/Script-to-export-results-of-a-Defrag-a-v-Command-to-a-file.html