Help!!!! Copy file to another folder

Im using the SD library and i need help knowing how to be able to copy one file to another folder!!

The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. "directory/filename.txt". Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. "/file.txt" is equivalent to "file.txt").

Open original and new file, copy whole content to the new file, close both files.

Except the library can’t open two files simultaneously.

It’s a very simple library, at least it was the last time I looked. It doesn’t have copy or move functions.

MorganS:
Except the library can’t open two files simultaneously.

I disagree.

#include <SPI.h>
#include <SD.h>

File myFileIn;
File myFileOut;

void setup() {
  Serial.begin(115200);
  if (!SD.begin(4)) {
    Serial.println(F("initialization failed!"));
  }
  if (!SD.exists("fromFile.txt")) {
    myFileIn = SD.open("fromFile.txt", FILE_WRITE);
    myFileIn.println(F("one two three"));
    myFileIn.println(F("four five six"));
    myFileIn.close();
  }
  if (SD.exists("toFile.txt")) {
    SD.remove("toFile.txt");
  }
  myFileIn = SD.open("fromFile.txt", FILE_READ);
  myFileOut = SD.open("toFile.txt", FILE_WRITE);
  while (myFileIn.available()) {
    myFileOut.write(myFileIn.read());
  }
  myFileIn.close();
  myFileOut.close();
  myFileIn = SD.open("toFile.txt", FILE_READ);
  while (myFileIn.available()) {
    Serial.write(myFileIn.read());
  }
  myFileIn.close();
}

void loop() {}
one two three
four five six
1 Like

This version creates the copy in a different folder, as requested

#include <SPI.h>
#include <SD.h>

File myFileIn;
File myFileOut;

void setup() {
  Serial.begin(115200);
  if (!SD.begin(4)) {
    Serial.println(F("initialization failed!"));
  }
  if (!SD.exists("fromFile.txt")) {
    myFileIn = SD.open("fromFile.txt", FILE_WRITE);
    myFileIn.println(F("one two three"));
    myFileIn.println(F("four five six"));
    myFileIn.close();
  }
  if (!SD.exists("sub")) {
    SD.mkdir("sub");
  }
  if (SD.exists("sub/toFile.txt")) {
    SD.remove("sub/toFile.txt");
  }
  myFileIn = SD.open("fromFile.txt", FILE_READ);
  myFileOut = SD.open("sub/toFile.txt", FILE_WRITE);
  while (myFileIn.available()) {
    myFileOut.write(myFileIn.read());
  }
  myFileIn.close();
  myFileOut.close();
  myFileIn = SD.open("sub/toFile.txt", FILE_READ);
  while (myFileIn.available()) {
    Serial.write(myFileIn.read());
  }
  myFileIn.close();
}

void loop() {}
one two three
four five six

What if I wanted to do a batch copy at once, say from a folder to root dir, how do I go about it?

Or better still, can someone please help tweak the sd.h library to be able to copy and paste files?

Thanks in advance

stickycub:
What if I wanted to do a batch copy at once, say from a folder to root dir, how do I go about it?

Or better still, can someone please help tweak the sd.h library to be able to copy and paste files?

Thanks in advance

build your UI, include this function. all done.

Chuck.

chucktodd:
build your UI, include this function. all done.

Chuck.

Yea, I don't even know what a UI is talk more of including functions. I'll be grateful if you can walk me through it. The library is sdfat.h

stickycub:
Yea, I don't even know what a UI is talk more of including functions. I'll be grateful if you can walk me through it. The library is sdfat.h

UI (user Interface), based on your request for Drag and Drop, I would assume you have a graphic display with mouse pointer support? If you have been able to squeeze that much functionality into an Arduino, why is a simple loop causing you so much grief?

Take @Whandall's code:

  • Wrap it in a function()
  • Iterate through your source directory one file at a time.
  • pass each source file to the function, include the destination directory

You have to actually write code.

If you want to hire me to write your code, open your wallet and send me $100.

Chuck.

P.S. a helping hand, is not a the same as a hand out.