Arthur
Arthur

Reputation: 3418

Deleting a temporary directory

I have this code,

showmessage('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID);
if removedir('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID) then
  showmessage('Removed')
else
  showmessage('Failed');

The message shows C:\TEMP\0 and this directory does exist as the program created it earlier and used files inside it and then later deletes them. I can see the files and directories so I know they're there. The program successfully deletes the files but does not remove the directory.

If I hardcode the directory it works - this means that it accepts the string C:\TEMP\0 but does not accept C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID both equate to C:\TEMP\0. I cannot hardcode these directories, so what can I do? How do I convert from a string + string to whatever removedir() is expecting. I looked this up at Delphi basics and it's expecting a string.

I'm confused, since string + string = string. What is going on?

Upvotes: 1

Views: 2180

Answers (4)

Re0sless
Re0sless

Reputation: 10886

What type of objects are openfiles and openfilelist?

Do they open folders at all, if so they may still be open when your trying to delete the folder.

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126547

In addition to the other good answers, you should not be storing your temp folder in C:\TEMP. Use the value returned from GetTempFilename, instead. Unlike C:\TEMP, this location (which varies by operating system) will work on all operating systems, and all levels of user access control. This also eliminates the risk that the location you have hardcoded might also be hardcoded into another system.

Upvotes: 8

mghie
mghie

Reputation: 32334

Make sure that neither your program nor any other program have the directory as their current working directory. When you recompile the program this may no longer be the case, so it may be a red herring that the hardcoded value works for you.

Upvotes: 8

zendar
zendar

Reputation: 13562

If I understood correctly, openfiles[openfilelist.ItemIndex].ID is a string that contains number?
If so, did you check that it does not contain blanks? Something like this:

filename := 'C:\TEMP\' + trim(openfiles[openfilelist.ItemIndex].ID);
showmessage(filename);
if removedir(filename) then
   showmessage('Removed')
else
   showmessage('Failed');

Upvotes: 3

Related Questions