?
Solved

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

Posted on 2008-10-09
3
Medium Priority
?
197 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:rgh119
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22679439
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

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 200 total points
ID: 22679480
I changed it up a little to reduce the duplication in the copy commands.
'-------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.CreateFolder(parentpath1)
        objFSO.CreateFolder(parentpath2)
END IF
	
objFSO.CopyFile "c:\test\*.txt", parentpath1, OverwriteExisting
 
CALL GetFilesList("c:\test", "TXT", " was copied to c:\test\parentpath1")
 
objFSO.CopyFile "c:\test\*.bak", parentpath2, OverwriteExisting
 
CALL GetFilesList("c:\test", "BAK", " was copied to c:\test\parentpath2")
		
	       	
Set objFSO = Nothing
 
 
'--------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

0
 

Author Closing Comment

by:rgh119
ID: 31504676
Thank you so much for quick and accurate answer.  I really appreciate your help.  Awesome job!!!
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.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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