CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Recursion

  1. #1
    Join Date
    Jun 2013
    Posts
    21

    Recursion

    The code below is suppose to create directories and upload files, but it is not working. What can I do to make the code work?


    Code:
    void CRECURSIONDlg::Recursion(CString sPath)
    {
    
    hInternet = InternetOpen(TEXT("BEAR FTP"),
       INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    
    // Make sure there are no errors
    hIConnect = InternetConnect(hInternet,"192.168.1.4", 211,
       "******", "*****", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
    
    
    HANDLE hFind;
    WIN32_FIND_DATA fdFind;
    UpdateData(true);
    
    CString salvation = m_path + "\\" + sPath;
    	UpdateData(false);
    
    
    hFind = FindFirstFile( salvation, &fdFind );
    
    
    
    do{
    
    if( fdFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
    {
    	FtpCreateDirectory(hIConnect,fdFind.cFileName);
    UpdateData(true);
    CString cov = m_path;
    UpdateData(false);
    CString newPath = cov + "\\" + fdFind.cFileName;
    FtpCreateDirectory(hIConnect,fdFind.cFileName);
    
    Recursion(newPath);
    }
    if(fdFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
    continue;
    else
    {
    FtpPutFile(hIConnect,fdFind.cFileName,fdFind.cFileName,FTP_TRANSFER_TYPE_BINARY,NULL);
    
    }
    
    
    
    
    
    
    }while( FindNextFile( hFind, &fdFind ) );
    
    
    }
    Last edited by vaas; July 21st, 2013 at 06:12 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Recursion

    What can I do to make the code work?
    Debug the code and fix it!

    What debugging of this code have you done? What part is not working?

    Where are you checking the result of InernetOpen, InternetConnect and UpdateData for errors? What about FtpCreateDirectory and FtpPutFile? How do you know they are not returning errors? What if FindFirstFile doesn't find a file? Everytime you use an API you need to check that it has succeeded and if it has failed unexpectedly then log the reason for the failure.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2013
    Posts
    21

    Re: Recursion

    Code:
    BOOL CListfilesDlg::recursion(CString sPath)
    {
    
    	HANDLE hFind;
    WIN32_FIND_DATA fdFind;
    UpdateData(true);
    
    	CString StrPathS = m_editvalue;
    	
    CString salvation =  StrPathS + sPath;
    	UpdateData(false);
    
    CString ren = salvation + "\\*.*";
    hFind = FindFirstFile( ren, &fdFind );
    
    
    
    do{
    
    if( fdFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    	 if(IsDots(fdFind.cFileName)) continue; // skip . and ..
    	
    UpdateData(true);
    CString cov = sPath;
    CString fire = m_editvalue;
    UpdateData(false);
    CString newPath =  cov + "\\" + fdFind.cFileName;
    FtpCreateDirectory(hIConnect,fdFind.cFileName);
    
    recursion(newPath);
    }
    if(fdFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
    continue;
    else
    {
    
    
    }
    
    
    
    
    
    
    }while( FindNextFile( hFind, &fdFind ) );
    
    return 0;
    }

    The code above will take a directory and rip out all the sub directory and put them in the same directory. how can I upload a directory with all its contents inside.? the code above is what I got so far. can someone help out as ihave been trying to get this work..even tried debugging..but no luck

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Recursion

    Quote Originally Posted by vaas View Post
    Code:
    BOOL CListfilesDlg::recursion(CString sPath)
    {
    
    	HANDLE hFind;
    WIN32_FIND_DATA fdFind;
    UpdateData(true);
    
    	CString StrPathS = m_editvalue;
    	
    CString salvation =  StrPathS + sPath;
    	UpdateData(false);
    
    CString ren = salvation + "\\*.*";
    hFind = FindFirstFile( ren, &fdFind );
    
    
    
    do{
    
    if( fdFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    	 if(IsDots(fdFind.cFileName)) continue; // skip . and ..
    	
    UpdateData(true);
    CString cov = sPath;
    CString fire = m_editvalue;
    UpdateData(false);
    CString newPath =  cov + "\\" + fdFind.cFileName;
    FtpCreateDirectory(hIConnect,fdFind.cFileName);
    
    recursion(newPath);
    }
    if(fdFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
    continue;
    else
    {
    
    
    }
    
    
    
    
    
    
    }while( FindNextFile( hFind, &fdFind ) );
    
    return 0;
    }

    The code above will take a directory and rip out all the sub directory and put them in the same directory. how can I upload a directory with all its contents inside.? the code above is what I got so far. can someone help out as ihave been trying to get this work..even tried debugging..but no luck
    First of all, please format your code properly. No one is going to try and read code where their eyes are going back and forth as if they're watching a tennis match.
    Code:
    BOOL CListfilesDlg::recursion(CString sPath)
    {
        HANDLE hFind;
        WIN32_FIND_DATA fdFind;
        CString StrPathS = m_editvalue;
        CString salvation =  StrPathS + sPath;
        CString ren = salvation + "\\*.*";
        hFind = FindFirstFile( ren, &fdFind );
        do
        {
            if (fdFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
            { 
               if( IsDots(fdFind.cFileName)) 
                    continue; 
               CString cov = sPath;
               CString fire = m_editvalue;
               CString newPath =  cov + "\\" + fdFind.cFileName;
               FtpCreateDirectory(hIConnect,fdFind.cFileName);
               recursion(newPath);
            }
            if (fdFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
               continue;
            else
            {
            }
          } while( FindNextFile( hFind, &fdFind ) );
        return 0;
    }
    So you're saying you can't debug this code?

    Remove the "UpdateData" as they are unimportant to the issue. You have a sample of a directory you want to traverse, right? So step through this code and identify where the code deviates from what you expected to occur.

    Which function, which line, etc. does it start to go awry or different than what you expect? Is it the IsDots() function? If so, is it returning true when you expected it to return false? Is it the FtpCreateDirectory()? Where is your check for the return code? Does it create a directory, or does it create one with a wrong name? Is it the FindNextFile()? Is it not finding the next file? Is the newPath the correct name? When the code calls recursion again, is it following the path you expected? (you should have some sort of diagram as to what calling recursion() again will do). Etc. Etc.

    This is how you debug code. Debugging doesn't mean run code, change code, run code, change code, and in-between these steps, hope for the best. Debugging means to go through the code and see where it deviates from the plan you had in mind by inspecting the variables, return values, etc.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 23rd, 2013 at 08:46 PM.

  5. #5
    Join Date
    Jun 2013
    Posts
    21

    Re: Recursion

    I got it to upload all the directorys but now I need the files to be placed in the folders to but im having no luck. the files are going in the very last sub directory. Heres is my updated code!

    Code:
    BOOL CListfilesDlg::recursion(CString sPath)
    {
    
    	 HANDLE hFind;
      WIN32_FIND_DATA fdFind;
        
      UpdateData(true);
      TCHAR DirPath[MAX_PATH];
       TCHAR FileName[MAX_PATH];
      CString ren = m_editvalue + sPath + "\\*.*";
        _tcscpy(FileName,sPath);
       _tcscat(FileName,"\\");
      hFind = FindFirstFile( ren, &fdFind );
     
      do
      {
        if( fdFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
        {
          if(IsDots(fdFind.cFileName)) 
            continue;
    	
          FtpCreateDirectory(hIConnect,fdFind.cFileName);
    	  FtpSetCurrentDirectory(hIConnect,fdFind.cFileName);
          
                
          CString newPath = sPath + "\\" + fdFind.cFileName;
    	  
    	   
          recursion(newPath);
        }
     
        if(fdFind.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
          continue; 
    	else
    	{
    		FtpPutFile(hIConnect,fdFind.cFileName,fdFind.cFileName,FTP_TRANSFER_TYPE_BINARY, 0); 
    
    
    	}
      }
      while(FindNextFile( hFind, &fdFind ) );
    
    
    return 0;
    }
    So where should I place ftpputfile? how should I recode this code.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Recursion

    So what is the FtpCurrentDirectory at the point you use FtpPutFile? (Use FtpGetCurrentDirectory). From ealier posts, how do you know that FtpCreateDirectory(..) and FtpSetCurrentDirectory(..) have been successful as you are not testing for errors?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Recursion

    Quote Originally Posted by vaas View Post
    I got it to upload all the directorys
    As others have stated, where is your check that the functions you're calling actually returned with a success code?

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    What if that function returns FALSE? You should never call functions and just assume that they "work".

    Second, since you know what your program is doing wrong, I don't see why you can't just debug your program to see why it's just going to the last subdirectory. Do you know how the program you have is supposed to work? If you do, again, where in the code does it go different than what you expected?

    So far, you're telling us on a high-level what is wrong, as if you're not a programmer. Since you now have taken on the mantle of a programmer, you're job is to go at the "programmer level", and that is to run the program under the debugger and noting where the program takes a different path than what you expected.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured