반응형
마지막이다.. 주석달기도 빡세군.. 빡세..왠지모르게 친숙하다.. 암튼.. 정말 마지막.. 쿠쿠
Option Explicit
Dim currDate, ConvertDate
Dim objFSO, objSrcFolder, colSubFolders, objSubFolder, colSrcFiles, objSrcFile, strSrcFile
Dim file, compFile, getTgtFolder, strLog, srcFolder, tgtFolder, bakFolder
Dim fileCnt, copyCnt, bakCnt
fileCnt = 0
copyCnt = 0
bakCnt = 0
currDate = DateAdd("d", 0 , Date) ' 2007-08-30 형식으로 된다.
ConvertDate = ConvertDate8(currDate) ' 날짜를 8자리로 변경
srcFolder = "C:\xodim\UpdateSrc" ' 업데이트할 소스 폴더
tgtFolder = "C:\xodim\bbackgoon" ' 업데이트 폴더 ( 운영하고 있는 폴더)
bakFolder = "C:\xodim\SrcBackup" ' 업데이트 할 경우 기존 소스를 백업할 폴더
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objSrcFolder = objFSO.GetFolder(srcFolder)
Set colSubFolders = objSrcFolder.SubFolders ' 소스폴더의 서브 폴더를 가져온다.
For Each objSubFolder In colSubFolders
If objSubFolder.Name = ConvertDate Then ' 소스폴더에 현재 날짜 폴더가 있으면
GetFileList(objSubFolder.Name)
End If
Next
Function GetFileList(Byval path) ' 폴더 패스를 입력받아 파일 리스트를 구한다.
Dim intPos, getFileName
Set objSrcFolder = objFSO.GetFolder(srcFolder & "\" & path)
Set colSrcFiles = objSrcFolder.Files
For Each objSrcFile In colSrcFiles ' 폴더에 파일이 있으면
file = path & "\" & objSrcFile.Name ' path 에 파일 이름을 붙이고
compFile = Split(file, ConvertDate & "\") ' 현재 날짜로 split 하면
strLog = strLog & "파일 : " & compFile(1) & vbCrLf
' 두번째 인자( 여기서는 compFile(1) )가 업데이트할 파일이다.
fileCnt = fileCnt + 1
getFileName = tgtFolder & "\" & compFile(1)
fileCnt = fileCnt + 1
getFileName = tgtFolder & "\" & compFile(1)
' 타겟 폴더(운영 폴더)에 파일 이름(compFile(1))을 붙여서
If objFSO.FileExists(getFileName) Then ' 파일이 타겟폴더에 존재하는지 확인
fncCreateFolder getFileName ' 존재하면 백업을 해야하므로 fncCreateFolder 을 수행
End If
fncCopyFile srcFolder & "\" & file ' 존재하지 않으면 파일을 복사
Next
Set colSubFolders = objSrcFolder.SubFolders
If objFSO.FileExists(getFileName) Then ' 파일이 타겟폴더에 존재하는지 확인
fncCreateFolder getFileName ' 존재하면 백업을 해야하므로 fncCreateFolder 을 수행
End If
fncCopyFile srcFolder & "\" & file ' 존재하지 않으면 파일을 복사
Next
Set colSubFolders = objSrcFolder.SubFolders
' 파일 외에 폴더가 존재하면 해당 폴더를 가지고 다시 GetFileList를 호출 (재귀)
For Each objSubFolder In colSubFolders
GetFileList(path & "\" & objSubFolder.Name)
Next
End Function
Sub fncCreateFolder(ByVal fileName)
Dim bakPath, srcFile, intPos, getFolder, getFileName
bakPath = bakFolder & "\" & ConvertDate & "_SrcBackup"
For Each objSubFolder In colSubFolders
GetFileList(path & "\" & objSubFolder.Name)
Next
End Function
Sub fncCreateFolder(ByVal fileName)
Dim bakPath, srcFile, intPos, getFolder, getFileName
bakPath = bakFolder & "\" & ConvertDate & "_SrcBackup"
' 백업 폴더에 "현재날짜_SrcBackup" 이라는 폴더를 생성한다.
intPos = InStrRev(fileName, "\") ' "\" 이 마지막으로 있는 위차값을 구한다.
getFolder = Left(fileName, intPos)
intPos = InStrRev(fileName, "\") ' "\" 이 마지막으로 있는 위차값을 구한다.
getFolder = Left(fileName, intPos)
' 인자로 넘어온 파일 이름에서 위에서 구한 위치값(intPos)까지의 문자가 폴더이다.
getFileName = Replace(fileName, getFolder, "") ' 따라서 폴더를 제외한게 파일 이름이다.
getFileName = Replace(fileName, getFolder, "") ' 따라서 폴더를 제외한게 파일 이름이다.
Set srcFile = objFSO.GetFile(fileName)
If srcFile.Attributes AND 1 Then ' 해당 파일의 읽기 속성을 제거한다.
srcFile.Attributes = srcFile.Attributes XOR 1
End If
If Not objFSO.FolderExists(bakPath) Then
' 현재 날짜의 백업폴더가 없으면 폴더를 생성한다.
objFSO.CreateFolder(bakPath)
End If
srcFile.Copy bakPath & "\" & getFileName ' 백업 폴더로 파일을 복사한다.
strLog = strLog & "백업 : " & bakPath & "\" & getFileName & vbCrLf
bakCnt = bakCnt + 1
End Sub
Sub fncCopyFile(ByVal fileName)
Dim srcFile, intPos, getFolder, getFileName, file, tgtPath, srcFolders, folders, folder, parentFolder
intPos = InStrRev(fileName, "\")
getFolder = Left(fileName, intPos) ' 소스 폴더 (업데이트할 파일이 있는 폴더)
getFileName = Replace(fileName, getFolder, "") ' 업데이트할 파일 이름
Set srcFile = objFSO.GetFile(fileName)
file = Split(srcFile, ConvertDate & "\")
srcFolders = Replace(file(1), getFileName, "") ' aaa\bbb\ 이런 식의 값을 갖는다.
If srcFolders <> "" Then
folders = Split(srcFolders, "\")
objFSO.CreateFolder(bakPath)
End If
srcFile.Copy bakPath & "\" & getFileName ' 백업 폴더로 파일을 복사한다.
strLog = strLog & "백업 : " & bakPath & "\" & getFileName & vbCrLf
bakCnt = bakCnt + 1
End Sub
Sub fncCopyFile(ByVal fileName)
Dim srcFile, intPos, getFolder, getFileName, file, tgtPath, srcFolders, folders, folder, parentFolder
intPos = InStrRev(fileName, "\")
getFolder = Left(fileName, intPos) ' 소스 폴더 (업데이트할 파일이 있는 폴더)
getFileName = Replace(fileName, getFolder, "") ' 업데이트할 파일 이름
Set srcFile = objFSO.GetFile(fileName)
file = Split(srcFile, ConvertDate & "\")
srcFolders = Replace(file(1), getFileName, "") ' aaa\bbb\ 이런 식의 값을 갖는다.
If srcFolders <> "" Then
folders = Split(srcFolders, "\")
' "\" 으로 split 하면 위의 값은 aaa, bbb, "" 으로 나뉜다.
For Each folder In folders
If folder <> "" Then
parentFolder = Split(srcFolders, folder)
For Each folder In folders
If folder <> "" Then
parentFolder = Split(srcFolders, folder)
' aaa 폴더가 없는 상태에서 bbb 폴더를 생성할 수 없기 때문에 이렇게 처리를 했다.
If parentFolder(0) = "" Then
If Not objFSO.FolderExists(tgtFolder & "\" & folder) Then
If parentFolder(0) = "" Then
If Not objFSO.FolderExists(tgtFolder & "\" & folder) Then
' 타겟 폴더에 aaa 폴더가 없으면
objFSO.CreateFolder(tgtFolder & "\" & folder)'폴더를 생성한다.
End If
Else
If Not objFSO.FolderExists(tgtFolder & "\" & parentFolder(0) & folder) Then
objFSO.CreateFolder(tgtFolder & "\" & folder)'폴더를 생성한다.
End If
Else
If Not objFSO.FolderExists(tgtFolder & "\" & parentFolder(0) & folder) Then
' 타겟 폴더에 bbb 폴더가 없으면
objFSO.CreateFolder(tgtFolder & "\" & parentFolder(0) & folder)
objFSO.CreateFolder(tgtFolder & "\" & parentFolder(0) & folder)
' aaa 폴더를 포함한 경로를 만들어서 bbb 폴더를 생성한다.
End If
End If
End If
Next
End If
If srcFile.Attributes AND 1 Then
srcFile.Attributes = srcFile.Attributes XOR 1
End If
tgtPath = tgtFolder & "\" & file(1) ' 타겟 폴더에 경로를 포함한 파일이름을 붙여서
srcFile.Copy tgtPath ' 소스폴더에 있는 값을 타겟폴더로 복사한다.
strLog = strLog & " : " & tgtPath & vbCrLf
strLog = strLog & "--------------------------------------------------------" & vbCrLf
copyCnt = copyCnt + 1
End Sub
Function ConvertDate8(Byval currDate) ' ex) 2007-08-30 를 20070830 로 변경
If IsDate(currDate) Then
ConvertDate8 = Replace(FormatDateTime(currDate, 2), "-", "")
Else
ConvertDate8 = ""
End If
End Function
Dim objLog, strScriptFullName ' 스크립트 폴더에 해당날짜로 로그 파일을 생성한다.
strScriptFullName = WScript.ScriptFullName
Set objLog = objFSO.OpenTextFile(srcFolder & "\log\" & Replace(FormatDateTime(Date, 2), "-", "") & ".log", 8, True)
strLog = strLog & fileCnt & " files" & vbCrLf
End If
End If
End If
Next
End If
If srcFile.Attributes AND 1 Then
srcFile.Attributes = srcFile.Attributes XOR 1
End If
tgtPath = tgtFolder & "\" & file(1) ' 타겟 폴더에 경로를 포함한 파일이름을 붙여서
srcFile.Copy tgtPath ' 소스폴더에 있는 값을 타겟폴더로 복사한다.
strLog = strLog & " : " & tgtPath & vbCrLf
strLog = strLog & "--------------------------------------------------------" & vbCrLf
copyCnt = copyCnt + 1
End Sub
Function ConvertDate8(Byval currDate) ' ex) 2007-08-30 를 20070830 로 변경
If IsDate(currDate) Then
ConvertDate8 = Replace(FormatDateTime(currDate, 2), "-", "")
Else
ConvertDate8 = ""
End If
End Function
Dim objLog, strScriptFullName ' 스크립트 폴더에 해당날짜로 로그 파일을 생성한다.
strScriptFullName = WScript.ScriptFullName
Set objLog = objFSO.OpenTextFile(srcFolder & "\log\" & Replace(FormatDateTime(Date, 2), "-", "") & ".log", 8, True)
strLog = strLog & fileCnt & " files" & vbCrLf
' 업데이트한 전체 파일갯수가 이렇고 백업파일은 몇개고 복사된 파일갯수는 이렇다라는 걸 보여줄려고 심심해서 추가했다.
strLog = strLog & bakCnt & " backup" & vbCrLf
strLog = strLog & copyCnt & " copy" & vbCrLf
objLog.Write strLog
objLog.Close
strLog = strLog & bakCnt & " backup" & vbCrLf
strLog = strLog & copyCnt & " copy" & vbCrLf
objLog.Write strLog
objLog.Close
반응형
'기술 공부 노트 > 개발이야기' 카테고리의 다른 글
| 윈도우2003에서 CVSNT + TortoiseCVS 설치하기(2) (0) | 2007.11.28 |
|---|---|
| 윈도우2003에서 CVSNT + TortoiseCVS 설치하기(1) (0) | 2007.11.28 |
| 원격지에서 압축파일을 가져와 압축을 푸는 스크립트 (1) | 2007.11.16 |
| 압축하는 스크립트 (0) | 2007.11.16 |
| 이런~~ 자바스크립트~! (1) | 2007.11.16 |