Solved

ftp & changing textfile name

Posted on 2007-11-26
27
231 Views
Last Modified: 2008-02-01
Hello experts,
my problem is i mae a ftl uploading file but i have the problem cause the name of my text file it's static
so for everyn uploading it overwrite the last file with the new file content.
how can i change the text file name for every uploading?thank you
check my code pls
bool InviaLog()
{
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** connection problem***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"ftp.my spaceweb",INTERNET_DEFAULT_FTP_PORT, "myUserID","mypass", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** starting FTP session failed ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // if the file is uploade i delete the file
  StartLog();  // i delete the file
 return TRUE;
}

Open in new window

0
Comment
Question by:aitsu01
  • 14
  • 11
  • 2
27 Comments
 
LVL 5

Expert Comment

by:abith
ID: 20349170
file name can be passed as a parameter,
or file name can be created with time stamp, just like myfile20072611hhmmss...
0
 

Author Comment

by:aitsu01
ID: 20349238
hello abith,
ok i am agree for the file name can be created with the time stamp but how can i code it with you
suggestions??Thank you
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20349243
You'll have to be a little smarter than that I'm afraid... what if 2 or more people connect at the same time? The more users you have the more chance you have of a collision. Consider using time date as abtih has suggested but with an additional random element, such as the connecting clients IP address or MAC address.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:aitsu01
ID: 20349263
hello elverix ,
this is a good idea for example with the user name added too.
but my problem is how to append the time stamp  (or in youca case the IP)to my text file before the uploading.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20349276
Are you asking how to acquire these data or how to append them to a string? I'm not quite sure what it is you need help with!
0
 

Author Comment

by:aitsu01
ID: 20349281
helo evilrix,
i meant both.
0
 
LVL 5

Expert Comment

by:abith
ID: 20349473
refer this link http://www.thescripts.com/forum/thread432803.html
it gives to take timestamp. use sprinf to convert as a string.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20350531
Hmm. Trying to get IP-ADDR from IntenetConnect() isn't simple and would necessitate a lot of work. This; however, should be pretty safe. It won't guarantee no collisions but the chances are very slim. You'd have to have 2 people generate the same random number (0 - 99999) within a milli-second of each other! It's a working example (it uses STL -- all C++ compilers support STL) that you'll need to stitch into your own code (including the headers!). It should be enough for you to figure out what you need to do.

-Rx.
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string>
 
void FtpPutFile(char const * pFilename)
{
	// Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
int InviaLog ()
{
	//////////////////////////////////////////////////////////////////////////
	// Insert this code into yours
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
	return 1;
}
 
int main()
{
	// Put this line in you main() function to prime the random seed generator
	srand (static_cast<int>(time(NULL)));
 
	InviaLog();
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20350561
>> within a milli-second of each other
Sorry, typo -- that should have said "within a second of each other" -- I got a little bit over ambitious with the value held by time_t :)
0
 

Author Comment

by:aitsu01
ID: 20356491
hello evilrix,
why the procedure invialog() is INT now?i got soem error and i could not find the solution
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20360580
>> why the procedure invialog() is INT now?

Oh, sorry -- not for any specific reason... you can ignore that function return type, it was purely just used as an example... the only part you are specifically interested in is the part inside it, which needs to be
transplanted into your function InviaLog().

>> i got soem error and i could not find the solution
What error?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20360589

#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string>
 
void FtpPutFile(char const * pFilename)
{
	// Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
bool InviaLog ()
{
	//////////////////////////////////////////////////////////////////////////
	// Insert this code into yours
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
	return TRUE;
}
 
int main()
{
	// Put this line in you main() function to prime the random seed generator
	srand (static_cast<int>(time(NULL)));
 
	InviaLog();
}

Open in new window

0
 

Author Comment

by:aitsu01
ID: 20372416
Hello evilrix,
thank you for your supporting.
well i have been busy recenlty anyway sooon i will try your code
and if possible i will attach here the error logs.
talk to you soon
0
 

Author Comment

by:aitsu01
ID: 20373509
Hello,
well the file has been compiled in .exe but once launche noithing happen
i mena on my ftp server there not uploaded text file i just wonder why??
maybe the myfile.txt name has been changed with the new code??
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20373566
So, have you tried stepping into the code int he debugger to see what's happening?
0
 

Author Comment

by:aitsu01
ID: 20373774
Hello evilrix,
well the debugging was ok i compiled the exe without any error
but it program cannot send the file anymore may i forgot soemthing in the code





i will attach here my old code for the ftp connection



bool InviaLog()
{
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** connection problem***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"ftp.my spaceweb",INTERNET_DEFAULT_FTP_PORT, "myUserID","mypass", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** starting FTP session failed ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // if the file is uploade i delete the file
  StartLog();  // i delete the file
 return TRUE;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20373801
>> i will attach here my old code for the ftp connection

Um, posting current code might be more helpful :)
0
 

Author Comment

by:aitsu01
ID: 20374021
oops sorry evilrix,
keep in mind this code by now

 
 
 
void FtpPutFile(char const * pFilename)
{
        // Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
 
bool InviaLog()
{
     std::stringstream ssFilename;
 
        ssFilename
                << "myfile-"
                << time(NULL)
                << "-"
                << std::setw(5)
                << std::setfill('0')
                << (rand() % 100000)
                << ".txt";
 
        std::string const & sFilename = ssFilename.str();
        //////////////////////////////////////////////////////////////////////////
 
        // Usage
        FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
        return TRUE;
 
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
 
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
  StartLog();  // Cancellato il file, riscrivo lo startup log
 return TRUE;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20374208
You were calling the dummy FtpPutFile and returning immediately.
#include "stdafx.h"
 
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20374329
I removed this...

        //////////////////////////////////////////////////////////////////////////
 
        // Usage
        FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
        return TRUE;


and change this...


if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){


to this...

if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){


Does that make sense?
0
 

Author Comment

by:aitsu01
ID: 20374348
I got this error

C stdafx.h: No such file or directory.

what do you mean for #include "stdafx.h"??
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 20374384
Oops, sorry -- delete that... it's a precompiled header -- you don't need it!
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20374417
BTW: You will need to include the headers...

#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

I've just noticed you didn't include them in your post.
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
 
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 

Author Comment

by:aitsu01
ID: 20374854
now i just got 6 error
somme connession error i think:


  [Linker error] undefined reference to `InternetOpenA@20'
  [Linker error] undefined reference to `InternetConnectA@32'
  [Linker error] undefined reference to `FtpPutFileA@20'
  [Linker error] undefined reference to `InternetCloseHandle@4'
  [Linker error] undefined reference to `InternetCloseHandle@4'
  ld returned 1 exit status
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20375269
Well, I'm guessing you haven't linked against wininet.lib -- you must have removed or modified something as you say it worked previously! The code I provided was meant to be stitched into your existing code, not a replacement for it. You still need to ensure all your previous prerequisites are met, like including windows.h and linking to the same libraries.

InternetOpen(), InternetConnect(), FtpPutFile(), InternetCloseHandle(): -

Header Declared in Wininet.h.
Library Use Wininet.lib.
DLL Requires Wininet.dll.

http://msdn2.microsoft.com/en-us/library/aa385483.aspx
 
0
 

Author Comment

by:aitsu01
ID: 20381087
Hello Evilrix,
i have a good news.he project is working now!
as you right said ,i missed some dll ,so after that i fixed it everything working good,thank you so much!
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20381184
You are very welcome my friend :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

825 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