Created
March 13, 2025 03:53
-
-
Save Ausjorg/a683726b9bbc5605c446d9888878d6ea to your computer and use it in GitHub Desktop.
R Script Path Llama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to determine the OneDrive path based on the operating system | |
get_onedrive_path <- function() { | |
os <- Sys.info()["sysname"] | |
if (os == "Windows") { | |
user_profile <- Sys.getenv("USERPROFILE") | |
onedrive_path <- file.path(user_profile, "OneDrive") | |
} else if (os == "Darwin") { # macOS | |
home <- Sys.getenv("HOME") | |
onedrive_path <- file.path(home, "OneDrive") | |
} else { | |
stop("Unsupported operating system. Please specify the OneDrive path manually.") | |
} | |
# Check if the OneDrive path exists | |
if (!dir.exists(onedrive_path)) { | |
stop(paste("OneDrive path not found at:", onedrive_path, | |
"\nPlease ensure OneDrive is installed and synced, then specify the correct path.")) | |
} | |
return(normalizePath(onedrive_path, winslash = "/")) | |
} | |
# Recursive function to scan directories and collect metadata | |
scan_dir <- function(current_path) { | |
# Initialize result list for this directory | |
result <- list() | |
# Try to get metadata for the current directory | |
dir_info <- tryCatch({ | |
file.info(current_path) | |
}, error = function(e) { | |
if (grepl("path too long", e$message, ignore.case = TRUE)) { | |
# Handle long path by changing directory | |
old_wd <- getwd() | |
setwd(current_path) | |
on.exit(setwd(old_wd)) # Revert back after processing | |
dir_info <- file.info(".") | |
dir_info | |
} else { | |
stop(e) | |
} | |
}) | |
# Add current directory metadata with full path | |
result[[1]] <- list( | |
path = normalizePath(current_path, winslash = "/"), | |
name = basename(current_path), | |
type = "directory", | |
size = NA, | |
lastModified = dir_info$mtime | |
) | |
# Try to list subdirectories and files with full paths | |
dirs <- tryCatch({ | |
list.dirs(current_path, recursive = FALSE, full.names = TRUE) | |
}, error = function(e) { | |
if (grepl("path too long", e$message, ignore.case = TRUE)) { | |
# Navigate into the directory to handle long paths | |
old_wd <- getwd() | |
setwd(current_path) | |
on.exit(setwd(old_wd)) # Revert back after processing | |
# List subdirectories with full paths | |
sub_dirs <- list.dirs(".", recursive = FALSE) | |
file.path(current_path, sub_dirs) | |
} else { | |
stop(e) | |
} | |
}) | |
files <- tryCatch({ | |
list.files(current_path, recursive = FALSE, full.names = TRUE) | |
}, error = function(e) { | |
if (grepl("path too long", e$message, ignore.case = TRUE)) { | |
# Navigate into the directory to handle long paths | |
old_wd <- getwd() | |
setwd(current_path) | |
on.exit(setwd(old_wd)) # Revert back after processing | |
# List files with full paths | |
sub_files <- list.files(".", recursive = FALSE) | |
file.path(current_path, sub_files) | |
} else { | |
stop(e) | |
} | |
}) | |
# Collect metadata for files with full paths | |
for (file in files) { | |
file_info <- tryCatch({ | |
file.info(file) | |
}, error = function(e) { | |
if (grepl("path too long", e$message, ignore.case = TRUE)) { | |
# Navigate into the directory if file path is too long | |
old_wd <- getwd() | |
setwd(current_path) | |
on.exit(setwd(old_wd)) | |
file_info <- file.info(basename(file)) | |
file_info | |
} else { | |
stop(e) | |
} | |
}) | |
result[[length(result) + 1]] <- list( | |
path = normalizePath(file, winslash = "/"), | |
name = basename(file), | |
type = "file", | |
size = file_info$size, | |
lastModified = file_info$mtime | |
) | |
} | |
# Recurse into subdirectories with full paths | |
for (dir in dirs) { | |
sub_result <- scan_dir(dir) | |
result <- c(result, sub_result) | |
} | |
return(result) | |
} | |
# Main function to scan OneDrive and collect metadata | |
scan_onedrive <- function() { | |
# Get the OneDrive path | |
onedrive_path <- get_onedrive_path() | |
# Save the original working directory | |
original_wd <- getwd() | |
on.exit(setwd(original_wd)) # Ensure we revert back | |
# Scan the directory recursively | |
all_metadata <- scan_dir(onedrive_path) | |
# Convert the list of metadata to a data frame | |
metadata_df <- do.call(rbind, lapply(all_metadata, as.data.frame)) | |
# Sort by path for consistency | |
metadata_df <- metadata_df[order(metadata_df$path), ] | |
return(metadata_df) | |
} | |
# Run the scan and display the first few rows | |
metadata_df <- scan_onedrive() | |
print(head(metadata_df)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment