Skip to content

Instantly share code, notes, and snippets.

@lucnap
Created February 14, 2017 10:14
Show Gist options
  • Save lucnap/c11e30cbcb0b2cdb9fbdbdecd484808c to your computer and use it in GitHub Desktop.
Save lucnap/c11e30cbcb0b2cdb9fbdbdecd484808c to your computer and use it in GitHub Desktop.
Some usefull vbscript functions
Dim lista
lista = ShowFolderList("C:\temp")
Wscript.Echo lista
' Wscript.Echo GetEnvironmentVariable("TEMP")
' Wscript.Echo RandomString(6)
'Dim CommandResults
'CommandResults = vFn_Sys_Run_CommandOutput("CMD.EXE /U /C DIR C:\ /AD", 1, 0, "", 0, 1)
'Wscript.Echo "risultato = " & CommandResults
Dim dt1, dt1f
dt1 = CDate("2015-5-3 9:25:14")
dt1f = MyFormatDate(dt1)
'Wscript.Echo dt1f
Function MyFormatDate(dt1)
MyFormatDate = DatePart("yyyy", dt1) & "_" & ZeroBeforeTen(DatePart("m", dt1)) & "_" & ZeroBeforeTen(DatePart("d", dt1)) & "_" & ZeroBeforeTen(DatePart("h", dt1)) & "_" & ZeroBeforeTen(DatePart("n", dt1)) & "_" & ZeroBeforeTen(DatePart("s", dt1))
End Function
Function ZeroBeforeTen(v1)
if CLng(v1) < 10 then
ZeroBeforeTen = "0" & v1
Else
ZeroBeforeTen = v1
end if
End Function
Function ShowFolderList(folderspec)
Dim fso, folder1, f1, fc, s, dt1
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.GetFolder(folderspec)
Set fc = folder1.Files
For Each f1 in fc
dt1 = CDate(f1.DateLastModified)
if dt1 > CDate("2015-01-01") Then
s = s & f1.name & vbTab & f1.DateLastModified & vbTab & f1.path & vbCrlf
End If
Next
ShowFolderList = s
End Function
Function RunCommandNoCapture(scmd)
Dim retval1
Set WinScriptHost = CreateObject("WScript.Shell")
retval1 = WinScriptHost.Run(scmd, 0, true)
Set WinScriptHost = Nothing
RunCommand = retval1
End Function
Function GetEnvironmentVariable(varname)
Set wshShell = CreateObject("WScript.Shell")
GetEnvironmentVariable = wshShell.ExpandEnvironmentStrings("%" & varname & "%")
Set wshShell = Nothing
End Function
Function RandomString(cb)
Randomize
Dim rgch, genstring
rgch = "abcdefghijklmnopqrstuvwxyz"
rgch = rgch & UCase(rgch) & "0123456789"
Dim i
For i = 1 To cb
genstring = genstring & Mid(rgch, Int(Rnd() * Len(rgch) + 1), 1)
Next
RandomString = genstring
End Function
Function vFn_Sys_Run_CommandOutput (Command, Wait, Show, OutToFile, DeleteOutput, NoQuotes)
'Example: CommandResults = vFn_Sys_Run_CommandOutput("CMD.EXE /C DIR C:\ /AD",1,1,"C:\OUTPUT.TXT",0,1)
'Run Command similar to the command prompt, for Wait use 1 or 0. Output returned and
'stored in a file.
'Command = The command line instruction you wish to run.
'Wait = 1/0; 1 will wait for the command to finish before continuing.
'Show = 1/0; 1 will show for the command window.
'OutToFile = The file you wish to have the output recorded to.
'DeleteOutput = 1/0; 1 deletes the output file. Output is still returned to variable.
'NoQuotes = 1/0; 1 will skip wrapping the command with quotes, some commands wont work
' if you wrap them in quotes.
'----------------------------------------------------------------------------------------
Set f_objShell = CreateObject("Wscript.Shell")
Set f_objFso = CreateObject("Scripting.FileSystemObject")
'mio miglioramento
If OutToFile = "" then
OutToFile = f_objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & RandomString(6) & ".txt"
End If
On Error Resume Next
'On Error Goto 0
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'VARIABLES
If OutToFile = "" Then OutToFile = "TEMP.TXT"
tCommand = Command
If Left(Command,1)<>"""" And NoQuotes <> 1 Then tCommand = """" & Command & """"
tOutToFile = OutToFile
If Left(OutToFile,1)<>"""" Then tOutToFile = """" & OutToFile & """"
If Wait = 1 Then tWait = True
If Wait <> 1 Then tWait = False
If Show = 1 Then tShow = 1
If Show <> 1 Then tShow = 0
'RUN PROGRAM
f_objShell.Run tCommand & ">" & tOutToFile, tShow, tWait
'READ OUTPUT FOR RETURN
'last -1 is for reading unicode
Set f_objFile = f_objFso.OpenTextFile(OutToFile, ForReading, false, -1)
tMyOutput = f_objFile.ReadAll
f_objFile.Close
Set f_objFile = Nothing
'DELETE FILE AND FINISH FUNCTION
If DeleteOutput = 1 Then
Set f_objFile = f_objFso.GetFile(OutToFile)
f_objFile.Delete
Set f_objFile = Nothing
End If
vFn_Sys_Run_CommandOutput = tMyOutput
If Err.Number <> 0 Then vFn_Sys_Run_CommandOutput = "<0>"
Err.Clear
On Error Goto 0
Set f_objFile = Nothing
Set f_objShell = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment