Link to home
Start Free TrialLog in
Avatar of rgh119
rgh119

asked on

I would like to know how to put the names of files that I am copying into a log file so that i can verify they were copied successfully

I am using the FSO.CopyFile object.  The script is attached.

Thanks for you help
'-------START LogToFile Configuration-------
'Disable logging globally by
'setting the bEnableLogging option to false.
bEnableLogging = True
 
'Setting this to true will time stamp Each
'message that is logged to the log file
'with the current date and time.
bIncludeDateStamp = True
 
'This will set the log file name to the
'current date and time. You can use this
'option to create incremental log files.
bPrependDateStampInLogFileName = False
 
'Specify the log file location here. Path
'must contain a trailing backslash. If you
'would like to log to the same location as
'the currently running script, set this
'value to "relative" or uncomment out the
'line below.
sLogFileLocation = "C:\TEST\"
'sLogFileLocation = "relative"
 
'Specify the log file name here.
sLogFileName = "logtofiletest.txt"
 
'You can set whether or not you would like
'the script to append to an existing file,
'or if you would like it to overwrite
'existing copies. To overwrite set the
'sOverWriteORAppend variable to "overwrite"
sOverWriteORAppend = "append"
 
'Here you can set the maximum number of
'lines you like to record. If the maximum
'is reached the beginning of the log file
'will be pruned. Setting this to a value
'of 0 will disable this function.
vLogMaximumLines = 0
 
'This is just like limiting the log file
'to a number of lines but limits by the
'total size of the log file. This value
'is in bytes. Setting this to 0 will
'disable this function.
vLogMaximumSize = 0
'-------END LogToFile Configuration-------
 
'-------START Push Script-----------------
 
dim strday, strmonth, stryear, parentpath, cmd1, cmd2, CopyVer
 
Const OverwriteExisting = TRUE
 
parentpath1 = "c:\TEST\ParentPath1"
parentpath2 = "c:\TEST\ParentPath2"
    
strDay = Day(Date)
If Len(strDay) < 2 Then
	strDay = 0 & strDay
End If
 
strMonth = Month(Date)
If Len(strMonth) < 2 Then
	strMonth = 0 & strMonth
End If
 
strYear = Year(Date)
strDate = "\" & strYear & strMonth & strDay
 
cmd1 = "dir"
cmd2 = "cd\"
cmd3 = "exit"
 
 
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objshell.run "%comspec% /k " & cmd1 & "&" & cmd2 & "&" & cmd3
'objShell.Run "%comspec% /k e: & exit"
 
Set objFSO = CreateObject("scripting.filesystemobject")
 
 
If objFSO.FolderExists(parentpath1) Then
	intMsg = MsgBox("This script appears to have already been ran once today.  Would you like to run it again?",vbYesNo,"Run script again?")
		If intMsg = vbNo Then
		        wscript.Quit
		        
		else
		
			objFSO.CopyFile "c:\test\*.txt", parentpath1, OverwriteExisting
			logtofile "All TXT files were copied to c:\test\parentpath1"
		
 
			Set objFSO = Nothing
			Set objFSO = CreateObject("scripting.filesystemobject")
			objFSO.CopyFile "c:\test\*.bak", parentpath2, OverwriteExisting
			logtofile "All BAK files were copied to c:\test\parentpath2"
				
		End If
	Else
 
 
		objFSO.CreateFolder(parentpath1)
		objFSO.CopyFile "c:\test\*.txt", parentpath1, OverwriteExisting
		logtofile "All TXT files were copied to c:\test\parentpath1"
		Set objFSO = Nothing
		
 
		Set objFSO = CreateObject("scripting.filesystemobject")
		objFSO.CreateFolder(parentpath2)
		objFSO.CopyFile "c:\test\*.bak", parentpath2, OverwriteExisting
		logtofile "All BAK files were copied to c:\test\parentpath2"
		
	       	
Set objFSO = Nothing
 
 
End If
 
'--------STOP Push Script--------------------------------
 
 
'--------START Subroutine to Create Log File-------------
 
Sub LogToFile(Message)
 
    If bEnableLogging = False Then Exit Sub
 
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
 
    Set oLogFSO = CreateObject("Scripting.FileSystemObject")
   
    If sLogFileLocation = "relative" Then
        Set oLogShell = CreateObject("Wscript.Shell")
        sLogFileLocation = oLogShell.CurrentDirectory & "\"
        Set oLogShell = Nothing
    End If
   
    If bPrependDateStampInLogFileName Then
        sNow = Replace(Replace(Now(),"/","-"),":",".")
        sLogFileName = sNow & " - " & sLogFileName
        bPrependDateStampInLogFileName = False       
    End If
   
    sLogFile = sLogFileLocation & sLogFileName
   
    If sOverWriteORAppend = "overwrite" Then
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
        sOverWriteORAppend = "append"
    Else
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)
    End If
 
    If bIncludeDateStamp Then
        Message = Now & "   " & Message
    End If
 
    oLogFile.WriteLine(Message)
    
    oLogFile.Close
   
    If vLogMaximumLines > 0 Then
      Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True)   
      sFileContents = oReadLogFile.ReadAll
      aFileContents = Split(sFileContents, vbCRLF)
      If Ubound(aFileContents) > vLogMaximumLines Then
        sFileContents = Replace(sFileContents, aFileContents(0) & _
        vbCRLF, "", 1, Len(aFileContents(0) & vbCRLF))
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
        oLogFile.Write(sFileContents)
        oLogFile.Close
      End If
      oReadLogFile.Close
    End If
    
    If vLogMaximumSize > 0 Then
      Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True)  
      sFileContents = oReadLogFile.ReadAll
      oReadLogFile.Close
      sFileContents = RightB(sFileContents, (vLogMaximumSize*2))
      Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
      oLogFile.Write(sFileContents)
      oLogFIle.Close
    End If
    
    oLogFSO = Null
End Sub

Open in new window

Avatar of AmazingTech
AmazingTech

Inserted this into your script.
'-------START LogToFile Configuration-------
'Disable logging globally by
'setting the bEnableLogging option to false.
bEnableLogging = True
 
'Setting this to true will time stamp Each
'message that is logged to the log file
'with the current date and time.
bIncludeDateStamp = True
 
'This will set the log file name to the
'current date and time. You can use this
'option to create incremental log files.
bPrependDateStampInLogFileName = False
 
'Specify the log file location here. Path
'must contain a trailing backslash. If you
'would like to log to the same location as
'the currently running script, set this
'value to "relative" or uncomment out the
'line below.
sLogFileLocation = "C:\TEST\"
'sLogFileLocation = "relative"
 
'Specify the log file name here.
sLogFileName = "logtofiletest.txt"
 
'You can set whether or not you would like
'the script to append to an existing file,
'or if you would like it to overwrite
'existing copies. To overwrite set the
'sOverWriteORAppend variable to "overwrite"
sOverWriteORAppend = "append"
 
'Here you can set the maximum number of
'lines you like to record. If the maximum
'is reached the beginning of the log file
'will be pruned. Setting this to a value
'of 0 will disable this function.
vLogMaximumLines = 0
 
'This is just like limiting the log file
'to a number of lines but limits by the
'total size of the log file. This value
'is in bytes. Setting this to 0 will
'disable this function.
vLogMaximumSize = 0
'-------END LogToFile Configuration-------
 
'-------START Push Script-----------------
 
dim strday, strmonth, stryear, parentpath, cmd1, cmd2, CopyVer
 
Const OverwriteExisting = TRUE
 
parentpath1 = "c:\TEST\ParentPath1"
parentpath2 = "c:\TEST\ParentPath2"
    
strDay = Day(Date)
If Len(strDay) < 2 Then
	strDay = 0 & strDay
End If
 
strMonth = Month(Date)
If Len(strMonth) < 2 Then
	strMonth = 0 & strMonth
End If
 
strYear = Year(Date)
strDate = "\" & strYear & strMonth & strDay
 
cmd1 = "dir"
cmd2 = "cd\"
cmd3 = "exit"
 
 
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objshell.run "%comspec% /k " & cmd1 & "&" & cmd2 & "&" & cmd3
'objShell.Run "%comspec% /k e: & exit"
 
Set objFSO = CreateObject("scripting.filesystemobject")
 
 
If objFSO.FolderExists(parentpath1) Then
	intMsg = MsgBox("This script appears to have already been ran once today.  Would you like to run it again?",vbYesNo,"Run script again?")
		If intMsg = vbNo Then
		        wscript.Quit
		        
		else
		
			objFSO.CopyFile "c:\test\*.txt", parentpath1, OverwriteExisting
 
			CALL GetFilesList("c:\test", "TXT", " was copied to c:\test\parentpath1")
		
 
			Set objFSO = Nothing
			Set objFSO = CreateObject("scripting.filesystemobject")
			objFSO.CopyFile "c:\test\*.bak", parentpath2, OverwriteExisting
 
			CALL GetFilesList("c:\test", "BAK", " was copied to c:\test\parentpath2")
				
		End If
	Else
 
 
		objFSO.CreateFolder(parentpath1)
		objFSO.CopyFile "c:\test\*.txt", parentpath1, OverwriteExisting
 
                CALL GetFilesList("c:\test", "TXT", " was copied to c:\test\parentpath1")
		Set objFSO = Nothing
		
 
		Set objFSO = CreateObject("scripting.filesystemobject")
		objFSO.CreateFolder(parentpath2)
		objFSO.CopyFile "c:\test\*.bak", parentpath2, OverwriteExisting
         	CALL GetFilesList("c:\test", "BAK", " was copied to c:\test\parentpath2")
		
	       	
Set objFSO = Nothing
 
 
End If
 
'--------STOP Push Script--------------------------------
 
'--------START Get file list Script----------------------
 
Sub GetFilesList(Folder, Extension, Message)
    Set objFSOList = CreateObject("Scripting.FileSystemObject")
    For Each File In objFSOList.GetFolder(Folder).Files
        IF ucase(Right(File.Path, Len(File.Path) - InStrRev(File.Path, "."))) = Extension then LOGTOFILE File.Path & Message
    Next
    Set objFSOList = Nothing
End Sub 
 
'--------STOP Get file list Script-----------------------
 
'--------START Subroutine to Create Log File-------------
 
Sub LogToFile(Message)
 
    If bEnableLogging = False Then Exit Sub
 
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
 
    Set oLogFSO = CreateObject("Scripting.FileSystemObject")
   
    If sLogFileLocation = "relative" Then
        Set oLogShell = CreateObject("Wscript.Shell")
        sLogFileLocation = oLogShell.CurrentDirectory & "\"
        Set oLogShell = Nothing
    End If
   
    If bPrependDateStampInLogFileName Then
        sNow = Replace(Replace(Now(),"/","-"),":",".")
        sLogFileName = sNow & " - " & sLogFileName
        bPrependDateStampInLogFileName = False       
    End If
   
    sLogFile = sLogFileLocation & sLogFileName
   
    If sOverWriteORAppend = "overwrite" Then
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
        sOverWriteORAppend = "append"
    Else
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForAppending, True)
    End If
 
    If bIncludeDateStamp Then
        Message = Now & "   " & Message
    End If
 
    oLogFile.WriteLine(Message)
    
    oLogFile.Close
   
    If vLogMaximumLines > 0 Then
      Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True)   
      sFileContents = oReadLogFile.ReadAll
      aFileContents = Split(sFileContents, vbCRLF)
      If Ubound(aFileContents) > vLogMaximumLines Then
        sFileContents = Replace(sFileContents, aFileContents(0) & _
        vbCRLF, "", 1, Len(aFileContents(0) & vbCRLF))
        Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
        oLogFile.Write(sFileContents)
        oLogFile.Close
      End If
      oReadLogFile.Close
    End If
    
    If vLogMaximumSize > 0 Then
      Set oReadLogFile = oLogFSO.OpenTextFile(sLogFile, ForReading, True)  
      sFileContents = oReadLogFile.ReadAll
      oReadLogFile.Close
      sFileContents = RightB(sFileContents, (vLogMaximumSize*2))
      Set oLogFile = oLogFSO.OpenTextFile(sLogFile, ForWriting, True)
      oLogFile.Write(sFileContents)
      oLogFIle.Close
    End If
    
    oLogFSO = Null
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgh119

ASKER

Thank you so much for quick and accurate answer.  I really appreciate your help.  Awesome job!!!