Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

VB Script - Write to Log

Hello
I need help in writing all transactions of my vb script to a log file (success and error)

Can someone help?

Here is my Vb Script:
-----------------------------------------------------------------------------------------------------------------------------------------------

'Delete Log files older than 1 day from vFiler - DB Backups
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile, objShell
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
Const strMatch = "Log"


'Delete connection if exists
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
objNetwork.RemoveNetworkDrive "L:"

'Set UNC connection to vFiler
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "L:", "\\10.10.10.1\data\ProdBackups\Database", False, "username", "pwd"


strFolderToSearch = "\\10.10.10.1\data\ProdBackups\Database"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
'msgbox objRootFolder

SearchFolders objRootFolder

Wscript.Quit

Sub SearchFolders(objBaseFolder)
   For Each objFolder in objBaseFolder.SubFolders
      'msgbox objFolder.Path
      If InStr(objFolder.Name, strMatch) Then
         
         If DateDiff("d", objFolder.DateCreated, Now) >= 1 Then
            'msgbox objFolder.Path
            objFolder.Delete
            
         Else
            SearchFolders objFolder
         End If
      Else
         SearchFolders objFolder
      End If
   Next
End Sub

On Error Resume Next
objNetwork.RemoveNetworkDrive "L:"
0
sunexchange
Asked:
sunexchange
1 Solution
 
ltlbearand3Commented:
You can use the text file object that is part of the file system object you are already using.  I have added in some pieces to your code to show how it would work and modified you code a little bit.  Make sure to set strFilePath to where you want the log to write.  Try this (not tested):
'Delete Log files older than 1 day from vFiler - DB Backups
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile, objShell
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
Const strMatch = "Log"
Dim objTextFile, strFileLocation

' *** Set your log file path and name here ****
strFileLocation = "C:\myscriptlog.log"

' Instantiate Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")

' Start the Log File
Set objTextFile = objFSO.CreateTextFile("c:\file.txt", True)	' This will overwrite the exiting log file

'Delete connection if exists
On Error Resume Next ' Skip if Errors
objNetwork.RemoveNetworkDrive "L:"
On Error Goto 0 	' Turn Error handling back on
objtTextFile.writeline("Attempted to remove Drive L:"

'Set UNC connection to vFiler
objNetwork.MapNetworkDrive "L:", "\\10.10.10.1\data\ProdBackups\Database", False, "username", "pwd"
objTextFile.writeline("Mapped Drive L: to \\10.10.10.1\data\ProdBackups\Database")


strFolderToSearch = "\\10.10.10.1\data\ProdBackups\Database"
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
'msgbox objRootFolder

SearchFolders objRootFolder

objTextFile.Close

Set objTextFile = nothing
Set objFSO = nothing
Set objShell = Nothing
Set objNetwork = Nothing

Wscript.Quit

Sub SearchFolders(objBaseFolder)
	objTextFile.writeline("Searching Folder " & objBaseFolder.Path)

			For Each objFolder in objBaseFolder.SubFolders
      'msgbox objFolder.Path
      If InStr(objFolder.Name, strMatch) Then
         
         If DateDiff("d", objFolder.DateCreated, Now) >= 1 Then
            'msgbox objFolder.Path
            objFolder.Delete
			objTextFile.writeline("Deleted " & objFolder.Path)
            
         Else
            SearchFolders objFolder
         End If
      Else
         SearchFolders objFolder
      End If
   Next
End Sub

Open in new window


-Bear
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now