2012-09-26 47 views
0

我想寫一個腳本,將文件從文件夾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 
+0

我沒有看到任何執行日誌記錄的代碼。你忘了寫嗎? – Jay

+0

我試着編寫一些日誌代碼,但沒有任何工作,所以我發佈了工作的部分,在頂部可以看到strLoggingFiles,我在其中創建了日誌文件名。 –

回答

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 
+0

它正在記錄整個傳輸,我只是想記錄失敗。 –

+0

錯字錯誤,抱歉。 'Er.Number'應該是'Err.Number'。現在更新了。 – Jay

+0

這工作,現在我的下一個問題。我想改變輸入框到瀏覽框,而不是strSource = InputBox(「」)我可以把strSource = objshell.browseforfolder(o,「選擇源文件夾」,0) –

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 

'...