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
180 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you so much for quick and accurate answer.  I really appreciate your help.  Awesome job!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now