R05(2023)/08/04
macOS Ventura 13.4.1(c)(22F770820d), Xcode Version 14.3.1 (14E300c)
ChatGPT-3.5
C 言語でコードを完成させ、それをChatGPT-3.5 にC++ 言語に変換。更にSwift 言語に変換してもらいました。
Command 実行時に与えられたフォルダパス内のファイルの作成日時、変更日時、アクセス日時、i node 変更日時を表示します。「.」でファイル名が始まるファイルは除外されます。100 個のファイルが入ったフォルダで試しました。また、フォルダ内にフォルダが有る場合の反応は確認していません。
//
// main.swift
// nanoSeccreationtimeFolderSwift03
//
// Created by niki on 2023/08/03.
import Foundation
func getTimeString(date: Date) -> String {
let calendar = Calendar.current
let nanoseconds = calendar.component(.nanosecond, from: date)
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = formatter.string(from: date)
return "\(dateString).\(String(format: "%09d", nanoseconds))"
}
func getFileTimes(filePath: String) -> [String: String]? {
let fileURL = URL(fileURLWithPath: filePath)
do {
let attributes = try FileManager.default.attributesOfItem(atPath: fileURL.path)
var timesDict: [String: String] = [:]
if let creationDate = attributes[.creationDate] as? Date {
timesDict["File Creation Time (nanoseconds)"] = getTimeString(date: creationDate)
}
if let modificationDate = attributes[.modificationDate] as? Date {
timesDict["File Modification Time (nanoseconds)"] = getTimeString(date: modificationDate)
}
if let resourceValues = try? fileURL.resourceValues(forKeys: [.creationDateKey, .attributeModificationDateKey]) {
if let accessDate = resourceValues.creationDate {
timesDict["File Access Time (nanoseconds)"] = getTimeString(date: accessDate)
}
if let changeDate = resourceValues.attributeModificationDate {
timesDict["File i-node Change Time (nanoseconds)"] = getTimeString(date: changeDate)
}
}
return timesDict
} catch {
print("Error: \(error)")
}
return nil
}
// コマンドライン引数からフォルダパスを取得する
let arguments = CommandLine.arguments
guard arguments.count >= 2 else {
print("フォルダパスを指定してください。")
exit(1)
}
let folderPath = arguments[1]
// フォルダ内のファイルのCreation TimeとModification Time、Access Time、i-node Change Timeを取得して表示する
do {
let fileURLs = try FileManager.default.contentsOfDirectory(at: URL(fileURLWithPath: folderPath), includingPropertiesForKeys: [.creationDateKey], options: [.skipsHiddenFiles])
let sortedFileURLs = fileURLs.sorted(by: { url1, url2 in
do {
let attributes1 = try FileManager.default.attributesOfItem(atPath: url1.path)
let attributes2 = try FileManager.default.attributesOfItem(atPath: url2.path)
if let creationDate1 = attributes1[.creationDate] as? Date, let creationDate2 = attributes2[.creationDate] as? Date {
return creationDate1 < creationDate2
}
} catch {
print("Error: \(error)")
}
return false
})
for fileURL in sortedFileURLs {
if let timesDict = getFileTimes(filePath: fileURL.path) {
print("File Name: \(fileURL.lastPathComponent)")
for key in ["File Creation Time (nanoseconds)", "File Modification Time (nanoseconds)", "File Access Time (nanoseconds)", "File i-node Change Time (nanoseconds)"] {
if let value = timesDict[key] {
print("\(key): \(value)")
}
}
print()
} else {
print("Failed to get file times for \(fileURL.lastPathComponent).")
}
}
} catch {
print("Error: \(error)")
exit(1)
}