views:

800

answers:

4

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...

Hummm I'm confused but string+string does= string. What is going on?

+3  A: 

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');
zendar
+8  A: 

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.

mghie
+1 +A thanks this was the issue.
Arthur
+5  A: 

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.

Craig Stuntz
A: 

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.

Re0sless
No openfilelist is a listbox displaying the open files. openfiles is a array of a custom type that holds many values, id is one of them values and it is defined as string[8] and can be assinged any number between '0' and '99999999'. no actual files are touched here.
Arthur