0

I have found many posts on checking if the file on the ftp server exists. None of the codes provided helped. I am trying to auto upload small files to the ftp server BUT I don't want to override the file if it already exists. The following code works if the file already exists but if it doesn't it kicks back an error and does not run the code in the if statement.

System.Console.WriteLine("check if hash file is on ftp server " + hashfile);
string fileSize = ftpClient.getFileSize(hashfile);
int fSize =-1;
bool res = (Int32.TryParse(fileSize, out fSize));
if (res)
{
    System.Console.WriteLine("Hash file is not on ftp server " + hashfile);
    ftpClient.upload(hashfile, @hashfile);
    ftpClient.upload(charfile, @charfile);
    File.Delete(hashfile);
    File.Delete(charfile);
}
else
{
    System.Console.WriteLine("Hash file is on ftp server " + hashfile);
    string newHashFile = "ntlmhash" + str_cint + "_" + str_pint + "_" + e + ".txt";
    while (File.Exists(newHashFile))
    {
        e++;
        newHashFile = "ntlmhash" + str_cint + "_" + str_pint + "_" + e + ".txt";
        string FileSize = ftpClient.getFileSize(newHashFile);
        int FSize =-1;
        if (Int32.TryParse(FileSize, out FSize))
        {
            ftpClient.upload(newHashFile, @newHashFile);
            ftpClient.upload(charfile, @charfile);
            File.Delete(newHashFile);
            File.Delete(charfile);
        }
        else
        {
            e++;
        }
    }
    System.IO.File.Move(hashfile, newHashFile);
    ftpClient.upload(newHashFile, @newHashFile);
    ftpClient.upload(charfile, @charfile);
    File.Delete(newHashFile);
    File.Delete(charfile);
}
Yuck
  • 49,664
  • 13
  • 105
  • 135
  • You can find the answer here http://stackoverflow.com/questions/347897/how-to-check-if-file-exists-on-ftp-before-ftpwebrequest – M.A May 15 '13 at 18:54
  • I tried that and it does not work. It might tell me if the file exists but how would I be able to run a loop to rename the file and check it again? – user2087542 May 15 '13 at 19:08
  • You can close this ticket. I used the date time ticks to make sure that there are no duplicate files on the ftp server. – user2087542 May 20 '13 at 18:36

0 Answers0