使用添加的时间戳备份一段时间并在循环中删除原始文件

时间:2014-03-20 02:20:28

标签: file-io vbscript

代码的主要部分在手动启动时可以正常工作,但在我使用外部程序启动时并不需要,所以我觉得AIO做得更好..

当我在备份和循环(在代码中标记为pt1,pt2,pt3)后添加几行来删除文件时,我得到以下语法错误:

Line:  66
Char  1
Syntax Error
800A03EA  MS VBscript compilation error

作为参考,这些是我检查和复制的链接:

Constantly look for file, when file exist, run command

http://www.devhut.net/2013/11/15/vbscript-backup-a-file-and-add-a-date-time-stamp/

'--------------------------------------START OF ADDED CODE pt1'
Do
'---------------------------------END OF ADDED CODE pt1'   

Dim objFSO 

Dim sSourceFolder 

Dim sDestFolder 

Dim sDBFile 

Dim sDateTimeStamp 

Const OVER_WRITE_FILES = True   


Set objFSO = CreateObject("Scripting.FileSystemObject") 

sSourceFolder = "C:\Program Files\N H P"

sBackupFolder = "C:\Program Files\N H P\ArchiveData"

sDBFile = "N-H-P" 

sDBFileExt = "csv" 

sDateTimeStamp = cStr(Year(now())) & _ 
        Pad(cStr(Month(now())),2) & _ 
        Pad(cStr(Day(now())),2) & _ 
        Pad(cStr(Hour(now())),2) & _ 
        Pad(cStr(Minute(now())),2)  


'If the backup folder doesn't exist, create it. 

If Not objFSO.FolderExists(sBackupFolder) Then 

objFSO.CreateFolder(sBackupFolder) 

End If   


'Copy the file as long as the file can be found 

If objFSO.FileExists(sSourceFolder & "\" & sDBFile & "." & sDBFileExt) Then 
    objFSO.CopyFile sSourceFolder & "\" & sDBFile & "." & sDBFileExt,_ 
        sBackupFolder & "\" & sDBFile & "_" & sDateTimeStamp & "." & sDBFileExt,_ 
        OVER_WRITE_FILES 
End if   


Set objFSO = Nothing     


Function Pad(CStr2Pad, ReqStrLen) 

Dim Num2Pad   


Pad = CStr2Pad 

If len(CStr2Pad) < ReqStrLen Then 

Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0") 

Pad = Num2Pad & CStr2Pad 

End If 


'-------------------------------------------START OF ADDED CODE pt2'
'Delete the file as long as the file can be found 

If objFSO.FileExists(sSourceFolder & "\" & sDBFile & "." & sDBFileExt) Then 
    objFSO.DeleteFile sSourceFolder & "\" & sDBFile & "." & sDBFileExt,_ 
        OVER_WRITE_FILES 
End if   

'-------------------------------------END OF ADDED CODE pt2'

Set objFSO = Nothing 

End Function

'--------------------------------START OF ADDED CODE pt3'

   WScript.Sleep 50000

Loop

 '------------------------------------START OF ADDED CODE pt3'

1 个答案:

答案 0 :(得分:1)

这里的问题是在你的Do Loop中。您尝试在循环中声明一个新函数。我真的建议您将代码格式化一点,这样可以更容易地发现错误。另外一个具有视觉大括号匹配(Notepad2,Notepad ++等)帮助的记事本编辑器。这里的代码已经过格式化和修改,但我没有测试实际的复制,删除。

Dim objFSO
Dim sSourceFolder, sDestFolder, sDBFile, sDateTimeStamp 

Const OVER_WRITE_FILES = True   

Do
    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    sSourceFolder = "C:\Program Files\N H P"
    sBackupFolder = "C:\Program Files\N H P\ArchiveData"
    sDBFile = "N-H-P" 
    sDBFileExt = "csv" 

    sDateTimeStamp = cStr(Year(now())) & _ 
            Pad(cStr(Month(now())),2) & _ 
            Pad(cStr(Day(now())),2) & _ 
            Pad(cStr(Hour(now())),2) & _ 
            Pad(cStr(Minute(now())),2)

    'If the backup folder doesn't exist, create it. 
    If Not objFSO.FolderExists(sBackupFolder) Then
        objFSO.CreateFolder(sBackupFolder)
    End If   


    'Copy the file as long as the file can be found 
    If objFSO.FileExists(sSourceFolder & "\" & sDBFile & "." & sDBFileExt) Then 
        objFSO.CopyFile sSourceFolder & "\" & sDBFile & "." & sDBFileExt,_ 
            sBackupFolder & "\" & sDBFile & "_" & sDateTimeStamp & "." & sDBFileExt,_ 
            OVER_WRITE_FILES 
    End If   

    'Delete the file as long as the file can be found 

    If objFSO.FileExists(sSourceFolder & "\" & sDBFile & "." & sDBFileExt) Then 
        objFSO.DeleteFile sSourceFolder & "\" & sDBFile & "." & sDBFileExt,_ 
            OVER_WRITE_FILES 
    End if

    Set objFSO = Nothing   
    WScript.Sleep 50000
Loop

Function Pad(CStr2Pad, ReqStrLen)
    Dim Num2Pad   

    Pad = CStr2Pad
    If len(CStr2Pad) < ReqStrLen Then
        Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0") 
        Pad = Num2Pad & CStr2Pad 
    End If
End Function