将1个最新的扩展MDB文件从源文件夹复制到WIN 2000的目标文件夹

时间:2013-12-29 07:16:46

标签: vbscript

我有一个脚本,根据最新的日期文件,它会将2个文件(ext:txt)从源文件夹复制到目标文件夹。我已经使用WIN 7和Vista进行了测试,它运行良好。我得到了我想要的输出。当我使用WIN 2000测试不同的文件扩展名MDB文件和文件夹路径时,我无法获得输出。在这里,我编辑只复制1个最新文件。请查看下面的脚本并帮助我: - (

Option Explicit

Dim FolderToCheck, FolderDestination, FileExt, mostRecent, noFiles, fso, fileList,     file, filecounter, oShell, strHomeFolder

' Enumerate current user's home path - we will use that by default later if nothing  specified in commandline
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")

'Variables -----
folderToCheck = strHomeFolder & "C:\Mms2\MDB"' Folder Source to check for recent files  to copy FROM
folderDestination = strHomeFolder & "C:\Documents and  Settings\Mms\Desktop\Test"'Destination Folder where to copy files TO

fileExt = "MDB"     ' Extension we are searching for
mostRecent = 1      ' Most Recent number of files to copy
' --------------


'PreProcessing()     ' Retrieve Command Line Parameters

' Display what we are intending on doing
' wscript.echo "Checking Source: " & FolderToCheck 
' wscript.echo "For Files of type: " & FileExt
' wscript.echo "Copying most recent "& mostRecent &" file(s) to: " & FolderDestination  & "."
' wscript.echo 

noFiles = TRUE

Set fso = CreateObject("Scripting.FileSystemObject")

Set fileList = CreateObject("ADOR.Recordset")
fileList.Fields.append "name", 200, 255
fileList.Fields.Append "date", 7
fileList.Open

If fso.FolderExists(FolderToCheck) Then 
    For Each file In fso.GetFolder(FolderToCheck).files
     If LCase(fso.GetExtensionName(file)) = LCase(FileExt) then
       fileList.AddNew
       fileList("name").Value = File.Path
       fileList("date").Value = File.DateLastModified
       fileList.Update
       If noFiles Then noFiles = FALSE
     End If
    Next
    If Not(noFiles) Then 
'       wscript.echo fileList.recordCount & " File(s) found. Sorting and copying last "  & mostRecent &"..."
        fileList.Sort = "date DESC"
        If Not(fileList.EOF) Then 
            fileList.MoveFirst
            If fileList.recordCount < mostRecent Then 
'               wscript.echo "WARNING: " & mostRecent &" file(s) specified but only " &  fileList.recordcount & " file(s) match criteria. Adjusted to " & fileList.RecordCount & "."
                mostRecent = fileList.recordcount
            End If

            fileCounter = 0
            Do Until fileList.EOF Or fileCounter => mostRecent
                If Not(fso.FolderExists(folderDestination)) Then 
'                   wscript.echo "Destination Folder did not exist. Creating..."
                    fso.createFolder folderDestination
                End If
                fso.copyfile fileList("name"), folderDestination & "\", True
'               wscript.echo  fileList("date").value & vbTab & fileList("name")
                fileList.moveNext
                fileCounter = fileCounter + 1
            Loop
        Else
'           wscript.echo "An unexpected error has occured."
        End If
    Else
'       wscript.echo "No matching """ & FileExt &""" files were found in """ &    foldertocheck & """ to copy."
    End If
Else
'   wscript.echo "Error: Source folder does not exist """ & foldertocheck & """."
End If

fileList.Close

Function PreProcessing
    Dim source, destination, ext, recent

    ' Initialize some variables
    Set source = Nothing
    Set destination = Nothing
    Set ext = Nothing
    Set recent = Nothing

    'Get Command Line arguments
    ' <scriptname>.vbs /Source:"C:\somepath\somefolder"     /Destination:"C:\someotherpath\somefolder" /ext:MDB /recent:1

    source = wscript.arguments.Named.Item("source")
    destination = wscript.arguments.Named.Item("destination")
    ext = wscript.arguments.Named.Item("ext")
    recent = wscript.arguments.Named.Item("recent")

    If source <> "" Then FolderToCheck = source
    If destination <> "" Then FolderDestination = destination
    If ext <> "" Then FileExt = ext
    If recent <> "" Then mostRecent = int(recent)

End Function

1 个答案:

答案 0 :(得分:1)

你需要改变一些事情:

删除这些行 - 不再需要它们了:

' Enumerate current user's home path - we will use that by default later if nothing  specified in commandline
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")

更新这些行:

'Variables -----
folderToCheck = "C:\Mms2\MDB" ' Folder Source to check for recent files  to copy FROM
folderDestination = "C:\Documents and Settings\Mms\Desktop\Test" 'Destination Folder where to copy files TO