Skip to content

bat file doesn't run with taskscheduler_create() #109

@FelipeCarrillo

Description

@FelipeCarrillo

R scripts run fine but it does not run the bat file

`Save this as run_my_r_script.bat in a folder (e.g., C:\R_Tasks):

@echo off
REM Change to the folder where Rscript.exe is located
REM Adjust the path to your R installation
set R_PATH="C:\Program Files\R\R-4.3.3\bin\Rscript.exe"

REM Path to your R script
set R_SCRIPT="C:\R_Tasks\my_script.R"

REM Run the R script
%R_PATH% %R_SCRIPT%

Save this as my_script.R in the same folder:

# my_script.R
# Example R script that logs the current time

log_file <- "C:/R_Tasks/log.txt"

tryCatch({
  cat(sprintf("Script ran at: %s\n", Sys.time()), file = log_file, append = TRUE)
}, error = function(e) {
  cat(sprintf("Error at %s: %s\n", Sys.time(), e$message), file = log_file, append = TRUE)
})

Run this in R once to create the scheduled task:

# Install if not already installed
if (!require(taskscheduleR)) install.packages("taskscheduleR")

library(taskscheduleR)

# Path to the .bat file
bat_path <- "C:/R_Tasks/run_my_r_script.bat"

# Create a daily task at 9:00 AM
taskscheduler_create(
  taskname   = "Run_My_R_Script",
  rscript    = bat_path,
  schedule   = "DAILY",
  starttime  = "09:00",  #To run it, set the time a couple minutes ahead of your current time
  startdate  = format(Sys.Date(), "%m/%d/%Y"))

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions