Close Navigation
Learn more about IBKR accounts
How to Change a File’s Last Modified Date with R

How to Change a File’s Last Modified Date with R

Posted June 6, 2023
Andrew Treadway
TheAutomatic.net

This relatively quick post goes through how to change a file’s last modified date with base R.

How to change a file’s modified time with R

Let’s say we have a file, test.txt. What if we want to change the last modified date of the file (let’s suppose the file’s not that important)? Let’s say, for instance, we want to make a file have a last modified date back in the 1980’s. We can do that with one line of code.

First, let’s use file.info to check the current modified date of some file called test.txt.

file.info("test.txt")

We can see above by looking at mtime that this file was last modified December 4th, 2018.

Now, we can use a function called Sys.setFileTime to change the modified date to any date including or after January 1, 1970.

Sys.setFileTime("test.txt", "1980-12-04")

The base R function above takes the file name as the first parameter, and the date for the second parameter. If we use file.info on this file, we can see the newly changed last modified date – set back in 1980!

If we want to change the modified dates for all the files in a directory, we could do this:

sapply(list.files("/path/to/some/directory", full.names = TRUE), 
                  function(FILE) Sys.setFileTime(FILE, "1975-01-01"))

The above code will change the last modified time of every file in the directory specified to be January 1, 1975.

You can also, to an extent, make the last modified time some date in the future (up to 2038 as of this writing).

sapply(list.files("/path/to/some/directory", full.names = TRUE), 
                  function(FILE) Sys.setFileTime(FILE, "2038-01-18"))

Originally posted on TheAutomatic.net blog.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.