Logging within a VBScript

Hi all

In another question here:

http://www.experts-exchange.com/Programming/Programming_Languages/Q_21858361.html

JesterToo wrote me a script to carry out a specific mirror / convert routine.  What I'd like to do now is add log creation to it.  Ideally, the log should be in the format below (see original thread for this to make sense!)

Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Already converted, skipped
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Already converted, skipped
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Already converted, skipped
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Already converted, skipped
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Already converted, skipped
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Done!
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Done!
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Done!
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Done!
Date : Time -  \dd-mm-yy\hh-mm-ss.mp2  -  Done!

The log should be replaced each time the script is run (the batch file that calls the vbs will e-mail the log out, then delete or archive it somewhere else so technically, each time the script runs there shouldn't be an existing log - but if there is, just replace it).

Thanks...


Simon
LVL 1
SimonUKAsked:
Who is Participating?
 
JesterTooConnect With a Mentor Commented:
Here is the script with the logging feature added.

Regards,
Lynn

'*****************************************************************************************
' convert.vbs
'
'  This script backups all MP2 files found within a source folder tree to a target folder
'  tree of identical structure.  MP2 files are converted to MP3 files in the target folders
'  and only those MP2 files which have not yet been converted are operated on.  New target
'  folders are created as necessary and old ones removed to enforce that the target folder
'  structures stay synchronized.
'
'  Lynn Ransdell  05/21/2006
'
'  NOTE:  This script makes extensive use of the "Wscript.Echo" statement.  Therefore,
'         in order to avoid an interactive dialogue that you must acknowledge for each
'         message, be sure to run this script like this:
'
'             cscript convert.vbs
'
'  NOTE:  logging feature added on 05/23/2006
'*****************************************************************************************

Option Explicit

Dim oFSO, oShell
Dim SourceRoot, TargetRoot
Dim buFileCount, ofFileCount
Dim aSrcFolders, aTgtFolders
Dim Folder
Dim StopNow, n
Dim LogFileName, oLog

'' SourceRoot  = "E:\ee\source dir"       '<=== my test folder
'' TargetRoot  = "E:\ee\target dir"       '<=== my test folder
'' LogFileName = "E:\ee\Convert.log"      '<=== my log file

   SourceRoot  = "\\servername\audio"     '<=== change as necessary
   TargetRoot  = "\\servername\audio2"    '<=== change as necessary
   LogFileName = "\\servername\share\Convert.log"      '<=== change as necessary
   buFileCount = 0
   ofFileCount = 0

   Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oShell = CreateObject("Wscript.Shell")

   '-- determine if directory root structures exist...

   If Not oFSO.FolderExists(SourceRoot) Then
      Wscript.Echo "Path to root of SOURCE is invalid."
      StopNow = True
   End If

   If Not oFSO.FolderExists(TargetRoot) Then
      Wscript.Echo "Path to root of TARGET is invalid."
      StopNow = True
   End If

   If StopNow Then
      Wscript.Echo "Terminating due to error(s)."
      Set oFSO = Nothing
      Set oShell = Nothing
      Wscript.Quit(16)
   End If

   '-- ensure that all folders exist under TARGET
   aSrcFolders = GetFolders(SourceRoot)
   For n = 0 To UBound(aSrcFolders)
      Folder = TargetRoot & aSrcFolders(n)
      If Not oFSO.FolderExists(Folder) Then
         oFSO.CreateFolder Folder
      End If
   Next

   '-- remove any TARGET folders that no longer exist under SOURCE
   aTgtFolders = GetFolders(TargetRoot)
   For n = 0 To UBound(aTgtFolders)
      Folder = SourceRoot & aTgtFolders(n)
      If Not oFSO.FolderExists(Folder) Then
         oShell.Run("cmd /c rd /s /q " & Chr(34) & TargetRoot & aTgtFolders(n) & Chr(34)),0
      End If
   Next

   Set oLog = oFSO.CreateTextFile(LogFileName)

   '-- Let's get to work...
   For n = 0 To UBound(aSrcFolders)
      ScanFiles SourceRoot & aSrcFolders(n), TargetRoot & aSrcFolders(n)
   Next

   oLog.Close

   Wscript.Echo "Finished!  " & buFileCount & " files backed up."

   Set oFSO = Nothing
   Set oShell = Nothing

Sub ScanFiles(SrcPath, DestPath)
Dim oFolder, colFiles
Dim FileName, oFile
Dim SrcFile, oSrcFile
Dim DestFile, oDestFile
Dim n

   Set oFolder = oFSO.GetFolder(SrcPath)
   Set colFiles = oFolder.Files
   For Each oFile In colFiles
      FileName = oFile.Name
      If LCase(Right(FileName, 4)) = ".mp2" Then
         SrcFile = SrcPath & "\" & FileName
         Set oSrcFile = oFSO.GetFile(SrcFile)
         SrcFile = Chr(34) & SrcFile & Chr(34)
         DestFile = DestPath & "\" & FileName
         DestFile = Left(DestFile,Len(DestFile)-1) & "3"

         oLog.Write Date & " : " & Time & " - " & Mid(oFile.Path,Len(SourceRoot)+1) & " - "

         If oFSO.FileExists(DestFile) Then
            oLog.WriteLine "Already converted, skipped"
         Else
            DestFile = Chr(34) & DestFile & Chr(34)
            RunLame SrcFile, DestFile
            oLog.WriteLine "Done!"
         End If
      End If
   Next

   Set oSrcFile = Nothing
   Set oDestFile = Nothing
   Set colFiles = Nothing
   Set oFolder = Nothing

End Sub

Function GetFolders(BaseFolder)
Dim oBaseFolder
Dim SubFolder
Dim strArr

   Set oBaseFolder = oFSO.GetFolder(BaseFolder)
   For Each SubFolder In oBaseFolder.SubFolders
      If Not IsEmpty(SubFolder) Then
         strArr = strArr & "+" & Mid(SubFolder, Len(BaseFolder) + 1)
      End If
   Next

   GetFolders = Split(Mid(strArr, 2), "+")
   Set oBaseFolder = Nothing

End Function

Sub RunLame(SrcFile, DestFile)
Dim sCommand
Dim nHide

   nHide = 0   ''' 0 = hide the command shell window, 1 = don't hide

   sCommand = "cmd /c copy                   " & SrcFile & " " & DestFile
   sCommand = "lame --mp2input -m m -a -b 16 " & SrcFile & " " & DestFile
'' oShell.Run(sCommand),nHide,True
   buFileCount = buFileCount + 1

   ' if not showing the command shell, then tell the user what is happening
   If nHide = 0 Then
      Wscript.Echo "Converting file # " & buFileCount & " = " & DestFile
   End If

End Sub
0
 
SimonUKAuthor Commented:
Brilliant, as before!

Thanks again


Simon
0
 
JesterTooCommented:
You're welcome and thanks for the grade!

Regards,
Lynn
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
SimonUKAuthor Commented:
Pleasure !

There's a little problem.  The log works fine, it lists all skipped files and all converted files - but the script no longer actually performs the conversion on new files!  The new destination folders are created as expected - and the files to be converted appear in the process list and the log- but no mp3's appear...


Simon
0
 
JesterTooCommented:
I'm sorry, Simon... I introduced a careless mistake that you can easily fix.  About 9 lines up from the bottom of the script there is an "oShell.Run" statement.  I accidentally commented that line instead of the one two lines earlier that starts with "sCommand = cmd /c copy ...".  I had meant to comment out the lines I used for testing but flagged the wrong one here.

If you remove the apostrophes at the fron of that statement it will actually run the LAME program.  Let me know if you have any more problems with it.  The other line can be left asis if you like since very next line overwrites it anyway.

Lynn
0
 
SimonUKAuthor Commented:
Hi Lynn

Brilliant, all working perfectly now.  Many thanks !


Simon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.