2 Replies - 385 Views - Last Post: 09 January 2012 - 04:07 AM Rate Topic: -----

#1 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-November 11

Upload all files to ftp server

Posted 08 January 2012 - 11:26 AM

I am trying to make a program, that uploads all files in directory to ftp server, but there is a problem. I can't get files from subdirectories. Here is what I have:
#include	<iostream>
#include	<fstream>
#include	<string>
#include	<dirent.h>
#include	<WinInet.h>

#pragma	comment ( lib, "Wininet.lib" )

#define IsInvalidDir(dir)	( ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == 0 ) ) || ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == '.' ) && ( dir [ 2 ] == 0 ) ) )

using	namespace std;

HINTERNET
	Internet,
	Ftp;

string
	kelias;

void	ScanDirectory ( const char *dirname, string t_path = "\0" );
bool	FtpConnect ( string host, string user, string password );
bool	FtpUploadFile ( const char *file );


int	main ( )
{
	string
		hostas,
		vartotojas,
		slaptazodis;

	ifstream
		failas ( "info.txt" );
	
	cout << "Surenkama informacija is failo 'info.txt'\n" << endl;
	if ( failas.is_open ( ) )
	{
		while ( failas.good ( ) ) 
		{
			getline ( failas, hostas );
			getline ( failas, vartotojas );
			getline ( failas, slaptazodis );
			getline ( failas, kelias );
			break;
		}
		cout << ">> Hostas: " << hostas << endl;
		cout << ">> Vartotojas: " << vartotojas << endl;
		cout << ">> Slaptazodis: " << slaptazodis << endl << endl;
	}
	else
	{
		ofstream
			failas2;

		cout << "Nepavyko atverti failo." << endl;
		cout << "Failas kuriamas..." << endl;
		failas2.open ( "info.txt" );
		failas2 << "ftp_hostas\n";
		failas2 << "vartotojo_vardas\n";
		failas2 << "slaptazodis\n";
		failas2 << "katalogas\n";
		cout << "Failas sukurtas. Surasykite reikiamus duomenis y faila ir paleiskite programa is naujo." << endl;
		failas2.close ( );
		system ( "PAUSE" );
		return 1;
	}
	failas.close ( );
	
	if ( !FtpConnect ( hostas, vartotojas, slaptazodis ) )
	{
		cout << "Nepavyko prisijungi prie Ftp serverio." << endl;
		InternetCloseHandle ( Internet );
		InternetCloseHandle ( Ftp );
		system ( "PAUSE" );
		return 1;
	}

	ScanDirectory ( "." );

	system ( "PAUSE" );
	return 1;
}

void	ScanDirectory ( const char *dirname, string t_path )
{
	DIR
		*katalogas;
	dirent
		*katalogo_info;

	if ( !IsInvalidDir ( dirname ) )
	{
		t_path.append ( dirname );
		t_path.append ( "/" );
	}
	if ( t_path[0] == 0 )
		katalogas = opendir ( dirname );
	else
		katalogas = opendir ( t_path.c_str ( ) );

	while ( katalogo_info = readdir ( katalogas ) )
	{
		if ( katalogo_info->d_type == DT_DIR )
		{
			if ( !IsInvalidDir ( katalogo_info->d_name ) )
			{
				ScanDirectory ( katalogo_info->d_name, t_path );
			}
		}
		else
		{

		}
		cout << t_path << "  " << dirname << "/" << katalogo_info->d_name << endl;
	}
	closedir ( katalogas );
	
	size_t
		pos;
	pos = t_path.find ( dirname );
	if ( pos != string::npos )
		t_path.erase ( pos );
}

bool	FtpConnect ( string host, string user, string password )
{
	Internet = InternetOpen ( NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
	if ( Internet == NULL )
	{
		InternetCloseHandle ( Internet );
		cout << "infail" << endl;
		return false;
	}

	WCHAR
		whost [ 50 ],
		wuser [ 50 ],
		wpassword [ 50 ];

	char
		temp [ 50 ];

	strcpy ( temp, host.c_str ( ) );
	MultiByteToWideChar ( 0, 0, temp, -1, whost, 50 );
	strcpy ( temp, user.c_str ( ) );
	MultiByteToWideChar ( 0, 0, temp, -1, wuser, 50 );
	strcpy ( temp, password.c_str ( ) );
	MultiByteToWideChar ( 0, 0, temp, -1, wpassword, 50 );
	

	Ftp = InternetConnect ( Internet, whost, INTERNET_DEFAULT_FTP_PORT, wuser, wpassword, INTERNET_SERVICE_FTP, 0, 0 );
	if ( Ftp == NULL )
	{
		InternetCloseHandle ( Internet );
		InternetCloseHandle ( Ftp );
		cout << whost << " " << wuser << " " << wpassword << endl;
		return false;
	}
	return true;
}

bool	FtpUploadFile ( const char *file )
{
	char
		dest [ 200 ],
		ckelias [ 50 ];

	strcpy ( ckelias, kelias.c_str ( ) );
	sprintf ( dest, "%s/%s", ckelias, file );
	
	WCHAR
		wfile [ 50 ],
		wkelias [ 200 ];

	MultiByteToWideChar ( 0, 0, file, -1, wfile, 100 );
	MultiByteToWideChar ( 0, 0, dest, -1, wkelias, 200 );

	if ( !FtpPutFile ( Ftp, ( LPCWSTR )wfile, ( LPCWSTR )wkelias, FTP_TRANSFER_TYPE_BINARY, 0 ) )
	{
		return false;
	}
	return true;
}



Function ScanDirectory gets all files in current directory, and subdirectories, but if there is "CurrentDir/SecondDir/ThirdDir", program doesn't reach ThirdDir. How can I solve this?

Is This A Good Question/Topic? 0
  • +

Replies To: Upload all files to ftp server

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: Upload all files to ftp server

Posted 08 January 2012 - 05:12 PM

Hi. You can copy the path in the function.


/* opendir.cpp - test opendir(), readdir(), closedir() */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <string>
using namespace std;


#define IsInvalidDir(dir)  ( ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == 0 ) ) || ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == '.' ) && ( dir [ 2 ] == 0 ) ) )

// check name is directory
void  ScanDirectory ( const char *dirname, string t_path = "")
{
  DIR  *katalogas;
  struct dirent *katalogo_info;
  // copy path
  string x_path = t_path;

  // test directory allow initial . and ..
  if (IsInvalidDir ( dirname ) && t_path.length() != 0 )
  {
    cout << "<INVALID DIR>";
    return;
  }

  // update path with current name - supposed directory
  x_path.append ( dirname );
  // add backslash at end if none
  if(x_path.length() > 0 && x_path[x_path.length()-1] != '\\')
    x_path.append ( "\\" );

  // open and test directory
  katalogas = opendir ( x_path.c_str ( ));

  if(katalogas == NULL )
  {
    cout << "<NOT DIR>";
    return;
  }

  // name is a directory
  cout << "<DIR>";

  // output directory contents
  cout << endl;
  cout << "<PATH>" << x_path << endl;

  while ( (katalogo_info = readdir ( katalogas )) != NULL)
  {
    cout << "  " << katalogo_info->d_name;
    ScanDirectory ( katalogo_info->d_name, x_path );
    cout << endl;
  }

  cout << "<ENDPATH>" << x_path << endl;

  closedir ( katalogas );

}


int main(int argc, char *argv[])
{
  if (argc != 2)
  {
    cout << "usage: opendir dirname\n";
    exit(1);
  }

  cout << "Directory of " << argv[1] << " ";

  ScanDirectory(argv[1]);

  return(0);
}


Was This Post Helpful? 0
  • +
  • -

#3 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 10-November 11

Re: Upload all files to ftp server

Posted 09 January 2012 - 04:07 AM

View Post#define, on 08 January 2012 - 05:12 PM, said:

Hi. You can copy the path in the function.


/* opendir.cpp - test opendir(), readdir(), closedir() */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <string>
using namespace std;


#define IsInvalidDir(dir)  ( ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == 0 ) ) || ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == '.' ) && ( dir [ 2 ] == 0 ) ) )

// check name is directory
void  ScanDirectory ( const char *dirname, string t_path = "")
{
  DIR  *katalogas;
  struct dirent *katalogo_info;
  // copy path
  string x_path = t_path;

  // test directory allow initial . and ..
  if (IsInvalidDir ( dirname ) && t_path.length() != 0 )
  {
    cout << "<INVALID DIR>";
    return;
  }

  // update path with current name - supposed directory
  x_path.append ( dirname );
  // add backslash at end if none
  if(x_path.length() > 0 && x_path[x_path.length()-1] != '\\')
    x_path.append ( "\\" );

  // open and test directory
  katalogas = opendir ( x_path.c_str ( ));

  if(katalogas == NULL )
  {
    cout << "<NOT DIR>";
    return;
  }

  // name is a directory
  cout << "<DIR>";

  // output directory contents
  cout << endl;
  cout << "<PATH>" << x_path << endl;

  while ( (katalogo_info = readdir ( katalogas )) != NULL)
  {
    cout << "  " << katalogo_info->d_name;
    ScanDirectory ( katalogo_info->d_name, x_path );
    cout << endl;
  }

  cout << "<ENDPATH>" << x_path << endl;

  closedir ( katalogas );

}


int main(int argc, char *argv[])
{
  if (argc != 2)
  {
    cout << "usage: opendir dirname\n";
    exit(1);
  }

  cout << "Directory of " << argv[1] << " ";

  ScanDirectory(argv[1]);

  return(0);
}


Thank you, it works. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1