2013-08-05 4 views
0

VBScript 또는 .bat 파일을 작성하여 디렉터리 a에있는 특정 확장명 *.sch의 최신 파일 두 개를 다른 디렉터리로 이동하고 싶습니다.가장 최근의 두 파일을 새 디렉터리에 복사

실험 대상 : $newest 두 번째로 최신의 것을 어떻게 찾습니까? 이처럼 할 수있는 VBScript로

감사

+1

하면 '최근'무엇을 의미합니까를? 마지막으로 생성되었거나 마지막으로 수정 되었습니까? –

+0

최종 수정 날짜 고맙습니다. – TSAM1776

답변

2

:

src = "C:\source\folder" 
dst = "C:\destination\folder" 

Set fso = CreateObject("Scripting.FileSystemObject") 

mostRecent = Array(Nothing, Nothing) 

For Each f In fso.GetFolder(src).Files 
    If LCase(fso.GetExtensionName(f.Name)) = "sch" Then 
    If mostRecent(0) Is Nothing Then 
     Set mostRecent(0) = f 
    ElseIf f.DateLastModified > mostRecent(0).DateLastModified Then 
     Set mostRecent(1) = mostRecent(0) 
     Set mostRecent(0) = f 
    ElseIf mostRecent(1) Is Nothing Or f.DateLastModified > mostRecent(1).DateLastModified Then 
     Set mostRecent(1) = f 
    End If 
    End If 
Next 

For i = 0 To 1 
    If Not mostRecent(i) Is Nothing Then mostRecent(i).Copy dst & "\" 
Next 

편집 : 위의 코드는하지만, 너무 확장되지 않습니다. 가장 최근의 2 개 파일 이상을 필요로한다면 약간 다른 접근 방식을 취할 수 있습니다. 처리 할 파일 수만큼 배열을 만들고 여유 슬롯이 있거나 현재 파일이 배열에있는 가장 오래된 파일보다 새로운 경우 정렬 된 삽입을 수행하십시오.

src = "C:\source\folder" 
dst = "C:\destination\folder" 
num = 2 
last = num-1 

Function IsNewer(a, b) 
    IsNewer = False 
    If b Is Nothing Then 
    IsNewer = True 
    Exit Function 
    End If 
    If a.DateLastModified > b.DateLastModified Then IsNewer = True 
End Function 

Set fso = CreateObject("Scripting.FileSystemObject") 

ReDim mostRecent(last) 
For i = 0 To last 
    Set mostRecent(i) = Nothing 
Next 

For Each f In fso.GetFolder(src).Files 
    If LCase(fso.GetExtensionName(f.Name)) = "sch" Then 
    If IsNewer(f, mostRecent(last)) Then Set mostRecent(last) = Nothing 
    For i = last To 1 Step -1 
     If Not IsNewer(f, mostRecent(i-1)) Then Exit For 
     If Not mostRecent(i-1) Is Nothing Then 
     Set mostRecent(i) = mostRecent(i-1) 
     Set mostRecent(i-1) = Nothing 
     End If 
    Next 
    If mostRecent(i) Is Nothing Then Set mostRecent(i) = f 
    End If 
Next 

For i = 0 To num-1 
    If Not mostRecent(i) Is Nothing Then mostRecent(i).Copy dst & "\" 
Next 

대안은 CMD-내장 dir 명령에 포격하고 출력 읽는 것 :

num = 2 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set sh = CreateObject("WScript.Shell") 

cmd = "cmd /c dir /a-d /b /o-d """ & sh.CurrentDirectory & """\*.*" 
Set dir = sh.Exec(cmd) 
Do While dir.Status = 0 
    WScript.Sleep 100 
Loop 

i = num 
Do Until i = 0 Or dir.StdOut.AtEndOfStream 
    f = dir.StdOut.ReadLine 
    fso.CopyFile f, dst & "\" 
    i = i - 1 
Loop 
+0

감사합니다. robocopy ... ROBOCOPY C : \ alohaqs \ newdata C : \ mirus \ schedule * .sch/maxage : 15 – TSAM1776

관련 문제