【问题标题】:Simple Script With Logging带有日志记录的简单脚本
【发布时间】:2012-09-26 21:17:51
【问题描述】:

我正在尝试编写一个脚本,将文件从文件夹 A 复制到文件夹 B,它只会复制来自列表文件的文件。

然后我需要它来记录任何无法复制的文件。这是我到目前为止所拥有的,我只是无法让日志记录工作。

    Option Explicit

Dim Sour, Dest
Dim oFSO, sFile, oFile, sText
Dim objFSO, objFileCopy
Dim strFilePath, strDestination, strSource
Const ForReading = 1, ForWriting = 2, ForAppending = 8

strLoggingFiles = "C:\failedtransfer.txt"

strSource = InputBox("Enter source path information") 'Enter your source path here
strDestination = InputBox("Enter destination path information") 'Enter your destination path here

'Set the read of the input file and prompt for location
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = InputBox("Enter path to text document with files to be copied:")

'Open the read, get the file name of the file to be copied, and copy it to new location
If oFSO.FileExists(sFile) Then
   Set oFile = oFSO.OpenTextFile(sFile, ForReading)
   Do While Not oFile.AtEndOfStream
      sText = oFile.ReadLine
      If (Trim(sText) <> "") And _
         oFSO.FileExists(strSource & "\" & sText) Then

         oFSO.CopyFile strSource & "\" & sText, strDestination
      Else
         WScript.Echo "Couldn't find " & strSource & "\" & sText
      End If
   Loop
   oFile.Close
Else
   WScript.Echo "The file was not there."
End If

【问题讨论】:

  • 我没有看到任何记录日志的代码。你忘记写了吗?
  • 我尝试编写一些日志记录代码,但没有一个工作,所以我发布了工作的部分,在顶部你可以看到 strLoggingFiles 我让它创建日志文件名的地方。

标签: list logging vbscript copy


【解决方案1】:

这是代码。如果复制时丢失或失败,它将记录源文件名(完整路径)。请注意,在 Vista/Win7+ 中,如果要将文件放入根目录,则需要管理员权限。

Option Explicit

Dim Sour, Dest
Dim oFSO, oLog, sFile, oFile, sText
Dim objFSO, objFileCopy
Dim strFilePath, strDestination, strSource
Const ForReading = 1, ForWriting = 2, ForAppending = 8

strLoggingFiles = "C:\failedtransfer.txt"

strSource = InputBox("Enter source path information") 'Enter your source path here
strDestination = InputBox("Enter destination path information") 'Enter your destination path here

'Set the read of the input file and prompt for location
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = InputBox("Enter path to text document with files to be copied:")

'Open the read, get the file name of the file to be copied, and copy it to new location
If oFSO.FileExists(sFile) Then
   'Open/create log file
   set oLog = oFSO.OpenTextFile(strLoggingFiles, ForAppending, True)

   Set oFile = oFSO.OpenTextFile(sFile, ForReading)
   Do While Not oFile.AtEndOfStream
      sText = oFile.ReadLine
      If (Trim(sText) <> "") And _
         oFSO.FileExists(strSource & "\" & sText) Then

         On Error Resume Next 'disable quit on error
         oFSO.CopyFile strSource & "\" & sText, strDestination
         If Err.Number <> 0 Then
             oLog.WriteLine strSource & "\" & sText 'log failed copy
         End If
         On Error Goto 0 'enable quit on error
      Else
         WScript.Echo "Couldn't find " & strSource & "\" & sText
         oLog.WriteLine strSource & "\" & sText 'log failed copy 'log missing source
      End If
   Loop
   oFile.Close
   oLog.Close 'close log file
Else
   WScript.Echo "The file was not there."
End If

【讨论】:

  • 它正在记录整个传输,我只想记录失败。
  • 错字,抱歉。 Er.Number 应该是 Err.Number。现在更新了。
  • 成功了,现在是我的下一个问题。我想把输入框改成浏览框,而不是strSource= InputBox("") 可以把strSource=objshell.browseforfolder(o, "select source folder",0)
  • 是的,使用Shell.Application 对象。
【解决方案2】:

在某些时候,我厌倦了一遍又一遍地编写日志记录例程,所以我写了一个类 (CLogger) 作为记录到不同设施(控制台、事件日志、文件)的抽象层:

Set clog = New CLogger
clog.LogFile = "C:\failedtransfer.txt"
clog.LogToConsole  = False
clog.LogToEventlog = False

'...

On Error Resume Next
oFSO.CopyFile strSource & "\" & sText, strDestination
If Err Then
  clog.LogError strSource & "\" & sText & ": " & FormatErrorMessage(Err)
End If
On Error Goto 0

'...

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2012-07-11
    • 2016-04-22
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多