[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Moving folder to USB pen

Posted on 2009-04-27
10
Medium Priority
?
575 Views
Last Modified: 2013-11-17
I have a folder in the C drive say for example called temp this folder can contain a number of sub folders which could each contain text files with the same name

eg temp  contains two folders   - folder 1 containing text file test1 and test2
                                                   - folder 2 cointaining text file test1 and test2

What I would like the user to do is to be able to put in USB pen and copy the temp file from the C drive to the USB pen.  There are a few conditions

1) The first time this is done the USB pen will be empty
2) The next time information is copied to USB pen I want the program to overwrite the temp folder currently stored on USB pen
3) When the copy is complete I would like to delete the folder from the c drive

Any help greatly appreciated
0
Comment
Question by:lisa_mc
  • 7
  • 3
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24243267
Take a lookt at http://www.codeproject.com/KB/system/HwDetect.aspx ("Detecting Hardware Insertion and/or Removal") on how to detect when an USB drive is inserted and how to obtain the drive's letter. Once you have that one, you can use the following to achieve the whole copying thing, including the removal of 'c:\\temp'
#include <windows.h>
#include <stdio.h>
 
#pragma comment(lib,"shell32.lib")
 
bool MoveFiles(char *driveLetter)
{
     const char *fName = "CopyFiles";
     char from[256];
     char to[256];
     
     SHFILEOPSTRUCT  sfo;
     ZeroMemory  (   &sfo,   sizeof  (   SHFILEOPSTRUCT));
     sprintf(to, "%s:\\", driveLetter );
     sprintf(from, "c:\\temp\\*.*");
 
     printf("%s\n", from);
     printf("%s\n", to);
 
     sfo.wFunc   =   FO_MOVE;
     sfo.pFrom   =   from;
     sfo.pTo     =   to;
 
    sfo.fFlags  =   FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR;
    if(SHFileOperation (&sfo))
     {
          int err = GetLastError();
 
          printf("error %d\n", err);
     }
     
     return TRUE;
}
 
void main()
{
 
     CopyFiles("d");
}

Open in new window

0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24250137
hiya thanks for the comment

Im getting two errors with the abve code

1) fName is assigned a value that is never used
2) Call to undefined function CopyFiles ( should CopyFiles in main not be MoveFiles if so I tried this and Im now getting a fatal error saying that cannot open Shell32.lib)

Any suggestions
0
 
LVL 86

Expert Comment

by:jkr
ID: 24250917
>>should CopyFiles in main not be MoveFiles

Yes, sorry, I changed the example and forgot about that one.

>>and Im now getting a fatal error saying that cannot open Shell32.lib

That is strange, it should be present in your compiler's 'Lib' directory.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:lisa_mc
ID: 24250972
I checked the lib files and Shell3.lib desn't exist - should this exist for borland C++ builder?   Is there anyway of getting this file?

Also if I chnge to MoveFiles("d"); does the d represent the drive of the USB pen?
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24250983
sorry i meant shell32.lib doesn't exist
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24251055
just found the .lib file and copied it into the lib folder so I have got rid of that error

At the min my program uses forms in C++ builder 6.0 and I want the program to copy when user hits a button on the form.  Im getting an error an im not sure what it means

unresolved external '_form1' referenced from tster.obj
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24251121
ok ive just got it working my mistake 1 problem

when I copy the temp_folder to the USB pen it only copies the contents of temp_folder ie folder1 and folder2 - I need it to copy the temp_folder and put folder1 and folder2 inside
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24251456
got it working can you tell me which line of code deletes the directory of the c drive?  What would I need to change if I didn't want to delete it.

Also if I wanted to dislayed a message to the user telling them that the copy was complete how could I do this

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 24252618
Sorry for the delay, had a doctor's appointment.

>>can you tell me which line of code deletes the directory of the c drive?  What
>>would I need to change if I didn't want to delete it.

It is the line

     sfo.wFunc   =   FO_MOVE;

'Move' implies that the source files are deleted once they are copied to their new destination. If you don't want that, you can just change that line to

     sfo.wFunc   =   FO_COPY;

and the files will be copied only without being removed from the source location. See also the docs on 'SHFileOperation()' (http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx) and 'SHFILEOPSTRUCT' (http://msdn.microsoft.com/en-us/library/bb759795(VS.85).aspx)
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 24270234
Thats very much for the reply everything works great

Appreciate the help many thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
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.

834 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