Need help with 'Complicated' VBScript

rstruble's Avatar, Join Date: Oct 2009
Go4Expert Member
Hi,
I need help creating a script that will go through a computer, look at all the user profiles in C:\Documents and Settings, then for each profile it finds, it lists the size of the My Documents folder in MB, preferably without the My Music and My Pictures folders. i need it to write the computer name, the profile, and the size of the folder into a CSV file on my computer.

is this possible with VBScript?

-R.Struble
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Yes, it is possible in VBScript

But, how far have you reached ?
We won't write the whole code for you. We can "help" you make your code better.

Hint : Use VBScript's Folder object.
rstruble's Avatar, Join Date: Oct 2009
Go4Expert Member
to tell da truth, i dont know much about VB script at all. my boss is trying to help me, but we cant figure out how to make our script able to run through Altiris, right now i have to type in the computer name in a input box.

If there is a special way to post the code we have, please let me know so i can show you what we have so far

-RS
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Yeah, post your code here.

Remember to enclose it within
[code=VB]
.
.
<your code here>
.
.
[/code]
rstruble's Avatar, Join Date: Oct 2009
Go4Expert Member
Code: VB
Dim strComputer,strFileName,strCSV,TotalSize

strFileName = GetPathOfVBS & "\MyDocsReport.csv"
strComputer = InputBox ("Enter the computer name to scan","My Documents Scan", strComputer)

If strComputer = "" Then
  WScript.Echo "Script cancelled"
  WScript.Quit
Else
  GetFolderSize
  OutputToFile
End If

'get file paths to examine, skipping system profiles and My Music & My Pictures folders
Sub GetFolderSize
  On Error Resume Next
  Dim fso, StrDocandSettings, aryUser,aryFolders, strUserName, strFolderName
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set StrDocandSettings = fso.getFolder("\\" & strComputer & "\c$\documents and settings")
  Set aryUser = StrDocandSettings.SubFolders
  'Skip these profiles:
  For Each strUserName in aryUser
    If strUserName.name = "All Users" Then
    ElseIf strUserName.name = "Default User" Then
    ElseIf strUserName.name = "LocalService" Then
    ElseIf strUserName.name = "NetworkService" Then
    ElseIf strUserName.name = "Administrator" Then
    Else
      TotalSize = 0
      Set StrDocandSettings = fso.getFolder("\\" & strComputer &_
         "\c$\Documents and Settings\" & strUserName.name & "\My Documents")
      Set aryFolders = StrDocandSettings.SubFolders
      For Each strFolder in aryFolders
        'skip these folders:
        If instr(strFolder,"My Music") = 0 And instr(strFolder,"My Pictures") = 0 Then
          GetSize(strFolder)
          SpaceMB = Round(TotalSize/1048576,1) & " MB" 'format bytes into MB
        End If
      Next
      strCSV = strCSV & strComputer & "," & strUserName.name & "," & SpaceMB & VbCrLf
    End If
  Next
  WScript.Echo "Script complete. View " & strFileName & " to see results."
End Sub

'Gets folder size in bytes. accumulates total size.
Sub GetSize(FolderPath) 
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFSO.GetFolder(FolderPath)
  TotalSize = TotalSize + objFolder.Size
End Sub

'returns file path to where this script is located
Function GetPathOfVBS
  Set objShell = CreateObject("Wscript.Shell")
  strPath = Wscript.ScriptFullName
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile(strPath)
  strFolder = objFSO.GetParentFolderName(objFile)
  GetPathOfVBS = strFolder
End Function

'appends output to results file
Sub OutputToFile
  Const ForWriting = 2 
  Const ForAppending = 8 
  strDelimter = ","  'separates each data element for CSV file   
  Set FileSys = createobject("Scripting.FileSystemObject")
  If FileSys.FileExists(strFileName) Then 'just put results in existing file
    Set objFile = FileSys.OpenTextFile(strFileName,ForAppending)   
    objFile.Write strCSV
    objFile.Close   
  Else  'if no results file, create new CSV and describe header
    Set objFile = FileSys.CreateTextFile(strFileName,ForWriting)   
    strText = "Computer Name" & strDelimter & "Profile Name" & strDelimter & "Size of My Documents" & VbCrLf
    strText = strText & strCSV
    objFile.Write strText
    objFile.Close
  End If
End Sub

Like that?

-RS

Last edited by SaswatPadhi; 9Oct2009 at 20:48.. Reason: Unnecessary periods
jazscript's Avatar, Join Date: Oct 2009
Newbie Member
I'm trying to get the script to save to \\remote_server\D$\Logs\%computername%" instead of \\remote_server\D$\Logs\".

I'm new to VB and I've been trying for about 2 weeks. I'm doing something wrong. Can you stear me in the right direction?

================================================== =========
Code:
Dim DestServer, strComputer
' Put in the UNC path for where you want the logs to be stored

DestServer = "\\remote_server\D$\Logs\"


'Create the Time variables
sDate=Right("0" & Month(Date),2) _
& "-" & Right("0" & Day(Date),2) _
& "-" & Right(Year(Date),2)

sTime = DatePart("h", Now) & DatePart("n", Now)

set oFSO = CreateObject("Scripting.FileSystemObject")


'If correct folder doesn't exist, make it
if Not oFSO.FolderExists(DestServer & sDate) then
   set oFolder = oFSO.CreateFolder(DestServer & sDate )
end if

'Gets the log files for this machine
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
        & strComputer & "\root\cimv2")

Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")


'This section goes out and gets the hostname this is run on for us.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
  strHOSTNAME = objItem.Name
NEXT


'Now archive the logs and clear them
if oFSO.FolderExists(DestServer & sDate) then
  For Each objLogfile in colLogFiles
    strBackupLog = objLogFile.BackupEventLog _
        (DestServer & strComputer & sDate & "\" & strHOSTNAME & "_" & objLogFile.LogFileName & "_" & sDate & "_" & sTime & ".evt")
    objLogFile.ClearEventLog()
  Next
end if

Last edited by shabbir; 22Oct2009 at 09:31.. Reason: Code blocks
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
You need to moify the below line, and add the computername to it
Code: vb
DestServer = "\\remote_server\D$\Logs\"

You can fetch it from environment variables, using the below code
Code: vb
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

and then you can modify the code to
Code: vb
DestServer = "\\remote_server\D$\Logs\" & strComputerName

Few more ways to get computername: here

Last edited by nimesh; 22Oct2009 at 21:28..