Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find most recent file in a directory (in Windows System) in R [duplicate]

Tags:

r

I have few files in a directory(C:\MY_FOLDER\Freeze). Lets say i have two file with datetime infront of that. As shown below :

enter image description here

I have to read the most recent file in R. Help me please. I tried searching for the answers, but everywhere its in regarding with Linux system. "ctime" & "mtime" not working here.

like image 609
Shalini Baranwal Avatar asked Jun 15 '18 06:06

Shalini Baranwal


People also ask

How do I find the last modified file?

File Explorer has a convenient way to search recently modified files built right into the “Search” tab on the Ribbon. Switch to the “Search” tab, click the “Date Modified” button, and then select a range. If you don't see the “Search” tab, click once in the search box and it should appear.

Which command is used to display the 10 newest files in the current directory?

List files, displaying the most recently created or changed files first, by using the ls -t command.


1 Answers

We can use file.info with list.files. list.files would list all the files in the interested directory and file.info would give the details of all those files. We then get most recently modified file using which.max on mtime and then get the corresponding path of that file.

df <- file.info(list.files("/path/to/your/directory", full.names = T))
rownames(df)[which.max(df$mtime)]

#[1] "/path/to/your/directory/Interested_file.xlsx"

You can then use any of the commands to read csv or excel from that path.

like image 185
Ronak Shah Avatar answered Sep 30 '22 09:09

Ronak Shah