Using DS18B20 temperature sensor and capacitive soil moisture sensor with SD card and RTC on ESP8266 NodeMCU

I am trying to use DS18B20 temperature sensor and capacitive soil moisture sensor with SD card and RTC in ESP8266 NodeMCU. Before i was using the SD card reader with the RTC and a DHT 11, and i would like help to change the code so i can use the DS18B20 and a capacitive soil moisture sensor at the same time, saving the collected data in the SD card with the date and time that data was collected. I was using the following code with DHT 11.

#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "RTClib.h"
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 rtc;

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("File written");
    } else {
        Serial.println("Write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("Failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("Message appended");
    } else {
        Serial.println("Append failed");
    }
    file.close();
}

void setup(){
  Serial.begin(9600);
  dht.begin();
  
  if(!SD.begin()){
      Serial.println("Card Mount Failed");
      return;
      }

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
    }

  if (!rtc.isrunning()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  
  writeFile(SD, "/datalog_ESP32.txt", "Time, Humidity, Temperature \r\n");
}

void loop(){

  String dataString = "";

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  DateTime now = rtc.now();

  dataString += String(now.unixtime());
  dataString += ",";
  dataString += String(h);
  dataString += ",";
  dataString += String(t);
  dataString += "\r\n";

  Serial.println(dataString);
  appendFile(SD, "/datalog_ESP32.txt", dataString.c_str());
    
  delay(2000);
}

With this code, the saved data did not display the date and time correctly. If someone could help me to make the temperature sensor and the soil moisture work at the same time and to save the data on the SD card with date and time, I would be grateful.

I think your use of code tags </> failed - see if can fix it to make the code readable
in the case of the the temperature and the soil moisture sensors I would ininially implement seperate programs to check connections and get them operational
when working copy the code into to your program and test

I fixed the code for readability, and I have the DS18B20 code working, but I couldn't save the data to the SDcard with the time and date and I couldn't get it to work together with the humidity sensor.

I have never seen code for SD like that before but you are calling the standard SD library, so I'm betting it is bogus and the fact that the library has append by default at least proves that what you are doing is dumb. The data logging example included in the IDE should have all you need.