Solved

Question For Nietod - Need  a program to transfer files

Posted on 2000-03-08
2
251 Views
Last Modified: 2010-04-02
For the purpose of updating a remote system, I need a simple program which will delete the contents of certain directories and then transfer new files to the same directories.


Answer by nietod:

FindFirstFile() & FindNextFile() may be used to scroll through a directory to delete specific files or all files.  MoveFile() may be used to transfer the new files.


#include <windows.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std ;


/******************************   Write To Log  ******************************
*
* desc    : This procedure is used to pass all error messages to the designated
*    error log.  An output stream object is created and opened for the
*           designated file name.  If the file is unable to be opened, a message
*    box informs the user of the problem.  Otherwise the message is
*    written to the log and the file is closed.
*            
* input   : One string parameter is taken.  The calling function must first convert
*    any formatted text into a string before calling this procedure.
*            
* output  : If the file fails to be opened, a message box alerts the user.  This
*    message must be acknowledged by clicking the 'OK' button.
*            
* returns : -
*/


void WriteToLog(string Message)
{

string UtilityLog  = "D:\\Logs\\UtilityLog.txt";
ofstream WriteFile;

WriteFile.open(UtilityLog.c_str(), ios::app);

if(WriteFile.fail()){
MessageBox(NULL,"Error writing to UtilityLog!  Contact Support.",
   "UtilityInfo", MB_OK | MB_ICONERROR);
}

else
WriteFile << Message;
WriteFile.close();

return;
}

/*************************** Process Directory **************************
*
* desc    : This procedure scrolls through the specified directory deleting
* each file. FindFirstFile() and FindNextFile() are used to traverse
* the directory.
* input   : The directory path of the directory needing processed
* output  : -
* returns : -
*/

void ProcessDirectory(string DirPath)
{

BOOL  Done;           // used for searching the directory for files
HANDLE  FndHnd;          // used for FindFirstFile()
WIN32_FIND_DATA FindDat; // allocate a local structure
int FileCount = 0; // used to record number of files deleted
string LogMessage; // holds the converted message to be written in the log
char datebuf[9]; // stores the date obtained from _strdate()
char timebuf[9]; // stores the time obtained from _strtime()
char MessageBuffer[40]; // holds the message which is converted into a string
int rc; // holds the return value of DeleteFile()
int i; // used as an index for 'MessageBuffer' array

string Fname;
string FilePath;
string FullPath = DirPath;
FullPath = FullPath + "*.*";

FndHnd = FindFirstFile(FullPath.c_str(), &FindDat);
Done = (FndHnd == INVALID_HANDLE_VALUE);

while (!Done){
//  check directory attribute bit of WIN32_FIND_DATA structure
//  if not set, delete the file shown in the WIN32_FIND_DATA struct
if ((FndDat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0){
Fname = FindDat.cFileName;
FilePath = DirPath + Fname;
rc = DeleteFile(FilePath.c_str());

if(rc == 0){
MessageBox(NULL,"Utility could not delete file!  Contact Support.",
"UtilityInfo", MB_OK | MB_ICONERROR);
} // end of 2nd if
}// end of 1st if
else
FileCount++;
Done = !FindNextFile(FndHnd, &FindDat);

}
if (FndHnd)
FindClose(FndHnd);

string CurDate = _strdate(datebuf); // get current date
string CurTime = _strtime(timebuf); // get current time

if (FileCount > 0){
i =  sprintf(MessageBuffer, "Total files deleted = %d", FileCount);
i += sprintf(MessageBuffer + i,"  (%s %s)\n", CurDate.c_str(), CurTime.c_str());
LogMessage = MessageBuffer;
//WriteToLog(LogMessage);
}
return;

}

/*************************** Update Directory **************************
*
* desc    : This procedure moves the specified file to the specified
* directory.
* input   : The first parameter indicates the parent directory of the file
* being moved. The second parameter is the name of the file being moved.
* The third parameter is the destination file path.
* output  : If the file is moved, the transaction can be written to a log.
* Otherwise, a message box will alert the user if the file could
* not be moved.
* returns : Returns 1 for success, otherwise 0
*/

int UpdateDirectory(string SourcePath, string SourceFile, string DestPath)
{
string LogMessage; // holds the converted message to be written in the log
string FailMessage; // used as parameter in message box when MoveFile() fails
DWORD error_rc; // holds the error code returned from GetLastError()
char datebuf[9];    // stores the date obtained from _strdate()
char timebuf[9];    // stores the time obtained from _strtime()
char MessageBuffer[100];// holds the message which is converted into a string
int FileCount = 0; // used to record number of files deleted
int rc;    // holds the return value of DeleteFile()
int i; // used as an index for 'MessageBuffer' array


// Create source and destination paths including the file name
DestPath = DestPath   + SourceFile;
SourcePath = SourcePath + SourceFile;

rc = MoveFile(SourcePath.c_str(), DestPath.c_str());
error_rc = GetLastError();

string CurDate = _strdate(datebuf); // get current date
string CurTime = _strtime(timebuf); // get current time
FailMessage = "Could not move file: " + SourcePath;

// if success moving file
if (rc != 0){
i =  sprintf(MessageBuffer, "File %s moved successfully\n", SourcePath);
i += sprintf(MessageBuffer + i,"  (%s %s)\n", CurDate.c_str(), CurTime.c_str());
LogMessage = MessageBuffer;
//WriteToLog(LogMessage);
return 1;
}
// if failure moving file
else{
MessageBox(NULL, FailMessage.c_str(),"File Transfer Utility", MB_OK | MB_ICONERROR);
return 0;
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

DWORD NameLength = 40; // specifies the character length of the buffer
string FilePath; // assigned the value returned from GetModeuleFileName()
char CurPath[40]; // holds the message which is converted into a string
char datebuf[9]; // stores the date obtained from _strdate()
char timebuf[9]; // stores the time obtained from _strtime()
int PathLength; // stores return value of length()

// Obtain file path of current program
GetModuleFileName(NULL, CurPath, NameLength);
FilePath = CurPath;
PathLength = FilePath.length();
// Since length of 'test.exe' is equal to 8, subtract eight to get path
FilePath = FilePath.substr(0, (PathLength - 8));

//Source paths for deleting
string dir_1 = "C:\\test\\dir_1\\";
string dir_2 = "C:\\test\\dir_2\\";
string dir_3 = "C:\\test\\dir_3\\";
string dir_4 = "C:\\test\\dir_4\\";
string dir_5 = "C:\\test\\dir_5\\";

//Source files to be moved
string update_file_1 = "VB1.exe";
string update_file_2 = "VB2.exe";
string update_file_3 = "VB3.exe";
string update_file_4 = "VB4.exe";
string update_file_5 = "VB5.exe";

// Process the following directories by deleting all files
ProcessDirectory(dir_1);
ProcessDirectory(dir_2);
ProcessDirectory(dir_3);
ProcessDirectory(dir_4);
ProcessDirectory(dir_5);

// Update the following directories by moving the named files
UpdateDirectory(FilePath, update_file_1, dir_1);

      if (rc == 1)
rc = UpdateDirectory(FilePath, update_file_2, dir_2);

if (rc == 1)
rc = UpdateDirectory(FilePath, update_file_3, dir_3);

if (rc == 1)
rc = UpdateDirectory(FilePath, update_file_4, dir_4);

if (rc == 1)
rc = UpdateDirectory(FilePath, update_file_5), dir_5);

if (rc == 1)
MessageBox(NULL, "File transfer was successful!", "File Transfer Utility",
MB_OK | MB_ICONINFORMATION);

return 1;
}
0
Comment
Question by:John500
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2596477
thanks.
0
 

Author Comment

by:John500
ID: 2596482
Likewise...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 42
C Language combined operators 28 114
Should CArray be used for a list of pointers in C++? 19 115
VS2015 Redefinition errors 4 67
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question