2011-06-23 54 views
0
複製文件
Sub Copy_Files_Dates() 
'This example copy all files between certain dates from FromPath to ToPath. 
'You can also use this to copy the files from the last ? days 
'If Fdate >= Date - 30 Then 
'Note: If the files in ToPath already exist it will overwrite 
'existing files in this folder 
    Dim FSO As Object 
    Dim FromPath As String 
    Dim ToPath As String 
    Dim Fdate As Date 
    Dim FileInFromFolder As Object 

    FromPath = "C:\Users\Ron\Data" '<< Change 
    ToPath = "C:\Users\Ron\Test" '<< Change 

    If Right(FromPath, 1) <> "\" Then 
     FromPath = FromPath & "\" 
    End If 

    If Right(ToPath, 1) <> "\" Then 
     ToPath = ToPath & "\" 
    End If 

    Set FSO = CreateObject("scripting.filesystemobject") 

    If FSO.FolderExists(FromPath) = False Then 
     MsgBox FromPath & " doesn't exist" 
     Exit Sub 
    End If 

    If FSO.FolderExists(ToPath) = False Then 
     MsgBox ToPath & " doesn't exist" 
     Exit Sub 
    End If 

    For Each FileInFromFolder In FSO.getfolder(FromPath).Files 
     Fdate = Int(FileInFromFolder.DateLastModified) 
     'Copy files from 1-Oct-2006 to 1-Nov-2006 
     If Fdate >= DateSerial(2006, 10, 1) And Fdate <= DateSerial(2006, 11, 1) Then 
      FileInFromFolder.Copy ToPath 
     End If 
    Next FileInFromFolder 

    MsgBox "You can find the files from " & FromPath & " in " & ToPath 

End Sub 

我收到錯誤錯誤在VBS

line 1 
char 9 
error expected end of statement 
code 800A0401 

回答

0

變化Dim name As SomethingDim name

Dim FSO As Object 
    Dim FromPath 
    Dim ToPath 
    Dim Fdate 
    Dim FileInFromFolder 

    ' *snip* 

而且Next FileInFromFolder只是Next

For Each FileInFromFolder In FSO.getfolder(FromPath).Files 
    Fdate = Int(FileInFromFolder.DateLastModified) 
    'Copy files from 1-Oct-2006 to 1-Nov-2006 
    If Fdate >= DateSerial(2006, 10, 1) And Fdate <= DateSerial(2006, 11, 1) Then 
     FileInFromFolder.Copy ToPath 
    End If 
Next 

這看起來像一個VB6代碼片段,您試圖讓它作爲VBS工作。 VB6和VBS之間有一些細微差別,就像你需要注意的一樣。

我修正了這些,它似乎運行沒有錯誤,雖然我沒有真正測試它是否正確地複製文件。

+0

沒錯,VBScript不會讓你把任何特定的數據類型變成暗淡的變量。一切都是'變種'。 –