Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void map_drive(char *ip, char *ipname);
void copyfile(char *ip, char *ipname);
const char drivename[]="i:";
int main(void)
{
char ch; char line[64];
char buf[BUFSIZ] = {'\0'};
char ip[15];
char ipname[64];
FILE *fpIP;
fpIP = fopen ("ip_adress_list.txt","rt");
if (fpIP!=NULL)
{
while ( fgets(line, sizeof line, fpIP) != NULL && sscanf(line, "%15[^,], %31s", ip, ipname) == 2 )
{
map_drive(ip, ipname);
}
/*
// uzmi jedan red iz adress lista
while (fgets(buf, BUFSIZ, fpIP) != NULL)
{
// formatiraj red iz adress lista ip adresa i ip name
formatIP(buf, ip, ipname);
map_drive(ip, ipname);
} */
}
else
{
perror("fopen");
}
system("PAUSE");
return 0;
}
void map_drive(char *ip, char *ipname)
{
char map_dr[50] = {'\0'};
const char drive_path[]="e$";
char disc_map_dr[30] = {'\0'};
char ch;
FILE *fpMD;
fpMD = fopen("map_drive.bat", "wt");
strcpy(map_dr, "net use ");
strcat(map_dr, drivename);
strcat(map_dr, " \\\\");
strcat(map_dr, ip);
strcat(map_dr, "\\");
strcat(map_dr, drive_path);
fprintf(fpMD, "%s\nEXIT", map_dr);
fclose(fpMD);
system("map_drive.bat");
copyfile(ip, ipname);
//DISCONNECT MAP DRIVE
strcpy(disc_map_dr, "net use ");
strcat(disc_map_dr, drivename);
strcat(disc_map_dr, " /delete");
system(disc_map_dr);
}
void copyfile(char *ip, char *ipname)
{
char command[1024];
char take_from[] = "files";
char copy_to[] ="razno\retpos_update";
char log_dir[] = "logs\\";
strcpy(command, "robocopy ");
strcat(command, take_from);
strcat(command, "\\ ");
strcat(command, drivename);
strcat(command, "\\");
strcat(command, copy_to);
strcat(command, "\\ /COPYALL /E /ZB /LOG:");
strcat(command, log_dir);
strcat(command, ipname);
strcat(command, ".txt");
system(command);
}