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
188 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
  • 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 50 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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