Welcome to DS Homebrew!

Hope you enjoy your visit............

Access Power Management and Scheduler VBScript

Posed on 2009-08-11 00:14:00

Author: Gareth Bennett

I like listening to music/pod casts when I am going to sleep. However, I don't like leaving my computer on all night.

I needed a script to do the following:-

  • Switch my Monitor off
  • Shutdown the Computer after (x) amount of time

Here is the Complete Code, all you need to change is (Hour) and (Min):-

Option Explicit

Dim objShell, Hour, Min, strPowerManagement, strShutdown

Hour = 0
Min  = 0

strPowerManagement = CStr(Hour) & ":" & CStr(Min)
strShutDown = CStr(Hour) & ":" & CStr(Min + 1)

Set ObjShell = CreateObject("WScript.Shell")
objShell.run "powercfg.exe /X 1 /N /monitor-timeout-ac 1"

Rem Sheduled Tasks
objShell.run "AT " & strPowerManagement & " powercfg.exe /X 1 /N /monitor-timeout-ac 0"
objShell.run "AT " & strPowerManagement & " powercfg.exe /X 1 /N /disk-timeout-ac 0"
objShell.run "AT " & strPOwerManagement & " powercfg.exe /X 1 /N /standby-timeout-ac 0"
objShell.run "AT " & strShutdown & " shutdown.exe -s"

Set ObjShell = nothing

Here is what the full script does in brief:-

  • Chanages the Mointor Setting in Power-Management to Switches off the monitor in 1 minute.
  • Schedules the commands to set Power-Management back to setting I like.
  • Schedules the System to Shutdown, 1 minutes after the Power-Management schedule have been initiated.

Comments on Access Power Management and Scheduler VBScript (0)

Get MP3 Duration

Posed on 2009-07-27 00:09:38

Author: Gareth Bennett

I am very glad to get this script working as I have been working on it all night. The script will read a directory full of MP3 files; and then output the filename and duration of each MP3 file into a CSV file.

The only thing you have to change in the script is the Path and File names. You obviously don't want your output file in the same folder as your MP3 files; as it will not be able to read the duration property of the CVS file.

Option Explicit

Const OPENFORREADING = 1
Const OPENFORWRITING = 2
Const OPENFORAPPENDING = 8

Const DURATION = 21

Public Sub CreateMp3TrackList()
	Dim objFSO, objFile, objTxtStream, objFsoFolder, objFiles
	Dim objShell, objShellFolder, objFolderItem
	Dim strFolder, strOutputFile, strTime, strName, strFileInfo, strGroup
		
	strFolder = "D:\MP3Files"
	strOutputFile = "D:\mp3list.csv"
		
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("Shell.Application")
	
	Set objShellFolder = objShell.Namespace(strFolder)
	Set objFsoFolder = objFSO.GetFolder(strFolder)

	Set objFiles  = objFsoFolder.Files
	Set objFsoFolder = nothing
	
	Set objTxtStream = objFSO.CreateTextFile(strOutputFile, True)
	
	For Each objFile in objFiles
		Set objFolderItem = objShellFolder.ParseName(objFile.Name)
  		strFileInfo = objShellFolder.GetDetailsOf(objFolderItem, DURATION)
		strName = objFile.Name
		strGroup = strName & "," & strFileInfo
		objTxtStream.WriteLine strGroup
		Set objFolderItem = nothing
	Next
		
	objTxtStream.Close
	
	Set objFile = Nothing
	Set objFiles = Nothing
	Set objTxtStream = Nothing
	Set objShellFolder = Nothing
	Set objShell = Nothing
	Set objFSO = Nothing
	
	MsgBox "Completed"
End Sub

CreateMp3TrackList()

Comments on Get MP3 Duration (0)

Convert CSV to XLS

Posed on 2009-07-25 19:41:15

Author: Gareth Bennett

I have recently been working on converting a CSV file to an XLS spreadsheet using a script rather than opening the CSV file in Excel, then re-saving as an XSL file.

To solve this task, I used the Microsoft Excel COM Object. Below are the properties of the method (Open) used to open a workbook:-

expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)

I am interested in the properties FileName, Format and Delimiter.

[Format] takes on 6 constant values:-

  1. Tabs
  2. Commas
  3. Spaces
  4. Semicolons
  5. Nothing
  6. Custom character

Here is the line of code to import a CSV file:-

objXL.WorkBooks.Open(FileName & ".csv",,,2)

I do not like using Comma's as delimiters as they tend to crop up in many forms of data. Instead by setting the format to Custom Character (6) and using a PIPE (|) as the delimiter there is less chance that columns of data won't fall out of sequence.

Here is the line of code to import a delimited file using the PIPE (|) as the delimiter:-

objXL.WorkBooks.Open(FileName & ".txt",,,6,,,,,"|")

Here is the complete VBScript to convert a CSV file to an XLS spreadsheet.


Option Explicit

Const FileName = "YourPathYourFileName"

'***********************************************************

DIM objXL, objWB

Set objXL = WScript.CreateObject ("Excel.Application")
Set objWB = objXL.WorkBooks.Open(FileName & ".csv",,,2)

objXL.DisplayAlerts = False

objWB.SaveAs FileName & ".xls"

objXL.Quit()

Set objWB = Nothing
Set objXL = Nothing

Comments on Convert CSV to XLS (0)