?
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
?
204 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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