Set Time & Date of File & Directory in R (Example) | Sys.setFileTime Function

 

In this tutorial you’ll learn how to change the time of a file using the Sys.setFileTime function in R.

Table of contents:

Let’s jump right to the example.

 

Example File

Consider the directory and TXT file shown in the figure below:

 

timestamp

 

As you can see, I have stored a working directory called “my directory” on the desktop of my computer, which contains a TXT file called “my file.txt”.

This file has the time 12.10.2021 08:20 as a timestamp.

 

Example: Set File Time & Date Using Sys.setFileTime() Function

The following R syntax explains how to modify the file time (i.e. the timestamp) of a file.

For this task, we can use the Sys.setFileTime function as shown below. Within the Sys.setFileTime function, we have to specify the file path of the file we want to change, as well as the time we want to assign to this file formatted as a POSIXlt time object.

Sys.setFileTime("C:/Users/Joach/Desktop/my directory/my file.txt", # Apply Sys.setFileTime()
                as.POSIXlt("2021-10-01 07:10:30"))

After running the previous syntax, the file time of our file is updated. Have a look at the figure below:

 

timestamp

 

As you can see, we have changed the file time and date to 01.10.2021 07:10.

Note: In this example, we have changed the time and date of a TXT file. However, we could apply the same R code to other file types such as CSV, XLSX, DOCX, and so on. Furthermore, we might use the Sys.setFileTime function to change the time and date of a working directory.

 

Video & Further Resources

Would you like to learn more about the usage of the Sys.setFileTime function? Then I recommend watching the following video on my YouTube channel. In the video tutorial, I’m explaining the topics of this tutorial:

 

The YouTube video will be added soon.

 

In addition, you could read the related tutorials on my website. A selection of articles can be found below:

 

In summary: At this point you should have learned how to apply the Sys.setFileTime function in the R programming language. If you have further questions, don’t hesitate to tell me about it in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top