2012-08-13 2 views
0

VBScripting에서 작업 할 필요가있는 곳으로 문제가 발생했습니다. 도와 주셔서 미리 감사드립니다.두 번째로 수정 된 파일을 폴더에 복사하십시오. 도움이 필요합니다.

두 번째로 수정 된 파일을 한 폴더에서 다른 위치로 복사하려고합니다.

예 : 원본 폴더 "최종"에는 여러 하위 폴더가 포함됩니다. 스크립트를 실행 한 후 "Final"의 모든 하위 폴더에서 두 번째로 최신 수정 된 파일을 확인해야하며 대상 폴더에 복사해야합니다.

+0

문제를 일으킨 곳과 자신이 한 일을 알려주십시오. 지금 묻는 방법은 일종의 [ "이게 내 문제 야, 내게 코드를 제발!"] (http://slash7.com/2006/12/22/vampires/). – AutomatedChaos

답변

0

@user이 기능은 사용자가 찾고있는 기능이라고 생각합니다. 문제의 핵심은 Sub Find()입니다. 버그가 없다는 보장은 없으며 예외 처리가 더욱 향상 될 수 있다고 확신합니다.

: Microsoft의 신뢰할 수있는 WSH 참조를 Windows 도움말 파일로 다운로드하는 방법은 this answer을 참조하십시오.

Option Explicit 

Dim oFF : Set oFF = New FileFinder 
oFF.RootFolder = "C:\Source\Folder" ' absolute or relative path 
oFF.DestinationFolder = "C:\Copy\Folder\" ' must end with backslash 

On Error Resume Next 

' 
' Find the newest and second-newest files. 
' 
oFF.Find 

If Err Then 
    WScript.Echo "Find error: " & Err.Description 
    WScript.Quit(1) 
Else 
    WScript.Echo "Newest file: " & oFF.NewestFilePath 
    WScript.Echo "Second-newest file: " & oFF.SecondNewestFilePath 
End If 

' 
' Copy the second-newest file to the destination folder. 
' 
oFF.CopySecondNewestFileToDestination 

If Err Then 
    WScript.Echo "Copy error: " & Err.Description 
    WScript.Quit(1) 
Else 
    WScript.Echo "'" & oFF.SecondNewestFilePath _ 
      & "' was copied to folder '" & oFF.DestinationFolder & "'." 
End If 

Set oFF = Nothing 

' ============================================================ 

Class FileFinder 

' Public RootFolder 
Public NewestFilePath 
Public NewestFileDate 
Public SecondNewestFilePath 
Public SecondNewestFileDate 

Private mFso 
Private mRootFolder 
Private mDestinationFolder 

Private Sub Class_Initialize() 
    Set mFso = CreateObject("Scripting.FileSystemObject") 
    Me.SecondNewestFilePath = "" 
    Me.SecondNewestFileDate = CDate("1970/01/01") 
    Me.NewestFilePath = "" 
    Me.NewestFileDate = DateAdd("s", 1, Me.SecondNewestFileDate) 
End Sub 

Private Sub Class_Terminate() 
    Set mFso = Nothing 
End Sub 

Public Property Let RootFolder(sValue) 
    If Not mFso.FolderExists(sValue) Then 
     Err.Raise vbObjectError + 1, "", _ 
      "Root folder '" & sValue & "' does not exist." 
    End If 

    mRootFolder = sValue 
End Property 

Public Property Get RootFolder() 
    RootFolder = mRootFolder 
End Property 

Public Property Let DestinationFolder(sValue) 
    If Not (Right(sValue, 1) = "\") Then 
     Err.Raise vbObjectError + 1, "", _ 
      "Destination folder '" & sValue & "' must end with a backslash." 
    End If 

    If Not mFso.FolderExists(sValue) Then 
     Err.Raise vbObjectError + 1, "", _ 
      "Destination folder '" & sValue & "' does not exist." 
    End If 

    mDestinationFolder = sValue 
End Property 

Public Property Get DestinationFolder() 
    DestinationFolder = mDestinationFolder 
End Property 

Public Sub Find() 
    Dim oFolder : Set oFolder = mFso.GetFolder(RootFolder) 
    Dim oSubFolder, oFile 

    For Each oSubFolder In oFolder.SubFolders 
     For Each oFile In oSubFolder.Files 
      ' 
      ' File is newer than newest file. 
      ' 
      If DateDiff("s", NewestFileDate, _ 
         oFile.DateLastModified) > 0 Then 
       SecondNewestFilePath = NewestFilePath 
       SecondNewestFileDate = NewestFileDate 
       NewestFilePath = oFile.Path 
       NewestFileDate = oFile.DateLastModified 

      ' 
      ' File is newer than second-newest file. 
      ' 
      ElseIf DateDiff("s", SecondNewestFileDate, _ 
          oFile.DateLastModified) > 0 Then 
       SecondNewestFilePath = oFile.Path 
       SecondNewestFileDate = oFile.DateLastModified 
      End If 
     Next 
    Next 
End Sub 

Public Sub CopySecondNewestFileToDestination() 
    mFso.CopyFile SecondNewestFilePath, DestinationFolder 
End Sub 

End Class 
관련 문제