?
Solved

Logging within a VBScript

Posted on 2006-05-23
6
Medium Priority
?
3,994 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:SimonUK
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
JesterToo earned 2000 total points
ID: 16748090
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
 
LVL 1

Author Comment

by:SimonUK
ID: 16749327
Brilliant, as before!

Thanks again


Simon
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 16757490
You're welcome and thanks for the grade!

Regards,
Lynn
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:SimonUK
ID: 16761883
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
 
LVL 22

Expert Comment

by:JesterToo
ID: 16765041
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
 
LVL 1

Author Comment

by:SimonUK
ID: 16767486
Hi Lynn

Brilliant, all working perfectly now.  Many thanks !


Simon
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question