File io Vbscript-检查每个子文件夹中的文件和复制文件

File io Vbscript-检查每个子文件夹中的文件和复制文件,file-io,vbscript,directory,subdirectory,File Io,Vbscript,Directory,Subdirectory,我正在努力让这个脚本正常工作。 它基本上应该镜像两组文件夹,并确保它们完全相同。如果缺少文件夹,则应复制该文件夹及其内容 然后,脚本应该比较DateModified属性,并且仅当源文件比目标文件新时才复制文件 我正试着编一个剧本来做这个。到目前为止,我已经能够检查所有子文件夹,如果它们存在,然后创建它们,如果它们不存在。 然后,我可以扫描顶部的源文件夹中的文件,如果它们不存在或者源文件上的DateModified属性较新,则复制它们 剩下的基本上是扫描每个子文件夹中的文件,如果文件不存在或Dat

我正在努力让这个脚本正常工作。 它基本上应该镜像两组文件夹,并确保它们完全相同。如果缺少文件夹,则应复制该文件夹及其内容

然后,脚本应该比较DateModified属性,并且仅当源文件比目标文件新时才复制文件

我正试着编一个剧本来做这个。到目前为止,我已经能够检查所有子文件夹,如果它们存在,然后创建它们,如果它们不存在。 然后,我可以扫描顶部的源文件夹中的文件,如果它们不存在或者源文件上的DateModified属性较新,则复制它们

剩下的基本上是扫描每个子文件夹中的文件,如果文件不存在或DateModified stamp较新,则复制它们

代码如下:

Dim strSourceFolder, strDestFolder

strSourceFolder = "c:\users\vegsan\desktop\Source\"
strDestFolder = "c:\users\vegsan\desktop\Dest\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set objTopFolder = fso.GetFolder(strSourceFolder)
Set colTopFiles = objTopFolder.Files

'Check to see if subfolders actually exist. Create if they don't
Set objColFolders = objTopFolder.SubFolders
For Each subFolder in objColFolders
    CheckFolder subFolder, strSourceFolder, strDestFolder
Next

' Check all files in first top folder
For Each objFile in colTopFiles
    CheckFiles objFile, strSourceFolder, strDestFolder
Next

Sub CheckFolder (strSubFolder, strSourceFolder, strDestFolder)
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim folderName, aSplit

    aSplit = Split (strSubFolder, "\")
    UBound (aSplit)

    If UBound (aSplit) > 1 Then
        folderName = aSplit(UBound(aSplit))
        folderName = strDestFolder & folderName
    End if

    If Not fso.FolderExists(folderName) Then
        fso.CreateFolder(folderName)
    End if

End Sub

Sub CheckFiles (file, SourceFolder, DestFolder)

    Set fso = CreateObject("Scripting.FileSystemObject")
        Dim DateModified
        DateModified = file.DateLastModified
        ReplaceIfNewer file, DateMofidied, SourceFolder, DestFolder
End Sub


Sub ReplaceIfNewer (sourceFile, DateModified, SourceFolder, DestFolder)

    Const OVERWRITE_EXISTING = True
    Dim fso, objFolder, colFiles, sourceFileName, destFileName
    Dim DestDateModified, objDestFile

    Set fso = CreateObject("Scripting.FileSystemObject")

    sourceFileName = fso.GetFileName(sourceFile)
    destFileName = DestFolder & sourceFileName

    if Not fso.FileExists(destFileName) Then
        fso.CopyFile sourceFile, destFileName

    End if

    if fso.FileExists(destFileName) Then

        Set objDestFile = fso.GetFile(destFileName)
        DestDateModified = objDestFile.DateLastModified


        if DateModified <> DestDateModified Then
            fso.CopyFile sourceFile, destFileName
        End if

    End if

End Sub
Dim strSourceFolder,strDestFolder
strSourceFolder=“c:\users\vegsan\desktop\Source\”
strDestFolder=“c:\users\vegsan\Dest\”
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set objTopFolder=fso.GetFolder(strSourceFolder)
设置colTopFiles=objTopFolder.Files
'检查子文件夹是否确实存在。如果他们不这样做,就创造
设置objColFolders=objTopFolder.SubFolders
对于objColFolders中的每个子文件夹
CheckFolder子文件夹、strSourceFolder、strDestFolder
下一个
'检查第一个顶级文件夹中的所有文件
对于colTopFiles中的每个objFile
检查文件objFile、strSourceFolder、strDestFolder
下一个
子检查文件夹(strSubFolder、strSourceFolder、strDestFolder)
设置fso=CreateObject(“Scripting.FileSystemObject”)
暗文件夹名称,aSplit
aSplit=Split(strSubFolder,“\”)
UBound(aSplit)
如果UBound(aSplit)>1,则
folderName=aSplit(UBound(aSplit))
folderName=strDestFolder&folderName
如果结束
如果不是fso.FolderExists(folderName),则
fso.CreateFolder(folderName)
如果结束
端接头
子检查文件(文件、源文件夹、目标文件夹)
设置fso=CreateObject(“Scripting.FileSystemObject”)
暗日期修改
DateModified=file.DateLastModified
替换更新的文件、DateMoFIDED、SourceFolder、DestFolder
端接头
Sub-ReplaceIfNewer(sourceFile、DateModified、SourceFolder、DestFolder)
Const OVERWRITE_EXISTING=True
Dim fso、objFolder、colFiles、sourceFileName、destFileName
Dim DestDateModified,objDestFile
设置fso=CreateObject(“Scripting.FileSystemObject”)
sourceFileName=fso.GetFileName(sourceFile)
destFileName=DestFolder&sourceFileName
如果不存在fso.FileExists(destFileName),则
fso.CopyFile源文件,destFileName
如果结束
如果存在fso.files(destFileName),则
设置objDestFile=fso.GetFile(destFileName)
DestDateModified=objDestFile.DateLastModified
如果DateModified DestDateModified则
fso.CopyFile源文件,destFileName
如果结束
如果结束
端接头

我知道这是一篇老文章,但我一直在寻找一种方法来运行VBS,根据修改的日期复制和备份数据,并运行所有子目录和文件,偶然发现了一个基于上述问题的解决方案

您的代码行中有一个错误

ReplaceIfNewer file, DateMofidied, SourceFolder, DestFolder
您已经修改了DateModified miss spelled,导致该文件无法通过您的文件发送。datelastmodified on发送到您的sub。另外,在我修复该文件后,您的代码正在复制第一级文件和文件夹

我在这段代码的基础上复制了多个级别的子目录,并在每个corespondng子目录中复制文件,方法是再次调用子目录本身,每次使用动态数组重命名源文件夹

这组代码将比较这两个文件,并将旧文件替换为新文件。 见代码:

Dim i
Dim defaultchoice
Dim Defaultuser
Dim Theday
Dim Source
Dim driveletter 
Dim backup1 
Dim destin
Dim objshell
Dim objf
Dim Bsplit
Dim k
Dim total
Dim SourceFolder 
Dim DestFolder
Dim objFSO
Dim Objfolder
Dim Msg1


'**********************************************************
' Start off your arrays at zero
'**********************************************************
i=0


'**********************************************************
'set default choice to 1 run with user input to select source and destination or 0 to follow below schedule
'**********************************************************
defaultchoice = 0
Defaultuser = "*******"


Set objFSO = CreateObject("Scripting.FileSystemObject")

'**********************************************************
' Define default locations where you get data and where you want to put it depending on the day, BAcking up something different every day in the week
'**********************************************************
Theday = weekday(now())

if Theday = 2 then
    Source = "U:\**"
    destin = "H:\**\Backups"
elseif Theday = 4 then
    Source ="C:\***\backups"
    destin = "H:\***\Backups"
elseif Theday = 3 then
    Source ="U:\****"
    destin = "H:\****\Backups"
elseif Theday = 5 then
    Source ="C:\Users\*****\Documents"
    destin = "H:\*****\Backups"
elseif Theday = 6 then
    Source = "L:\******\data"
    destin = "H:\******\Backups"

else
    Wscript.Quit
end if

if defaultchoice = 1 then
    MSG1 = MsgBox("Do you wish to manually enter your location",vbyesno,"Select")
    If MSG1 = vbyes then
        Source = inputbox("Enter the file location you wish to get data from",,Source)
        Destin = inputbox("Enter the file location you wish to Backup to",,destin)
    else    
        Set objShell = CreateObject("Shell.Application")
        Set objF = objShell.BrowseForFolder(0, "Choose folder to get data from", 0, 17)
        checkfolderagain objf
        source = objF.self.path

        Destin = inputbox("Enter the file location you wish to Backup to",,destin)
    end if

end if

'**********************************************************
' Check to see if your source exists
'**********************************************************
If objFSO.FolderExists(Source) Then
'**********************************************************
' Create Destination folder if it doesn't exist
'**********************************************************

    BSplit = Split (destin, "\")
    total = UBound (BSplit)
    Backup1= Bsplit(i)
    If objfso.FolderExists(Backup1) Then
        For k= 1 to total
        Backup1= Backup1 & "\" & Bsplit(k)
        If objFSO.FolderExists (backup1) Then
        Else
            Set objFolder = objFSO.CreateFolder(backup1)
        End If
        next
    else
        Msgbox("Destination Drive does not exist")
        Wscript.Quit
    end if

'**********************************************************
' Format to utilize the Get folder command
'**********************************************************

    SourceFolder = source & "\"
    DestFolder = destin & "\"

'**********************************************************
' Execute the Sub to write files and sub folders
'**********************************************************
    copyfirstfilesandsubs Sourcefolder, Destfolder      
else
    Msgbox("Source folder does not exist")
end if

set i = nothing
Set defaultchoice = nothing
set Defaultuser = nothing
Set Theday = nothing
set Source = nothing
set driveletter = nothing 
set backup1 = nothing 
set destin = nothing
Set objshell = nothing
Set objf = nothing
Set Bsplit = nothing
Set k = nothing
Set total = nothing
set objFSO = nothing
set Objfolder = nothing
Set Msg1 = nothing

'**********************************************************
' first copy each file in top directory then create each subfolder
'**********************************************************
Sub copyfirstfilesandsubs(strsourcefolder,strdestfolder)

'**********************************************************
' Get the files that are in source folder and define top folder
'********************************************************** 
    Dim objColFolders
    Dim colTopFiles
    Dim objTopFolder

    Set objTopFolder = objfso.GetFolder(strsourcefolder)
    Set colTopFiles = objTopFolder.Files

    For Each objFile in colTopFiles
        CheckFiles objFile, strSourceFolder, strDestFolder
    Next

    Set objColFolders = objTopFolder.SubFolders
    For Each subFolder in objColFolders
        CheckFolder subFolder, strSourceFolder, strDestFolder
    next

    set objColFolders = nothing
    Set colTopFiles = nothing
    Set objTopFolder = nothing
end sub

'**********************************************************
' looks at modified date and sends date to ReplaceIfNewer
'**********************************************************
Sub CheckFiles (file, CFSourceFolder, CFDestFolder)

    Dim DateModified
    DateModified = file.DateLastModified
    ReplaceIfNewer file, DateModified, CFSourceFolder, CFDestFolder
End Sub

'**********************************************************
'copys file if it doesn't exist or updates whichever version of the file is older or does nothing if they are equal
'**********************************************************
Sub ReplaceIfNewer (File, DateModified, CFSourceFolder, CFDestFolder)
    Dim sourcefilename, destFileName, objDestFile, DestDateModified

    Const OVERWRITEEXISTING = True
    sourceFileName = objfso.GetFileName(File)
    destFileName = CFDestFolder & sourceFileName
    if objfso.FileExists(destFileName) Then
        Set objDestFile = objfso.GetFile(destFileName)
            DestDateModified = objDestFile.DateLastModified 
    if DateModified > DestDateModified Then
                objfso.CopyFile File, destFileName, OVERWRITEEXISTING
        elseif DateModified < DestDateModified Then
                objfso.CopyFile destFileName, File, OVERWRITEEXISTING
        End if
    else
        objfso.CopyFile File, destFileName

    End if
End Sub

'**********************************************************
'Creates folder if it currently doesn not exist, Creates new source folder path based on the folder it is in and repeats process at lower level.
'**********************************************************
Sub CheckFolder (SubFolder, cfoSourceFolder, cfoDestFolder)

    Dim foldername
    Dim asplit
    Dim chkdestfolder
    Dim SourceFolder2()
    Dim DestFolder2()

        aSplit = Split (SubFolder, "\")
    UBound (aSplit)
        If UBound (aSplit) > 1 Then
         folderName = aSplit(UBound(aSplit))                

    End if
    chkdestfolder = cfoDestFolder  & folderName
'**********************************************************
'Identify any folders that you don't have permissions to copy from they will error out as you do not have permission to this folder
'**********************************************************
    if subfolder = "C:\Users\" & defaultuser & "\Documents\My Shapes" or subfolder="C:\Users\" & defaultuser & "\Documents\My Music" or subfolder="C:\Users\" & defaultuser & "\Documents\My Pictures"or subfolder="C:\Users\" & defaultuser & "\Documents\My Videos" then  
    else
    If Not objfso.FolderExists(chkdestfolder) Then
        objfso.CreateFolder(chkdestfolder)
    End if

    i=i+1

'**********************************************************
'Redefine Source folder  and destination folder one level deeper    
'**********************************************************
    ReDim Preserve SourceFolder2(i)
    ReDim Preserve DestFolder2(i)
    SourceFolder2(i) = cfoSourceFolder & foldername & "\"
    DestFolder2(i) = chkdestfolder & "\"

'**********************************************************
'Execute the sub to write folders within the subfolder you just created 
'**********************************************************
    copyfirstfilesandsubs SourceFolder2(i), DestFolder2(i)
    end if
    set foldername = nothing 
    set asplit = nothing  
    set chkdestfolder = nothing 
End Sub

Sub checkfolderagain (objf)
        If objF Is Nothing Then    
            Wscript.Quit
        End If
end sub
Dim i
模糊默认选择
Dim Defaultuser
昏暗的日子
暗源
模糊文字
暗备份1
暗淡的命运
昏暗的奥布舍尔
Dim objf
昏暗的
暗k
模糊总数
Dim SourceFolder
变暗文件夹
Dim objFSO
Dim Objfolder
暗Msg1
'**********************************************************
'从零开始阵列
'**********************************************************
i=0
'**********************************************************
'将默认选项设置为1使用用户输入运行以选择源和目标,或将默认选项设置为0以遵循以下计划
'**********************************************************
defaultchoice=0
Defaultuser=“*******”
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
'**********************************************************
'根据日期定义获取数据和放置数据的默认位置,每周每天备份不同的内容
'**********************************************************
Theday=工作日(现在())
如果日期=2,则
Source=“U:\**”
destin=“H:\**\Backups”
如果其他日期=4,则
Source=“C:\***\backups”
destin=“H:\***\Backups”
如果其他日期=3,则
Source=“U:\**”
destin=“H:\******\Backups”
否则,则为5天
Source=“C:\Users\*****\Documents”
destin=“H:\******\Backups”
否则,则为6天
Source=“L:\******\data”
destin=“H:\******\Backups”
其他的
Wscript.Quit
如果结束
如果defaultchoice=1,则
MSG1=MsgBox(“您希望手动输入您的位置吗”,vbyesno,“选择”)
如果MSG1=vbyes,则
Source=inputbox(“输入要从“,”Source)获取数据的文件位置)
Destin=inputbox(“输入要备份到的文件位置”、、Destin)
其他的
设置objShell=CreateObject(“Shell.Application”)
设置objF=objShell.BrowseForFolder(0,“选择要从中获取数据的文件夹”,0,17)
复选框增益objf
source=objF.self.path
Destin=inputbox(“输入要备份到的文件位置”、、Destin)
如果结束
如果结束
'**********************************************************
'检查您的源是否存在
'*****************************************************