GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-May-2006, 10:07
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 101
dlp will become famous soon enough

C++ for moving files across network?


Hi. First post here, I wanted to run an idea by you guys and ask if you think C++ is a good way to do it or not. I am a fairly novice C++ programmer, but I learn quickly. I do not have on hand a C++ book of any kind, nor do I have much professional coding experience. I am still in college but on a coop job in an IT department where I have been asked to make this program.
My tools are Windows XP, Microsoft Visual Studio .NET 2003 (or whatever free compiler I can get on the internet I guess).
The machines this is being done on: All Dell, Intel based machines, all fairly recent. The oldest are early Pentium 4 systems. All OS's are Windows 2000 or XP.

The main purpose of this program is to:
Ask the user for their name and ID # for the purpose of storing their files in a unique folder based on their name
Find a specified folder on their C:\ directory (first level on C:\), and if there both possible folder names, to find the one with the latest date
To mount a shared folder on another computer on our network
See if they already have a folder based on their name on the shared folder, and if it is already there, delete it, then copy the whole folder found on the C:\ into a folder based on their name and ID #
Automatically unmount the drive so that the user has no access to it outside while this program is running.
I'd like minimal cryptic errors from DOS commands. When I run system() calls, it displays the output from these commands like you'd usually see in DOS, and I'd like to suppress that if possible. I'm also trying to keep as much out of the user's hands as I can. All they should see on their screen after they input their name: Moving files...files moved, thank you come again.

I've already got a working code to do absolutely everything I need. The question is can I improve this code in terms of style, using safer functions for sake of being compliant or to avoid things like memory leaks.
I'm going to post sections of my code because it currently is 349 lines uncommented (will add comments for your understanding).

How I did the program:
The naming thing is simple, I just cout a request for the name or ID, and use getline( cin, string, '\n' ) to grab the whole line in case their first or last name has a space in it (Mary Anne, etc.)

Next is where I really have my issues on if I can improve my current code or should try using something else. I use a lot of system calls from here on out. See this section of code:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main()
{
    system( "dir C:\\ /ad /b /l > directory.txt" );
    // directs list of folder names (no path to), all in lower case, into a file called directory.txt in executable's path
    // example (note that almost all these folders contain capital letters originally):
    /*
    dell
    documents
    documents and settings
    program files
    windows
    */

    // opens file
    ifstream fin("directory.txt");

    // using vectors with no max number so that i don't need to worry taking in too many vectors
    vector<string> folders 

    // temporary string for each line in file;
    string temp;

    for( ; ; )
    {
        getline( fin, temp, '\n' );
        if( fin.eof() ) break;
        folders.push_back( temp );
    }
    fin.close();
    fin.clear();  // something i saw to ensure everything's clear but i feel might be unnecessary
    system( "del directory.txt" ); // clean up unnecessary txt files left behind

    int i, j;
    int count = 0;
    string folName[2], hits[2];

    folName[0] = "my mail";
    folName[1] = "outlook";

    // for loop that runs through list of folders in folders vector
    for( i=0; i < folders.size(); i++ )
    {
        // for loop that is used to check call folname 0 or 1
        for( j=0; j<2; j++ )
        {
            // checks if folder currently being read from list of folders is the same as folname 0 or 1
            if( folders[i] == folName[j] )
            {
                hits[count] = folName[j];  // stores the found folname in hits so that i can call the correct folder name later
                count++;  // number of folders found so that if both are found, i know by value of count
            }
        }
    }

This code above just tells me if there's either or folder above. I use the value of counts to:end the program when there is neither folder where they should be located; move the proper folder; figure out which folder has the latest modified date and move only that one.
Things I'd like you guys to comment on: Is there an easier way to do that nested for loop with if statement?
For the system call I have: system( "dir C:\\ /ad /b /l > directory.txt" ); Is there any other ways of doing that? Is there any way to search only in C:\ for folders besides the way I've done it with the DOS "dir" command?

The one I'm not sure I like at all is a function I made that moves the folder over.

CPP / C++ / C Code:
// call like this:
// makeAndMove( hits[0], folderName );
// hits[0] is folder found to be the one to move as determined by code above
// folderName is string based on user input of name and company ID #
void makeAndMove( string hit, string folder )
{
    string dirCmd, copyCmd, mount, unmount, temp;

    //string that mounts network folder
    mount = "net use m: \"\\\\localcomputer\\sharedfolder\" " ;

    // system call that mounts folder
    system( mount.c_str() ); 
 
    // system call that lists folders on shared folder, stores them in mailfolders.txt
    system( "dir \"M:\\\" /ad /b > mailfolders.txt" );

    ifstream fin("mailfolders.txt");

    for( ; ; ) // for loop for readin in folder names on shared folder
    {
        getline(fin, temp, '\n');
        if( fin.eof() ) break;

        // if currently read folder name equals username based folder name
        if( temp == folder )
        {
            // string that contains command to delete existing folder
            string del = "rd /S /Q \"M:" + folder + "\"";
            // deletes existing folder
            system( del.c_str() );
        }
    }

    fin.close();
    fin.clear();

    system( "del mailfolders.txt" ); // deletes unnecessary txt file

     // makes new folder based on user name and ID
    dirCmd = "md \"M:\\" + folder + "\\" + hit;
    system( dirCmd.c_str() );

    // copies folder on C: into new folder on shared computer folder
    copyCmd = "copy \"C:\\" + hit + "\" \"M:\\" + folder + "\\" + hit + "\"";
    system( copyCmd.c_str() );

    // unmounts shared folder
    unmount = "net use m: /delete";
    system( unmount.c_str() );
    }

Here are a boat load of system() calls. Honestly, they make me nervous. I read why using system("pause") is a bad idea on the gidnetworks site, so I don't know if I should try avoiding such calls for what I'm doing. Also, are there any risks in moving files that might be as big as 2GB with this command?

Are there smarter ways of doing this? Do I even want to be doing this in C++ or am I better off doing it some other way?

Tear me apart, let me know. Thanks in advance!

-Dave
Last edited by LuciWiz : 25-May-2006 at 14:44. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 25-May-2006, 12:22
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: C++ for moving files across network?


Quote:
Originally Posted by dlp
Hi. First post here, I wanted to run an idea by you guys and ask if you think C++ is a good way to do it or not.
Of course it's a good way!

Quote:
Originally Posted by dlp
I've already got a working code to do absolutely everything I need. The question is can I improve this code in terms of style, using safer functions for sake of being compliant or to avoid things like memory leaks.
Yes, there are things you can do.

First, for memory leaks as long as you are not allocating dynamic memory you should be fine.

As for system() calls, get rid of them. There are functions in Standard C++ that can handle some of the system calls directly, like deleting files (remove()).


Quote:
Originally Posted by dlp
For the system call I have: system( "dir C:\\ /ad /b /l > directory.txt" ); Is there any other ways of doing that? Is there any way to search only in C:\ for folders besides the way I've done it with the DOS "dir" command?
Yes... You are now entering the realm of non-standard C++ but that's OK in this case. Look thru the compiler's functions for a couple names something like findfirst() and findnext(). These are functions that open a directory and start returning the names of each file (one at a time) in a directory. Do your research here. These calls also return directory names too.

Same with mounting, although I believe you will be looking at the Windows API's -- another type of function to access the Operating System.


Quote:
Originally Posted by dlp
Here are a boat load of system() calls. Honestly, they make me nervous. I read why using system("pause") is a bad idea on the gidnetworks site, so I don't know if I should try avoiding such calls for what I'm doing. Also, are there any risks in moving files that might be as big as 2GB with this command?
In general system() is frowned upon because of portability. In your case, you are at the mercy of the operating system and compiler you are using anyway, so the code itself cannot be made portable. If you want a Unix/Linux version, or change compilers, this program will have to be rewritten anyway.

In one sense, with system() you are in less control of the program. In essense you are contracting out a portion of your project to a consulting company rather than keeping the project in-house. The program you call will utilize the same resourses you'd use to write the program yourself, but you have to figure out how to do that. They already know -- that's why you farmed out the task. I personally feel calling system() should be limited at worst, never used at best. But if you don't have the development time, it does the job.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
issues compiling .h and .cpp files with Dev C++ iceman2006 C++ Forum 9 17-May-2006 19:32
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.