Hello
my
name
is
Reasha
Informatics is the best major!
getwd()
## [1] "C:/Users/rubbl/OneDrive - UW/The Info 201/PS3"
“../../desktop/PS3 New Folder”
pathString <- "../../desktop/PS3 New Folder"
stringRelative <- dir(pathString)
print(stringRelative)
## [1] "CheetahCubs.jpg"
## [2] "CheetahLooking.jpg"
## [3] "CheetahRunning.jpg"
## [4] "Empty F1"
## [5] "Empty F2"
## [6] "Empty F3"
## [7] "excel.xlsx"
## [8] "PDF 1.pdf"
## [9] "PDF 2.pdf"
## [10] "PDF 3.pdf"
## [11] "Reflection on Mindful Engagements.docx"
## [12] "Süleymaniye Mosque.pptx"
Yes
Yes
for (file_name in stringRelative) {
# Get file info
fullPath <- paste(pathString, sep = "/", file_name)
info <- file.info(fullPath)
# Check if the file is a directory
if (FALSE) {
#info$isdir) {
cat(paste(file_name, "is a directory\n"))
} else {
# If not a directory, get file size
file_size <- info$size
# Print the file name and size in a pretty manner
cat(paste(file_name, ": ", prettyNum(file_size, big.mark = ","), "\n", sep = ""))
}
}
## CheetahCubs.jpg: 84,081
## CheetahLooking.jpg: 173,440
## CheetahRunning.jpg: 70,368
## Empty F1: 0
## Empty F2: 0
## Empty F3: 0
## excel.xlsx: 9,474
## PDF 1.pdf: 328,979
## PDF 2.pdf: 681,308
## PDF 3.pdf: 70,869
## Reflection on Mindful Engagements.docx: 15,537
## Süleymaniye Mosque.pptx: 5,395,400
# List of file names
file_names <- list.files(path = pathString, full.names = TRUE)
#system("pdftoppm -png -f 2 -l 2 '../../Desktop/PS3 New Folder/PDF 1.pdf' output")
#knitr::include_graphics("../../Desktop/PS3 New Folder/PDF 1.pdf")
# Loop over files
for (file_name in file_names) {
cat("-------------------------------------------------\n")
cat("<b>", file_name, "</b>", sep = "\n")
# Extract file extension
file_extension <- tools::file_ext(file_name)
# Display file name
if (file_extension %in% c("jpg", "png", "heic")) {
cat("===============================================\n")
cat("<b></b>\n", sep = "")
} else if (file_extension == "pdf") {
# PDF files
cat("<i>", file_name, "</i>", sep = "\n")
cat("<i>{width=500px}</i>\n", sep = "")
} else {
# other file types
cat("\"", basename(file_name), "\" cannot be displayed.\n\n")
}
}
| ../../desktop/PS3 New Folder/CheetahCubs.jpg
=============================================== |
|---|
| ../../desktop/PS3 New Folder/CheetahLooking.jpg
=============================================== |
../../desktop/PS3 New Folder/CheetahRunning.jpg
=============================================== ————————————————- ../../desktop/PS3
New Folder/Empty F1 ” Empty F1 ” cannot be displayed.
| ../../desktop/PS3 New Folder/Empty F2 ” Empty F2 ” cannot be displayed. |
|---|
| ../../desktop/PS3 New Folder/Empty F3 ” Empty F3 ” cannot be displayed. |
../../desktop/PS3 New Folder/excel.xlsx ” excel.xlsx ” cannot be displayed.
| ../../desktop/PS3 New Folder/PDF 1.pdf ../../desktop/PS3 New Folder/PDF 1.pdf |
|---|
| ../../desktop/PS3 New Folder/PDF 2.pdf ../../desktop/PS3 New Folder/PDF 2.pdf |
../../desktop/PS3 New Folder/PDF 3.pdf ../../desktop/PS3 New Folder/PDF 3.pdf ————————————————- ../../desktop/PS3 New Folder/Reflection on Mindful Engagements.docx ” Reflection on Mindful Engagements.docx ” cannot be displayed.
../../desktop/PS3 New Folder/Süleymaniye Mosque.pptx ” Süleymaniye Mosque.pptx ” cannot be displayed.
file_type_sentences <- ifelse(
tools::file_ext(stringRelative) %in% c("jpg", "png", "heic"),
paste0(basename(stringRelative), " is an image file"),
ifelse(
tools::file_ext(stringRelative) == "pdf",
paste0(basename(stringRelative), " is a pdf file"),
paste0(basename(stringRelative), " is something else")
)
)
cat(file_type_sentences, sep = "\n")
## CheetahCubs.jpg is an image file
## CheetahLooking.jpg is an image file
## CheetahRunning.jpg is an image file
## Empty F1 is something else
## Empty F2 is something else
## Empty F3 is something else
## excel.xlsx is something else
## PDF 1.pdf is a pdf file
## PDF 2.pdf is a pdf file
## PDF 3.pdf is a pdf file
## Reflection on Mindful Engagements.docx is something else
## Süleymaniye Mosque.pptx is something else
png_count <- sum(tools::file_ext(stringRelative) == "png")
# Print corresponding sentence
if (png_count == 0) {
cat("There are no png files\n")
} else if (png_count == 1) {
cat("There is a single png file\n")
} else {
cat("There are", png_count, "png files\n")
}
## There are no png files