TIPS: VBScript : Get Logged in username using VBScript.


  • Using Environment Variable

Set wshShell = WScript.CreateObject( “WScript.Shell” )
strUserName = wshShell.ExpandEnvironmentStrings( “%USERNAME%” )
WScript.Echo “User Name: ” & strUserName

  • Using WshNetwork

Set wshNetwork = WScript.CreateObject( “WScript.Network” )
strUserName = wshNetwork.UserName
WScript.Echo “User Name: ” & strUserName

  • Using ADSI (ADSystemInfo)

Set objSysInfo = CreateObject( “ADSystemInfo” )
strUserName = objSysInfo.UserName
WScript.Echo “User Name: ” & strUserName

  • ADSI (WinNTSystemInfo)

Set objSysInfo = CreateObject( “WinNTSystemInfo” )
strUserName = objSysInfo.UserName
WScript.Echo “User Name: ” & strUserName

  • WMI (Win32_ComputerSystem)

Set objWMI = GetObject( “winmgmts:\\.\root\cimv2” )
Set colItems = objWMI.ExecQuery( “Select * from Win32_ComputerSystem” )
For Each objItem in colItems
strUserName = objItem.UserName
Next

 

recursive list files in a dir using Python


use the following code to list the files in a dir tree

import os
import sys
fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
    for file in files:
        fileList.append(os.path.join(root,file))
print fileList

#================================================================================
# List of all the files, total count of files and folders & Total size of files.
#================================================================================
import os
import sys

fileList = []
fileSize = 0
folderCount = 0
rootdir = sys.argv[1]

for root, subFolders, files in os.walk(rootdir):
    folderCount += len(subFolders)
    for file in files:
        f = os.path.join(root,file)
        fileSize = fileSize + os.path.getsize(f)
        #print(f)
        fileList.append(f)

print("Total Size is {0} bytes".format(fileSize))
print(“Total Files “, len(fileList))
print(“Total Folders “, folderCount)