Thread: movefile() problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    movefile() problem

    okay, you have all been great help. I now have the program started with it appearing to do what I want but when it runs no files are moved.

    I have created a string that contains my move to directory and filename. I think that is the problem it is a string.

    Would anyone have any ideas? I will post the code if anyone would want to look at it and give an opinion.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You'll need to post the peice of code that has the problem.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    here is the code

    the following is the code for this program/script

    Code:
    #include <windows.h>
    
    #include <iostream>
    # include <string>
    # include <stdio.h>
    using namespace std;
    char *curfilename;
    char newfilename, ext;
    char newpath[20], stfname[20];
    char *loc;
    int posit;
    int main(void)
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        int ErrorCode;
    
        BOOL Continue = TRUE;
    
        
    
        hFind = FindFirstFile("C:\\specfilesin\\*.pdf ", &FindData);
    
        if(hFind == INVALID_HANDLE_VALUE)
        {
            ErrorCode = GetLastError();
            if (ErrorCode == ERROR_FILE_NOT_FOUND)
            {
                cout << "nothing to do\n" << endl;
            }
            else
            {
                cout << "FindFirstFile() returned error code " << ErrorCode << endl;
            }
            Continue = FALSE;
        }
        else
        {   
            curfilename = FindData.cFileName; //
                strcpy(stfname, curfilename);
            loc = strrchr(curfilename, '_');
            string ext = loc;
            
            
             string newpath = "c:\\specfilesout\\" + ext.substr(1,3) + "\\" + stfname;
           
             BOOL movefile(
             LPCTSTR &curfilename, LPCTSTR newpath
             ) ;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    BOOL movefile(
             LPCTSTR &curfilename, LPCTSTR newpath
             ) ;
    I see too problems here.
    first it looks like your declaring a prototype of move file the windows move file function is called MoveFile. Second is that curfilename and newpath are instances of the string class which can't just be passed to a function expecting a LPCSTR, there is the c_str() member function for that.
    Code:
    MoveFile(curfilename.c_str(), newpath.c_str());
    EDIT
    well newpath is a string class
    Code:
    MoveFile(curfilename, newpath.c_str());
    Last edited by Quantum1024; 03-19-2005 at 12:29 AM.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    That doesn't seem to work, causes a compiler error

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    hmm what error?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > That doesn't seem to work, causes a compiler error
    You need to decide whether you're using C++ strings, C-char arrays or pointers.

    Code:
                strcpy(stfname, curfilename);
            loc = strrchr(curfilename, '_');
            string ext = loc;
    One example of each in 3 successive lines of code.

    Plus, get rid of all the unused variables, and make the remaining globals local to whatever block of code is the most appropriate.

    Code:
    string makeNewFileName ( string oldFileName ) {
      string result;
      // do stuff here
      return string;
    }
    I suggest you create a function (like this) which does all the string chopping and combining for you.
    Then you can test it without going to the trouble of actually opening and closing files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    well I went back and tried again, must have typed something wrong. Now I dont get a comp. error however it still doesnt do anything. all files remain in source directory and no copies are made either. I dont get any errors when the program runs.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well DUH!, post your latest effort then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Have changed a bit as suggested by Salem, still working on that part.

    Here is the code that does not move the files.

    Code:
    #include <windows.h>
    #include <strstream.h>
    #include <iostream>
    # include <string>
    # include <stdio.h>
    using namespace std;
    char *curfilename;
    char newfilename, ext;
    char newpath[20], stfname[20];
    char *loc;
    char *posit;
    int main(void)
    {
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        int ErrorCode;
    
        BOOL Continue = TRUE;
    
        
    
        hFind = FindFirstFile("C:\\specfilesin\\*.pdf ", &FindData);
    
        if(hFind == INVALID_HANDLE_VALUE)
        {
            ErrorCode = GetLastError();
            if (ErrorCode == ERROR_FILE_NOT_FOUND)
            {
                cout << "nothing to do\n" << endl;
            }
            else
            {
                cout << "FindFirstFile() returned error code " << ErrorCode << endl;
            }
            Continue = FALSE;
        }
        else
        {   
            curfilename = FindData.cFileName; 
            loc = strrchr(curfilename, '_');
            string ext = loc;
            
            
             string newpath = "c:\\specfilesout\\" + ext.substr(1,3) + "\\" + curfilename;
            
             MoveFile(curfilename, newpath.c_str()); 
           
            
            cout << "newpath= " <<newpath << endl;
            cout << "curfilename= " << curfilename << endl;
            cout << "loc= " << loc << endl;
        }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What values are printed?
    Code:
    MoveFile
    
    The MoveFile function moves an existing file or a directory, including its children.
    
    To specify how to move the file, use the MoveFileEx function.
    
    BOOL MoveFile(
      LPCTSTR lpExistingFileName,
      LPCTSTR lpNewFileName
    );
    
    Parameters
    
    lpExistingFileName
        [in] Pointer to a null-terminated string that names an existing file or directory.
    
        In the ANSI version of this function, the name is limited to MAX_PATH characters.
     To extend this limit to 32,767 wide characters, call the Unicode version of the function
     and prepend "\\?\" to the path. For more information, see Naming a File.
    
    Windows Me/98/95:  This string must not exceed MAX_PATH characters. lpNewFileName
        [in] Pointer to a null-terminated string that specifies the new name of a file or 
    directory. The new name must not already exist. A new file may be on a different file 
    system or drive. A new directory must be on the same drive.
    
        In the ANSI version of this function, the name is limited to MAX_PATH characters.
     To extend this limit to 32,767 wide characters, call the Unicode version of the function 
    and prepend "\\?\" to the path. For more information, see Naming a File.
    
    Windows Me/98/95:  This string must not exceed MAX_PATH characters.
    Return Values
    
    If the function succeeds, the return value is nonzero.
    
    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    Try actually LOOKING at the return result of MoveFile and see if what error (if any) is being returned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    As I said in an earlier post, I get no error when the program runs. The value returned is non zero

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>As I said in an earlier post, I get no error when the program runs. The value returned is non zero
    From the code you've posted, it doesn't look like you even check the return result of MoveFile().
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    it checks the error and outputs it just before the cout's, I know the code isnt listed on what I posted but it is there in the program. the return is 2, I dont know what that signifies but according to the documentation if it is not a 0 the function succeeds.

  15. #15
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Error 2 is a file not found error.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM