투케이2K

199. (swift5/xcode) [간단 소스] FileManager 파일 매니저 creationDate , modificationDate 사용해 파일 생성 및 수정 시간 확인 본문

Swift

199. (swift5/xcode) [간단 소스] FileManager 파일 매니저 creationDate , modificationDate 사용해 파일 생성 및 수정 시간 확인

투케이2K 2023. 12. 11. 18:58
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

 

// [파일 생성 시간 확인] : [filePaths == 특정 디렉토리 및 파일 명칭을 포함하는 path]
do {
	let attributes:[FileAttributeKey:Any] = try FileManager.default.attributesOfItem(atPath: filePaths)
	let createdDate = attributes[FileAttributeKey.creationDate] as? Date
                            
	let dateFormatter = DateFormatter() // Date 포맷 객체 선언
	dateFormatter.locale = Locale(identifier: "ko") // 한국 지정
                                                        
	// [date 포맷 타입 정의]
	dateFormatter.dateFormat = "yyyyMMddHHmmss" // Date 포맷 타입 지정
	var returnData = dateFormatter.string(from: createdDate!) // 포맷된 형식 문자열로 반환
}
catch {
}





// [파일 수정 시간 확인] : [filePaths == 특정 디렉토리 및 파일 명칭을 포함하는 path]
do {
	let attributes:[FileAttributeKey:Any] = try FileManager.default.attributesOfItem(atPath: filePaths)
	let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date
                            
	let dateFormatter = DateFormatter() // Date 포맷 객체 선언
	dateFormatter.locale = Locale(identifier: "ko") // 한국 지정
                                                        
	// [date 포맷 타입 정의]
	dateFormatter.dateFormat = "yyyyMMddHHmmss" // Date 포맷 타입 지정
	var returnData = dateFormatter.string(from: modificationDate!) // 포맷된 형식 문자열로 반환
}
catch {
}

 

반응형
Comments