0

this is my code:

uploadOD <- function(filePath, destFold) {
  # Connect to the user's OneDrive for Business
  odb <- get_business_onedrive()
  
  # Get the base name of the destination folder
  destFolderName <- basename(destFold)
  
  # Get the base name of the file
  fileName <- basename(filePath)
  
  # Check if the destination folder already exists in the OneDrive
  tempList <- odb$list_items()
  folderExists <- destFolderName %in% tempList
  
  # Get the current date and time
  currentDate <- format(Sys.Date(), "%Y%m%d")
  currentTime <- format(Sys.time(), "%H%M%S")
  
  # If the folder exists, upload the file and rename it
  if (folderExists) {
    # Get the current folder object
    currentFolder <- odb$get_item(destFolderName)
    
    # Upload the file with its original name
    currentFolder$upload(filePath)
    
    # Create the new file name with the current date and time
    newFileName <- paste0(gsub("\\.[^.]*$", "", fileName), "_", currentDate, currentTime, ".", tools::file_ext(fileName))
    
    # Retrieve the file as a drive item
    renamedFile <- currentFolder$get_item(fileName)
    
    # Rename the file using the update() method
    renamedFile$update(name = newFileName)
  } else {
    # If the folder doesn't exist, create it and upload the file
    odb$create_folder(destFolderName)
    
    # Get the current folder object
    currentFolder <- odb$get_item(destFolderName)
    
    # Upload the file with its original name
    currentFolder$upload(filePath)
    
    # Create the new file name with the current date and time
    newFileName <- paste0(gsub("\\.[^.]*$", "", fileName), "_", currentDate, currentTime, ".", tools::file_ext(fileName))
    
    # Retrieve the file as a drive item
    renamedFile <- currentFolder$get_item(fileName)
    
    # Rename the file using the update() method
    renamedFile$update(name = newFileName)
  }

  # Return the new file path
  newFilePath <- paste0(destFold, "/", newFileName)
  newFilePath
}

I keep getting :

Error in process_response(res, match.arg(http_status_handler), simplify) : Conflict (HTTP 409). Failed to complete operation. Message: The specified item name already exists. Called from: handler(response, paste0("complete operation. Message:\n", sub("\.$", "", error_message(cont))))

This error only works if the folder im trying to move the file into in my onedrive, already exists!

I tried moving it into a new folder that works, but I need it to work for an already existing folder.

0 Answers0