记录简单脚本

时间:2012-09-26 21:17:51

标签: list logging vbscript copy

我正在尝试编写一个脚本,将文件从文件夹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

2 个答案:

答案 0 :(得分:0)

这是代码。如果它们在复制时丢失或失败,它将记录源文件名(完整路径)。请注意,在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

答案 1 :(得分:0)

在某些时候我厌倦了一遍又一遍地编写日志记录例程,所以我编写了一个类(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

'...