본문 바로가기
TechNical/WMI

일괄 파일 생성을 해 봅시다.

by 강멍멍이 2010. 6. 28.
반응형
.... 일괄 파일 생성이라.. 뭐 마땅히 제목을 붙일 만 한게 없어서 ;;
무엇인고 하니~
동일한 규칙에 따라 생성해야 하는 파일이 있습니다. 설정 파일 이었습니다.
내용이 다 똑같은 건 아니지만 폴더 명이나 기타 등등 조금만 변경해서 생성하면 되는 파일이 있었습니다.
근데 요걸 각 폴더 찾아가면서 일일이 복사해서 붙이고 폴더명 바꿔주고...
엄청난 카피 엔 페이스트와 오타 나거나 햇갈릴수도 있는 노가다의 최고봉이었습니다.

그래서.. 이 놈을 가만히 살펴보니 자동화가 가능합디다. 그래서 만든거였습니다.
이러저러한 사정이 있으므로 전체 소스를 공개 하진 않겠습니다. ㅋ
중간중간 막 잘라 먹고 이름변경해서 해 놨음둥 ㅎ
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
overWrite = ""
Do until Not overWrite = ""
 overWrite = InputBox("기존에 파일을 덮어쓰고 싶으면 Y 아니면 N(새로생성)을 입력하세요."  & vbCrlf & _
            "실행하기 싫으면 C 를 입력하든지 말든지")
loop
'대문자로 변환
overWrite = UCase(overWrite)
If overWrite = "C" Then
 WScript.Quit
End If
Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath)
     
If objFolder Is Nothing Then
   Wscript.Quit
End If
'다얄로그에서 루트 폴더명을 가져와요
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'폴더를 가져오기 위한 폴더 객체 생성
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objPath)
Set colSubfolders = objFolder.Subfolders
'쓰기용도 입니다
Const ForWriting = 2
'자바파일 찾기 용도임
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
'서브폴더를 뒤집시다.
For Each objSubfolder in colSubfolders
 '엄한 폴더 선택했을 때는 폴더 맹글면 안되연. 완전 혼날 듯...
 If objFSO.FolderExists(objSubfolder.Path & "\aaa") And objFSO.FolderExists(objSubfolder.Path & "\bbb\ccc") Then
   
  '서비스파일이 있을 폴더가 있는지 살펴 본다.
  If objFSO.FolderExists(objSubfolder.Path & "\aaa\bbbf\ccc") Then
    
     '덮어쓰기 여부 체크
     If overWrite = "Y" Then
      
      Set objFile = objFSO.OpenTextFile(objSubfolder.Path & "\aaa\bbb\ccc\" & objFolder.Name & "_" & objSubfolder.Name & "_kkk.xml" ,ForWriting,true)
    
     Else
    
      '이미 파일이 존재한다믄 다른거로 맹글자
      If objFSO.FileExists(objSubfolder.Path & "\aaa" & objFolder.Name & "_" & objSubfolder.Name & "_kkk.xml") Then     
       Set objFile = objFSO.OpenTextFile(objSubfolder.Path & "\aaa" & objFolder.Name & "_" & objSubfolder.Name & "_kkk_NEW.xml" ,ForWriting,true)         
      Else
       Set objFile = objFSO.OpenTextFile(objSubfolder.Path & "\aaa" & objFolder.Name & "_" & objSubfolder.Name & "_kkk.xml" ,ForWriting,true)
      End If
     
     End If    
         
    
   Else
    
    '폴더가 없으면 새로 생성해 준다.
    If Not objFSO.FolderExists(objSubfolder.Path & "\aa") Then
     objFSO.CreateFolder objSubfolder.Path & "\z"
    End If
    If Not objFSO.FolderExists(objSubfolder.Path & "\aa") Then
     objFSO.CreateFolder objSubfolder.Path & "\z"
    End If
   
    '파일을 생성하자.
    Set objFile = objFSO.OpenTextFile(objSubfolder.Path & "\a" & objFolder.Name & "_" & objSubfolder.Name & "_kk.xml" ,ForWriting,true)
    
   End If     
  
    '얘가 서비스 파일 내용을 맹글어여!!
    objFile.WriteLine "공통적으로 쓰이는 거"
    objFile.WriteLine "aaaaaa""" & objSubfolder.Name & """ bbbb" & objFolder.Name & "." & objSubfolder.Name & """ ccccc" 
    objFile.WriteLine "" 
 
    
    '파일을 뒤진다
     Set objFolder2 = objFSO.GetFolder(objSubfolder.Path & "\aaa" & objFolder.Name & "\" & objSubfolder.Name)
   Set colSubfolders2 = objFolder2.Subfolders
     
     For Each objSubfolder2 in colSubfolders2
     
      If InStr(objSubfolder2.path,".tmp") Then       
       Else
      
        '파일명을 따 온다. 나중에 써 먹을꺼야.
        Set colFiles = objWMIService.ExecQuery _
          ("ASSOCIATORS OF {Win32_Directory.Name='" & objSubfolder2.path & "'} Where " & "ResultClass = CIM_DataFile")
        
        For Each objFile2 In colFiles
          
          '파일명을 구분한다.
          If InStr(objFile2.FileName,"asdf") Then
           implName = objFile2.FileName
          Else
           inflName = objFile2.FileName
          End If
         
      Next    
     
         objFile.WriteLine " 이래저래 요래조래 요료래래래"
         objFile.WriteLine "             asdfasf" & objSubfolder2.Name & "." & inflName & """"  
         objFile.WriteLine ""
        
       End If  
     End If  
      
    Next
    
    objFile.WriteLine "끝"
  
 End If
Next
Wscript.Echo "다했다!!! ^^"
WScript.Quit

보시다시피 텍스트 파일 생성입니다.
생성하면서 중간중간에 폴더명도 써 먹고 파일명도 써 먹습니다.
이 스크립트를 작성하고 상당한 부분의 노가다를 줄였다는 뿌듯함.. ㅠ.ㅠㅋ
이런식으로 파일을 자동 생성가능하다는 사실? ㅋ
노가다를 줄이면서 살아 갑시다 ㅎㅎ
반응형

댓글