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

Delete files read only

Status
Not open for further replies.

Holm78

Programmer
Mar 5, 2002
60
SE
Hi all

I have this module that I use to delete files and libraries.

I have one problem:

If the file is writeprotected Foxpro says acces denied.

Is there a way to delete the file/files anyways?

Thanks
Michael
 
Holm78
lcFileName = [C:\MyApp\test.prg]
! ATTRIB -R &lcFileName && Change atrributes to read/write
DELE FILE (lcFileName)

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Holm78

I would suggest to change the file attribute, before you try to delete. There is an API call that does that. Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
This the WinAPI call, courtesy of
LPARAMETERS lpFileName

#DEFINE FILE_ATTRIBUTE_READONLY 1
#DEFINE FILE_ATTRIBUTE_HIDDEN 2
#DEFINE FILE_ATTRIBUTE_SYSTEM 4
#DEFINE FILE_ATTRIBUTE_DIRECTORY 16
#DEFINE FILE_ATTRIBUTE_ARCHIVE 32
#DEFINE FILE_ATTRIBUTE_NORMAL 128
#DEFINE FILE_ATTRIBUTE_TEMPORARY 512
#DEFINE FILE_ATTRIBUTE_COMPRESSED 2048

DECLARE SHORT SetFileAttributes IN kernel32;
STRING lpFileName, INTEGER dwFileAttributes

DECLARE INTEGER GetFileAttributes IN kernel32 STRING lpFileName

* read current attributes for this file
dwFileAttributes = GetFileAttributes (lpFileName)

IF dwFileAttributes = -1
* the file does not exist
RETURN
ENDIF

IF dwFileAttributes > 0
* read-only attribute to be set
dwFileAttributes = BitOr(dwFileAttributes, FILE_ATTRIBUTE_READONLY)

* archive attribute to be removed
IF BitAnd(dwFileAttributes, FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE
dwFileAttributes = dwFileAttributes - FILE_ATTRIBUTE_ARCHIVE
ENDIF

* setting selected attributes
= SetFileAttributes (lpFileName, dwFileAttributes)
ENDIF
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Thanks Cris that worked for me...

But now I have another problem...since the folders is userprofiles in a windows network it contains subfolders and files in them.

Is there a way to check in the userprofile directory if there is any subfolders or not and the if there is go in and delete files and folders

Now I use this code

SET DEFA TO "c:\test\"
= ADIR(array,'*.*')
filename = array(1, 1)
if !empty(filename)
FOR i = 1 TO ALEN(array,1)
filename = array (i,1)
scan
if file(location + filename)
! ATTRIB -R &filename
erase addbs(location + filename)
endif
endscan
endfor
endif

It may look amatuerish since I am a beginner.
So how can I find if there is any directories??

thanks
Michael



 
Michael

ADIR(array,'*.*',[D])

will list the folders as well.

'ARRAY' is a reserved word in VFP, so I suggest you use an alternative word for your array name.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Michael

If you want to hide the DOS window :-

DECLARE INTEGER ShellExecute IN shell32.dll ;
[tab]INTEGER hndWin ,;
[tab]STRING lcAction ,;
[tab]STRING lcFileName ,;
[tab]STRING lcParams ,;
[tab]STRING lcDir ,;
[tab]INTEGER lnShowWin

lcFiles = [*.*]
lcFileName = [ATTRIB]
lcParams = [-R -H &lcFiles]
lcAction = [open]

ShellExecute(0 ,;
[tab]lcAction ,;
[tab]lcFileName ,;
[tab]lcParams ,;
[tab][] ,;
[tab]0)


In this example, the Hidden attribute is also being removed.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
This thread begs someone to write a FAQ on how to replicate DELTREE (or RD /S ) in VFP, with and without auto-deleting ReadOnly files.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top