Simple File Copying

I've been trying to figure out a way to simple copy all the files in the current working directory to another folder. I've done this before using system("XCOPY... but wanted to get away from using those old command line commands. I tried using filesystem and copyfile but nothing seems to not error out in Visual Studio.

The basis behind this is to quickly copy a group of icons to a folder and overwrite any that are there while doing other things. This way my engineers don't have to open file explorer and select all the icons and copy and paste them. Trying to make things fast and efficient.

I apologize that I don't have any code as of yet. As I said I was trying but nothing has been working. So I am really asking for some ideas or places to start or sites to visit as I have searched and search and I can't seem to find anything simple.

Thanks,
V
there is absolutely nothing wrong with using xcopy or move or other command line tools to do this. Xcopy does multiple files at a time and is pretty efficient, and microsoft keeps it up to date.

that said filesystem should be the way to do it, and you said your attempts did not work.. post what you have?

maybe some of these tips will help too; it looks like unix vs windows has some details:
https://docs.microsoft.com/en-us/cpp/standard-library/filesystem?view=msvc-160

Last edited on
> I tried using filesystem and copyfile

std::filesystem::copy_file copies a single file.
To copy directories, use std::filesystem::copy https://en.cppreference.com/w/cpp/filesystem/copy
(with the option std::filesystem::copy_options::recursive to also copy subdirectories recursively).

There is an example on the cppreference page: https://en.cppreference.com/w/cpp/filesystem/copy#Example
So I did get filesystem to work. It just copies all the files from the directory so I am having to remove the exe after. Anyone have an idea of how to exclude?

I was under the impression that using system() commands were bad. Is that not the case? I'm still new to this so any info that helps me learn is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <filesystem>
#include <time.h>
#include <Windows.h>
#include <ConsoleColor.h>
using namespace std;
namespace fs = std::filesystem;

int main()
{
	
	const string dir1 = "C:\\synergyii\\image\\icons";

	fs::copy(fs::current_path(), dir1, fs::copy_options::overwrite_existing);

	remove("C:\\synergyii\\image\\icons\\Red PTZ Icons Deployment.exe");



    int counter = 9; //amount of seconds
    Sleep(1000);
    while (counter >= 1)
    {

        cout << lightblue << "\r\tWindow will close in: " << counter;
        cout << flush;
        Sleep(1000);
        counter--;
    }


	return 0;
	
}


Some may notice the little countdown timer at the end. I found that on a site and it's pretty cool. The only issue I have with it is that if it is set higher than 9 the trailing 0 stays behind all the single digit numbers until the end. If anyone sees something wrong with that please let me know.

Thanks,
V
The only way I see to do it is the unattractive directory iterator which can touch each file and you can look at the wildcard yourself -- if it has the right extension, copy it, if not, don't ?

https://en.cppreference.com/w/cpp/filesystem/directory_iterator

this begs multi-threading though because now what? Copy one file at a time again, the slow way? You would want to spawn a copy thread for each acceptable file name, I think, but that could flood you, maybe want to put a reference counter and keep it to 10 or so at a time?

you can emit backspace characters to the console to clear the extra character it or look at nonstandard functions like gotoxy or ncurses tools to make a cleaner countdown.

here is the count down. I will leave it to you to append seconds and erase it if you want to go there. It probably needs to have the # of spaces written match the log10(x) idea or it will append an extra space for each power of 10 as-is. /lazy me. It works for 100 etc too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main()
{
	int x{10};	
        cout << "this program ends in:";
	do
	{
          cout <<" "<< x--;      
	  cout.flush();
	  this_thread::sleep_for(chrono::seconds(1));
	  for(int i = 0; i < log10(x)+1; i++)
	  cout << "\b";
	} while(x);	
}

if you want extra credit you can use ms and convert it to a float to show the fractions of a second going by.
Last edited on
Registered users can post here. Sign in or register to post.