|
|
楼主 |
发表于 2013-5-27 06:36:39
|
显示全部楼层
本帖最后由 demo 于 2013-5-27 22:38 编辑
With a little extra function attached. maybe good for some special circumstances. AND... It's easy to extand it ....- ' --------------------------------
- ' Function: Synchronise files in different places either local or over the network
- ' Developed by: John Z
- ' Contact: cq_box@163.com
- ' Date: May 23, 2013
- ' -----------------------------------
- Const srcDir = "\\The\Source\Dat"
- Const destDir = "D:\Destination\Dir"
- Const LogFile = "\\Server\Logs\MonthEnd_Log"
- Const ErrLog = "\\Server\Logs\Error_Log"
- Const OVERWRITE_EXISTING = True
- Const DayOfDeffer = 27
-
- Dim FileList
- Dim Flg, strErr
-
- FileList = Array("binloc.txt","order.txt","product.txt","customer.txt")
- Flg = False
- strErr = ""
- Set fso = CreateObject("Scripting.FileSystemObject")
- For Each fn In FileList
- On Error Resume Next
-
- If fso.FileExists(srcDir & "" & fn) Then
- If Not fso.FileExists(DestDir & "" & fn) Then
- Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(LogFile,8,true)
- fso.CopyFile srcDir & "/" & fn, DestDir & "" & fn, True
- objFileToWrite.WriteLine(Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " --- copy " & DestDir & "" & fn & " from " & srcDir & "" & fn)
- objFileToWrite.Close
- Set objFileToWrite = Nothing
- Flg = True
- ElseIf fso.FileExists(srcDir & "" & fn) Then
- ReplaceIfNewer srcDir & "" & fn, DestDir & "" & fn, DayOfDeffer, OVERWRITE_EXISTING
- End If
- End If
-
- If err.number <> 0 Then
- Set objErrToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(ErrLog,8,true)
- objErrToWrite.WriteLine(Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " --- Error on File (" & fn & "): " & err.Description)
- objErrToWrite.Close
- Set objErrToWrite = Nothing
- End If
- Next 'fn
-
- If Flg Then
- CrLf_Convert FileList
- End If
-
- Set fso = Nothing
- 'Wscript.Echo "Done."
-
-
- Sub ReplaceIfNewer(strSourceFile, strTargetFile, DayDiffer, OVRWrite)
- Dim objFso
- Dim objTargetFile
- Dim dtmTargetDate
- Dim objSourceFile
- Dim dtmSourceDate
- Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(LogFile,8,true)
- Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
- Set objTargetFile = objFso.GetFile(strTargetFile)
- dtmTargetDate = objTargetFile.DateLastModified
- Set objSourceFile = objFso.GetFile(strSourceFile)
- dtmSourceDate = objSourceFile.DateLastModified
- If ( dtmSourceDate > DateAdd("d", DayOfDeffer, dtmTargetDate )) Then
- objFso.CopyFile objSourceFile.Path, objTargetFile.Path, OVRWrite
- objFileToWrite.WriteLine(Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " --- update " & strTargetFile & " from " & strSourceFile)
- Flg = True
- End If
- Set objFso = Nothing
- objFileToWrite.Close
- Set objFileToWrite = Nothing
- End Sub
-
- Sub CrLf_Convert(objARG)
- '*
- '* Declare Constants
- '*
-
- Const cVBS = "2crlf_v2.vbs - Convert CR/LF to CRLF Version 2"
-
- '*
- '* Declare Variables
- '*
-
- Dim boo_CR
- Dim boo_LF
- Dim boo_OK
- Dim intARG, intLEN
- Dim strARG, strDST
- Dim strMSG
-
- strMSG = "Converted files:" & vbCrLf
- strDST = "_a.txt"
-
- Dim strOTF
-
- '*
- '* Declare Objects
- '*
-
- 'Dim objARG
- 'Set objARG = WScript.Arguments
- Dim objFSO, objLogWrite
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- Set objLogToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(LogFile,8,true)
- Dim objOTF
-
- '*
- '* Convert Each Drag-and-Drop File
- '*
-
- For intARG = 0 to uBound(objARG) 'objARG.Count - 1
- Dim dFile
- On Error Resume Next
-
- strARG = destDir & "" & objARG(intARG)
- intLEN = len(strARG)
- 'strDST = destDir & "" & mid(strARG,1,intLEN-4) & "_a.txt" 'strDst
- strDST = mid(strARG,1,intLEN-4) & "_a.txt" 'strDst
- strMSG = strMSG & vbCrLf & strARG
- If objFSO.FileExists(strARG) Then
-
- '*
- '* Read File
- '*
-
- dFile = strARG
- Set objOTF = objFSO.OpenTextFile(strARG,1)
- strOTF = objOTF.ReadAll()
- Set objOTF = Nothing
-
- '*
- '* Convert File (if applicable)
- '*
-
- boo_CR = False
- boo_LF = False
- dFile = strDST
-
- If InStr(strOTF,vbCr) > 0 Then boo_CR = True
- If InStr(strOTF,vbLf) > 0 Then boo_LF = True
-
- If boo_CR And boo_LF Then
- boo_OK = False
- strMSG = strMSG & vbCrLf & vbTab & "(Not Converted)"
- ElseIf boo_CR Then
- strOTF = Replace(strOTF,vbCr,vbCrLf)
- boo_OK = True
- strMSG = strMSG & vbCrLf & vbTab & "(Converted CR)"
- ElseIf boo_LF Then
- strOTF = Replace(strOTF,vbLf,vbCrLf)
- boo_OK = True
- strMSG = strMSG & vbCrLf & vbTab & "(Converted LF)"
- End If
-
- '*
- '* Write File
- '*
-
- If boo_OK Then
- Set objOTF = objFSO.OpenTextFile(strDST,2,True)
- objOTF.Write(strOTF)
- Set objOTF = Nothing
- End If
-
- Else
- strMSG = strMSG & vbCrLf & vbTab & "(File Not Found)"
- End If
- If err.number <> 0 Then
- Set objErrToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(ErrLog,8,true)
- objErrToWrite.WriteLine(Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " --- Error on File (" & dFile & "): " & err.Description)
- objErrToWrite.Close
- Set objErrToWrite = Nothing
- objLogToWrite.WriteLine(Year(Date) & "-" & Month(Date) & "-" & Day(Date) & " " & Time & " --- Error!!! " & dFile & ": see error log for detail.")
- End If
-
- Next
-
- '*
- '* Destroy Objects
- '*
-
- 'Set objARG = Nothing
- Set objFSO = Nothing
- objLogToWrite.Close
- Set objLogToWrite = Nothing
-
- '*
- '* Done
- '*
-
- 'MsgBox strMSG,vbInformation,cVBS
- End Sub
复制代码 |
|