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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble deleting file 2

Status
Not open for further replies.

coldan

Programmer
Oct 19, 2008
98
AU
In my application I use the Imageviewer ocx control to (amongst other things) allow additional meta data to be written to a jpg file.

The control requires a temp file to be written with the new Exif data included.

My problem is that the original image file is used in a field in a grid that I want to keep open ( to allow a succession of images to be changed if required)

So with my code where mycopyfilename is a full path to the image

<code>

If File(mycopyfilename)

mycopysaver = juststem(mycopyfilename)+'.bak'

CopyFile (mycopyfilename , mycopysaver , 1)

delfile = Justfname(mycopyfilename)

{neither of these commands delete the file}

Delete File (delfile)

Delete File &delfile

{so I can't do }

CopyFile (myorigfilename , mycopycopy, 1) && using the API

&& Testing

If File(myorigfilename)

cMessageText = 'Overwrite success!'

Messagebox(cMessageText, 0 + 48 ,cMsgTitle)

Else

cMessageText = 'Overwrite failed!'

Messagebox(cMessageText, 0 + 48 ,cMsgTitle)

Endif

Endif

</code>

Has anybody any suggestions?

Thanks

Coldan
 
Coldan,

Your problem is this line:

delfile = Justfname(mycopyfilename)

DELETE FILE does not look in the search path for the file to be deleted. The file must either be in the default directory, or it must include the full directory path.

If you remove the JUSTFNAME() from around the filename, it should work.

Another point: Check that the file is not open, either in your application or elsewhere. DELETE FILE cannot delete an open file.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Coldan,
(your code is a little mess where it is not clear which one is a field variable and which one is a memory variable - I strongly recommend to prefix memory variables with m.)

1) Be explicit about where you want to save your backup. It is unlikely that you would want to save current directory (which under Vista and later have a high chance to be readonly).

2) If you use FailIfExist parameter as false then you might be skipping to save the 'latest' backup of a file.

3) I find 'Erase' better than 'Delete file' by usage.

Assuming you would want to save to a folder named "c:\MyImageBackups":

Code:
If File(mycopyfilename)
  mycopysaver = ;
     Forcepath( ;
     Forceext(mycopyfilename,'.bak'), ;
       'c:\MyImageBackups' )

  If CopyFile(mycopyfilename , m.mycopysaver , 0) <> 0
    Erase (mycopyfilename)
  Endif
Endif


Cetin Basoz
MS Foxpro MVP, MCP
 
Alternative code using standard VFP commands and which should work might be something like:

Code:
SET SAFETY OFF  && Enable 'blind' over-write

cMyCopyFilename = <Fully Pathed File Name>  && Such as "F:\MyFileDir\MyCopyFile.txt"

IF FILE(cMyCopyFilename)
   * --- Copy File Already Exists ---
   cMyCopySaver = STRTRAN(cMyCopyFilename,'.txt','.bak')

   COPY FILE (cMyCopyFilename) TO (cMyCopySaver)  && Use Fully Pathed File Names Here

   cDelfile = cMyCopyFilename  && Fully Pathed File Name

   ERASE (cDelfile)  && Use Fully Pathed File Names Here

   < and then do whatever >
ELSE  && IF FILE(cMyCopyFilename)
   * --- Copy File Does Not Exist ---
   < do whatever >
ENDIF  && IF FILE(cMyCopyFilename)

Good Luck,
JRB-Bldr


 
I didn't say they are not identical, did I? All I said I find it better usage wise. That is a personal preference not a technical.

Cetin Basoz
MS Foxpro MVP, MCP
 
This is just a general comment, not necessarily your problem here. Depending on the filename's path, especially if it is on a network or in certain areas of the local computer, the folder rights may have security settings restricting or blocking deletion and other user actions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top