Announcement

Collapse
No announcement yet.

How to redirect output of this script to a txt file ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to redirect output of this script to a txt file ?

    Hi,

    m very new to VBScript. i need urgent help from you experts
    i have this script which provide me info about available disk space.
    can any body tell me how do i redirect all the data to a .txt file ???
    The script is as follows
    Const HARD_DISK = 3strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")For Each objDisk in colDisks Wscript.Echo "DeviceID: "& vbTab & objDisk.DeviceID Wscript.Echo "Free Disk Space: "& vbTab & objDisk.FreeSpaceNext

  • #2
    Re: How to redirect output of this script to a txt file ?

    Hi

    Add the following lines at the start of your script:

    LogFileName = objShell.SpecialFolders("Desktop") & "\FreeSpace.txt"
    Set fsHandle = objFSO.OpenTextFile (LogFileName,8,True)

    and replace the WScript.Echo lines to:

    fsHandle.Writeline Now & " - " _
    & objDisk.SystemName & " - " _
    & objDisk.Name & " - " _
    & FormatNumber((objDisk.FreeSpace /1073741824),2) & " GB"

    this will then create a FreeSpace.txt file on your desktop.

    Comment


    • #3
      Re: How to redirect output of this script to a txt file ?

      Originally posted by twoip View Post
      Add the following lines at the start of your script:

      LogFileName = objShell.SpecialFolders("Desktop") & "\FreeSpace.txt"
      Set fsHandle = objFSO.OpenTextFile (LogFileName,8,True)
      Besites the objects objWMIService and objDisk, don't forget to create and define also the two objects objShell and objFSO.

      _
      The script ganesh_bendre showed is most likelywritten to be runned with the Cscript host.
      Run the script from the command line (with cscript) and redirect the output to a text file:
      Code:
      %comspec% /c ""%windir%\system32\cscript.exe" //NoLogo "pathtoscript\yourscript.vbs" >"c:\logfolder\Disk.FreeSpace.log""
      The vbscript (a lot better readable now),
      Code:
      Const HARD_DISK = 3
      
      strComputer = "."
      
      Set objWMIService = GetObject("winmgmts:" _
       & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
       
      Set colDisks = objWMIService.ExecQuery _
       ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
      
      For Each objDisk in colDisks
       Wscript.Echo ""
       Wscript.Echo "       DeviceID:", objDisk.DeviceID
       Wscript.Echo "Free Disk Space:", FormatNumber(objDisk.FreeSpace, 0), "bytes"
      Next
      Create a shortcut to this script (yourscript.vbs) edit the properties of the shortcut according the command line showed above.

      \Rems
      Last edited by Rems; 6th December 2011, 19:22.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X