#CREATE, DELETE, MOVE, AND MORE WITH FILES
#How to get and change the current working directory
# set working directory
setwd("C:\\Users\\liyix\\OneDrive\\Desktop\\data_1")
# get current working directory
getwd()
## [1] "C:/Users/liyix/OneDrive/Desktop/data_1"
#Creating Files and Directories
dir.create("new_folder")
#creating a blank file can be done with file.create.
file.create("new_text_file.txt")
## [1] TRUE
file.create("new_word_file.docx")
## [1] TRUE
file.create("new_csv_file.csv")
## [1] TRUE
sapply(paste0("file", 1:3, ".txt"), file.create)
## file1.txt file2.txt file3.txt
## TRUE TRUE TRUE
#Copying a file / folder
file.copy("file1.txt", "new_folder")
## [1] TRUE
#How to list all the files in a directory
# list all files in current directory
list.files()
## [1] "file1.txt" "file2.txt" "file3.txt"
## [4] "new_csv_file.csv" "new_folder" "new_text_file.txt"
## [7] "new_word_file.docx"
list.files(recursive = TRUE)
## [1] "file1.txt" "file2.txt" "file3.txt"
## [4] "new_csv_file.csv" "new_folder/file1.txt" "new_text_file.txt"
## [7] "new_word_file.docx"
list.files(full.names = TRUE, recursive = TRUE)
## [1] "./file1.txt" "./file2.txt" "./file3.txt"
## [4] "./new_csv_file.csv" "./new_folder/file1.txt" "./new_text_file.txt"
## [7] "./new_word_file.docx"
#################################
# list all CSV files non-recursively
list.files(pattern = ".csv")
## [1] "new_csv_file.csv"
# read in all the CSV files
#all_data_frames <- lapply(list.files(pattern = ".csv"), read.csv)
#all_data_frames
# stack all data frames together
#single_data_frame <- Reduce(rbind, all_data_frames)
#How to get created / modified times and other details about files
snapshot <- fileSnapshot()
snapshot$info
## size isdir mode mtime ctime
## file1.txt 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## file2.txt 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## file3.txt 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## new_csv_file.csv 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## new_folder 0 TRUE 777 2022-01-08 22:35:21 2022-01-08 22:35:21
## new_text_file.txt 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## new_word_file.docx 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:20:09
## atime exe
## file1.txt 2022-01-08 22:35:21 no
## file2.txt 2022-01-08 22:35:21 no
## file3.txt 2022-01-08 22:35:21 no
## new_csv_file.csv 2022-01-08 22:35:21 no
## new_folder 2022-01-08 22:35:21 no
## new_text_file.txt 2022-01-08 22:35:21 no
## new_word_file.docx 2022-01-08 22:35:21 no
file.info("new_csv_file.csv")
## size isdir mode mtime ctime
## new_csv_file.csv 0 FALSE 666 2022-01-08 22:35:21 2022-01-08 22:35:21
## atime exe
## new_csv_file.csv 2022-01-08 22:35:21 no
file.mtime("new_csv_file.csv")
## [1] "2022-01-08 22:35:21 EST"
# delete a file
unlink("new_csv_file.csv")
# delete another file
file.remove("new_text_file.txt")
## [1] TRUE
sapply(paste0("file", 1:3, ".txt"), unlink)
## file1.txt file2.txt file3.txt
## 0 0 0
#How to check if a file or directory exists
# check if a folder exists
file.exists("file2.txt")
## [1] FALSE
# alternatively, check if a folder exists with dir.exists
dir.exists("new_folder")
## [1] TRUE
#How to get the base name of a file
basename("file2.txt")
## [1] "file2.txt"
dirname("file1.txt")
## [1] "."
#How to physically open a file
# use shell.exec...
#shell.exec("file2.txt")
# or file.show to launch a file
#file.show("file2.txt")
#How to open a file selection window
#file.choose()
#ref http://theautomatic.net/2018/07/11/manipulate-files-r/