Thursday, July 10, 2008

VBScript to push files into an array based on a wildcard


Option Explicit
Dim gFSO
Dim dirListing
Dim fileName

set gFSO = CreateObject("Scripting.FileSystemObject")

dirListing = ListDir("c:\??*d*")

If UBound(dirListing) = -1 then
Wscript.Echo "No files found."
Else
For Each fileName in dirListing
WScript.Echo FileName
Next
End If

'==============================================================================
' List a directory, with the last part of the directory being the path
'==============================================================================
Function ListDir (ByVal Path)
Dim fileRegex
Dim searchFolderName
Dim searchFolder
Dim searchFolderFiles
Dim fileName
Dim fileArray
Dim file

set fileRegex = New RegExp
If gFSO.FolderExists(Path) Then ' Path is a directory, list all files in path
searchFolder = Path
fileName = ""
Else
searchFolderName = gFSO.GetParentFolderName(Path)
fileName = gFSO.GetFileName(Path)
fileRegex.Global = True
fileRegex.Pattern = "\."
fileName = fileRegex.Replace(fileName, "\.")
fileRegex.Pattern = "\?"
fileName = fileRegex.Replace(fileName, ".")
fileRegex.Pattern = "\*"
fileName = fileRegex.Replace(fileName, ".*")
End If
With fileRegex
.Pattern = fileName
.IgnoreCase = True
.Global = False
End With

ReDim fileArray(1)
Dim fileCount : fileCount = 0

Set searchFolder = gFSO.GetFolder(searchFolderName)
Set searchFolderFiles = searchFolder.Files

For Each file in searchFolderFiles
If fileRegex.Test(file.Name) Then
If fileCount > UBound(fileArray) Then
ReDim Preserve fileArray(fileCount*2)
End If
fileArray(fileCount) = file.Path
fileCount = fileCount + 1
End If
Next
ReDim Preserve fileArray(fileCount - 1)
ListDir = fileArray
End Function

Labels:

Tuesday, June 17, 2008

VBScript to convert timestamp to YYYYMMDD formatted string

I started with the code on this page.

Function FormatYYYYMMDD(timeStamp)

Dim dateMonth : dateMonth = DatePart("M", timeStamp)
Dim dateDay : dateDay = DatePart("D", timeStamp)
Dim dateYear : dateYear = DatePart("YYYY", timeStamp)
Dim dateString

dateString = dateYear

If dateMonth < 10 Then
dateString = dateString & "0" & dateMonth
Else
dateString = dateString & dateMonth
End If

If dateDay < 10 Then
dateString = dateString & "0" & dateDay
Else
dateString = dateString & dateDay
End If

FormatYYYYMMDD = dateString
End Function

Labels:

Thursday, April 10, 2008

Time since last boot (Windows, VBScript)

I don't remember where I found this, I was just happy to find code that did this.

 strComputer = "." ' Local computer 
set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
objWMIDateTime.Value = objOS.LastBootUpTime
Wscript.Echo "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
"Time Since Last Boot: " & TimeSpan(objWMIDateTime.GetVarDate,Now) & _
" (hh:mm:ss)"
next

Function TimeSpan(dt1, dt2)
' Function to display the difference between
' 2 dates in hh:mm:ss format
If (isDate(dt1) And IsDate(dt2)) = false Then
TimeSpan = "00:00:00"
Exit Function
End If

seconds = Abs(DateDiff("S", dt1, dt2))
minutes = seconds \ 60
hours = minutes \ 60
minutes = minutes mod 60
seconds = seconds mod 60

if len(hours) = 1 then hours = "0" & hours

TimeSpan = hours & ":" & _
RIGHT("00" & minutes, 2) & ":" & _
RIGHT("00" & seconds, 2)
End Function

Labels: