Solved

Question For Nietod - Need  a program to transfer files

Posted on 2000-03-08
2
243 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
Comment Utility
thanks.
0
 

Author Comment

by:John500
Comment Utility
Likewise...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now