ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [VB.Net] 파일 생성일자로 폴더 생성 후 파일이동
    VB(A) 2017. 6. 4. 21:15


    옛날 사진들이 한 폴더에 왕창 모여있는데

    iPhoto는 날짜별로 알아서 분류해주는 기능이 있었는데

    Photos로 넘어오면서 그런 기능이 없어져버려서 사진 관리에 어려움이 생겼다.


    하여 사진생성날짜를 기준으로 폴더별로 분류해주는 코드를 짬.


    이 파일들은 마지막 수정한 날짜를 검색해야 생성한 날짜를 뱉는 바람에

    고민고민 하다가 수정한 날짜를 뽑아내는 코드를 짰다.


    이미지 생성일자를 가져오는 메소드도 있는데

    이 파일에는 적용이 안 돼서.. 할 수 없이 파일 생성일자를 가져옴.

    원본이 아니라 한번 편집한 파일이라서 그럴지도..


    Option Explicit On
    Imports System.IO
    Public Class Form1
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
            Dim SelectedFolder As String
            Dim myDate As Date
            Dim DateFolder As String
     
            Try
                '사진을 날짜별로 분류할 폴더를 선택합니다.
                With FolderBrowserDialog1
                    .ShowDialog()
                    SelectedFolder = .SelectedPath
                End With
     
                '폴더 내의 파일을 순환합니다.
                For Each LogFile In Directory.GetFiles(SelectedFolder)
     
                    myDate = GetDatePictureTaken(LogFile)   '사진 파일이 찍힌 날짜
                    DateFolder = SelectedFolder & "\" & myDate  '날짜명의 폴더
     
                    '날짜 폴더가 없으면 폴더 생성
                    If Not Directory.Exists(DateFolder) Then
                        Directory.CreateDirectory(DateFolder)
                    End If
     
                    '날짜 폴더로 사진 이동
                    File.Move(LogFile, DateFolder & "\" & Path.GetFileName(LogFile))
                Next
     
            Catch ex As Exception
                MsgBox(ex.Message)
                Exit Sub
     
            End Try
     
            Close()
        End Sub
     
        Public Shared Function GetDatePictureTaken(path As StringAs Date
            '사진 찍은 날짜 가져오기
     
            Dim fileCreatedDate As Date = File.GetLastWriteTime(path)
     
            Return Date.Parse(fileCreatedDate).ToShortDateString
     
        End Function
     
    End Class
     
    cs


    GetDatePictureTaken.zip


    'VB(A)' 카테고리의 다른 글

    월보 취합 서식  (0) 2017.08.29
    난수 생성기(로또)  (0) 2017.06.24
    [VB.Net] 화면보호기  (0) 2017.06.03
    지역별로 시트 생성하여 나누기  (0) 2017.05.16
    주식표 변경(상승, 하락 글꼴변경, 주요 금액단위 변경)  (0) 2017.05.07

    댓글