Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting files from a server 2

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB
Hi all

Firstly, apologies if this is the wrong forum but I would be grateful for some direction where to post it if it's not.

I am trying to find out if there is a way to delete certain files from a server via phpmyadmin or some other way.

Everyday, we upload some CSV files via phpmyadmin and also upload some related images to those CSV files that contain music data. The images are the covers for the titles and all the processing and conversion is done via a Visual Foxpro 9 custom application.

As the table changes everyday, some of the images are no longer required and if not deleted, will start filling up our space.

The images are named for example 315268243789.jpg (or could be .gif), which relates to a UPC number or barcode. The list is created from the same VF9 APP (The exported file format can be adapted to any type e.g. txt, csv, tab etc).

The scenario we are trying to achieve is to create the new list (no problem), then run a piece of code that will remove the "no longer required" images from the server.

If you need any further information, please let me know.

Sorry it was long winded guys, but I hope you understand what I mean.

Many thanks
Lee

Windows XP
Visual FoxPro Version 6 & 9
 
Hi Chris

Thank you for the response.

With regards to the syntax, I am on the right track:
Code:
DO ftpdelete ;
  WITH 'ftp://ourhost@mywebsite.com/', ;
  'our_user_name', 'our_password', 'the_image.jpg'
Do I include the ' or remove it?

I tried running the code but received an error:
Code:
Variable 'STRHOST' is not found
Thank you
Lee

Windows XP
Visual FoxPro Version 6 & 9
 
Chris

Thank you for the correction. Ran that code and it gave me the wait window
Code:
WAIT WINDOW "ftp " + lchost + " is not available"
I'm guessing here it is the way I have assigned the access details to the variables as when I use Internet Explorer to access the FTP account or and FTP program it works OK.

If I show you what I've done with slightly different details, could you please suggest if what I'm doing is correct or otherwise?
Code:
DO ftpdelete WITH ;
  'ftp://u4XXXXXX5@ourweb.com/images/', ;
  'u4XXXXXX5', 'password', 'image_3.gif'
Thank you
Lee


Windows XP
Visual FoxPro Version 6 & 9
 
Lee

I use a variant of the code in the FAQ for connecting, downloading, uploading files etc to a FTP server. The WinAPI calls are the same.

I have made a note in my app that
Code:
ftp.mysite.com
fails whilst
Code:
mysite.com
succeeds.

Also I suggest you create a path for the file to be deleted with
Code:
'images/image_3.gif'
and so enter the FTP server at the root or other folder level and navigate to the file to be deleted through the path designated.

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander.com
PDFcommander.co.uk
 
Chris

After a few tweaks I've now managed to get it working after your additional advice. I've also been able to use a variable as the whole object of this project is to delete multiple files pergaps using a DO WHILE loop
Code:
STORE SPACE(20) TO mfile
STORE "image01.gif" TO mfile
DO ftpdelete WITH ;
  'oursite.com', ;
  'u4XXXXXX5', 'password', mfile
So far so good....

The only slight probem now is the fact that the images are stored in a folder on the server called (for test purposes) TEST. I tried changing the 'oursite.com' to 'oursite.com/test' and also '/test' and a further way 'test/' but all come back with the wait window message ftp test is not available

Apart from that Chris, it is just the solution I am looking for so could you (or anyone else viewing this thread) suggest how to overcome this final problem?

Thanks again for your time.
Lee

Windows XP
Visual FoxPro Version 6 & 9
 
Lee

Does that mean Chris' solution does not work for you?
Have a look at SetCurrentDirectory() API that allows you to change the current directory in FTP server.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Lee

Thanks for the star, although the person who really should be recognised is DSummZZZ for providing the FAQ.

You mention that you also access the FTP site through Internet Explorer so you can view the folder structure.

The structure of your FTP site may be similar to:-

folder1
[tab]folder2
[tab][tab]htdocs
[tab][tab][tab]downloads
[tab][tab][tab]images
[tab][tab][tab]uploads
[tab][tab][tab]sounds

You may well enter the FTP site through the 'htdocs' folder where typically website files might be stored, so the filename of the file to be deleted becomes
Code:
'images/image_3.gif'
where the 'images' folder is a first level subfolder of the folder where you entered the site.

I've not tried Mike's suggestion which may provide an alternative answer.

It's also worth remembering that with Broadband, suitable router and spare network box it's relatively easy for anyone to set up their own Windows FTP/HTTP server for free!

If of interest, please start a new thread.

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander.com
PDFcommander.co.uk
 
Chris,

You are right, it sould be FtpSetCurrentDirectory(), the SetCurrentDirectory() was the name on my function which calls for FtpSetCurrentDirectory().


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Mike
Does that mean Chris' solution does not work for you?
It does work for individual files stored in the root folder on the server. The problem was being unable to delete files in another folder which has now been resolved.

Chris
After a few more tweaks its now working as required. To delete multiple files, we came up with this:
Code:
mremove=0
USE FILEDELETE
GO TOP
DO WHILE NOT EOF()
  STORE SPACE(30) TO mimage
  STORE TRIM(FILENAME) TO mimage
  STORE "images/"+TRIM(mimage) TO mfile
  DO ftpdelete WITH ;
    'oursite.com', ;
    'u4XXXXXX5', 'password', mfile
  mremove=mremove+1
  SKIP
ENDDO
CLEAR
=MESSAGEBOX("File deletion process complete. Files removed from server: "+LTRIM(STR(mremove))+SPACE(10), ;
  0+32+0,"System Message")
Thanks again for all your help and advice and my thanks to Mike for his posting.

Kind regards
Lee

Windows XP
Visual FoxPro Version 6 & 9
 
Lee

I appreciate you have a working solution and if that is sufficient, fine. [smile]

However, if you have a lot of files to delete, the methodology of accessing the FTP site, deleting a single file and then closing the connection will be slow. [sad]
Code:
IF hftpSession = 0
   * close access to Inet functions and exit
   = InternetCloseHandle (hOpen)
   WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
ELSE
   WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
   IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
      WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
   ELSE
      WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
   ENDIF
ENDIF
If you were to put a SCAN... ENDSCAN code block within the main ELSE... ENDIF part of the above code, you could delete all the required files in a single 'visit' to the FTP site.

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander.com
PDFcommander.co.uk
 
Chris

Yes, that sounds good as potentially there could be about 5000 files to delete in one visit.

One thing I did do was to remove the WAIT WINDOW commands as I didn't think they were necessary for the exeption of
Code:
WAIT WINDOW "ftp " + strHost + " is not available"  TIMEOUT 2
and
Code:
WAIT WINDOW 'Error deleting ' + lcRemoteFile + "." TIMEOUT 1
I am away for a few days, so I'll post back towards the weekend.

Thanks again for your suggestion.

Lee [thumbsup2]

Windows XP
Visual FoxPro Version 6 & 9
 
Chris

Having tried this proces for a few days it does appear to be working but as you have quite rightly mentioned, the process could be quicker by changing the code.

You mentioned about placing SCAN WHILE NOT EOF()... etc between ELSE and ENDIF
Code:
ELSE
   WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  NOWAIT
   IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1

   ELSE
      WAIT WINDOW 'Error deleting ' + ;
        lcRemoteFile + "." TIMEOUT 1
   ENDIF
ENDIF
Can I assume that IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1 is the succesful connection and that once connected, I need to run the SCAN WHILE NOT EOF() between that IF and ELSE?

Many thanks
Lee

Windows XP
Visual FoxPro Version 6 & 9
 
Lee
Lee said:
Can I assume that IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1 is the succesful connection and that once connected, I need to run the SCAN between that IF and ELSE?
No


Assuming the cursor or table FILEDELETE mentioned in a previous post to contain only the file names to be deleted in a field called filename, you use could use code such as
Code:
[COLOR=blue]SELE FILEDELETE
SCAN
[tab]IF FtpDeleteFile(hftpSession,[images/] + ALLT(FILEDELETE.filename)) = 0
[tab][tab]WAIT WINDOW 'Error deleting ' + ALLT(FILEDELETE.filename) TIME 3
[tab]ENDIF
ENDSCAN[/color]
in the ELSE... ENDIF code block of the first IF... ENDIF statement.

SCAN... ENDSCAN processes all records and will be quicker than DO WHILE... ENDDO.

If you wish to filter in the loop, the syntax is SCAN FOR condition... ENDSCAN.

One comment about the use of WAIT WINDOW - it's not Windows compliant so if you want be 'politically correct', you should not use it in a distributed application.

You can always use MESSAGEBOX with a TIMEOUT value or take a look at an Asynchronous MESSAGEBOX in faq1254-6192

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander.com
PDFcommander.co.uk
 
Chris

Thank you for the response.

I added the code as suggested which now looks like this:
Code:
ELSE
   SELECT FILEDELETE
   SCAN
     IF FtpDeleteFile(hftpSession,[images/] + ;
       ALLT(FILEDELETE.COLUMN5)) = 0
       WAIT WINDOW 'Error deleting ' + ;
        ALLT(FILEDELETE.COLUMN5) TIMEOUT 3
     ENDIF
   ENDSCAN
IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
* WAIT WINDOW lcRemoteFile + ' deleted.' TIMEOUT 1
When I ran this, the images I had upload as a test were not deleted (wait window showed error deleting myimage.gif etc) and also an error message for the below line showing data type mismatch:
Code:
IF FtpDeleteFile(hftpSession, lcRemoteFile) = 1
Please can you suggest what's wrong?

Many thanks
Lee



Windows XP
Visual FoxPro Version 6 & 9
 
Lee

Assuming there are no typos in the code posted, try
Code:
[COLOR=blue]IF
[tab][/color][COLOR=green]* Existing code[/color][COLOR=blue]
ELSE
[tab]SELECT FILEDELETE
[tab]SCAN
[tab][tab]IF FtpDeleteFile(hftpSession,[images/] + ;
[tab][tab][tab][tab]ALLT(FILEDELETE.COLUMN5)) = 0
[tab][tab][tab]WAIT WINDOW 'Error deleting ' + ;
[tab][tab][tab][tab]ALLT(FILEDELETE.COLUMN5) TIMEOUT 3
[tab][tab]ENDIF
[tab]ENDSCAN
ENDIF[/color]

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander.com
PDFcommander.co.uk
 
Hi Chris

Success! The process works very quick and deletes multiple files within seconds. I haven't run a time test yet, but with an open connection, it deleted about twenty images in less than a second.

My sincerest thanks to you for all your help Chris

Kind regards
Lee [thumbsup]

Windows XP
Visual FoxPro Version 6 & 9
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top