movefile() problem

This is a discussion on movefile() problem within the C++ Programming forums, part of the General Programming Boards category; okay, you have all been great help. I now have the program started with it appearing to do what I ...

  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-18-2005 at 11:29 PM.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,831
    Error 2 is a file not found error.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Page 1 of 2 12 LastLast
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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21