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!

Delete a folder

Status
Not open for further replies.

paul1941

Programmer
May 25, 2006
32
BE
Hi all,

I want to delete an entire folder containing a database with his files and other files in one time and remove the name of the folder.

thanks

Paul
 
Hello,

Well you could 'run' out to an RD command with the options /S and /Q

Code:
m.instruction = 'RD /s/q "'+m.foldername+""
STRTOFILE(m.instruction,"c:\tst.bat")
RUN c:\tst.bat
it's pretty cruddy - sorry.

I *thnk* this could be done with a shellexecute, but I don't have time to work it out!

B-)



Regards

Griff
Keep [Smile]ing
 
Hi Mike

Isn't there a risk that the folder will have sub folders?

To do it that way, I think Paul would need to recurse all the subfolders... I have a way to do that, but it's not trivial.

There must be a shellexecute for shfileoperation technique that would work.

Martin

Regards

Griff
Keep [Smile]ing
 
And here, by no particular demand B-), is a non-trivial way of doing it in native VFP:

Code:
SET SAFETY OFF

IF DELETEFOLDER("c:\$incoming\testdir")
	MESSAGEBOX("Success",48,"Done")
ELSE
	MESSAGEBOX("Failed",48,"Not Done")
ENDIF



FUNCTION DELETEFOLDER
	** simple function that gets all the file and folder names (using the subfoldersearch function)
	** and then deletes them
	PARAMETERS m.FOLDERNAME
	PRIVATE m.FOLDERNAME,m.FLG
	M.FLG = .T.
	** gathers the names and folders into two tables (TMPFILES and TMPDIRS) in the c:\temp folder
	SUBFOLDERSEARCH(m.FOLDERNAME,"*.*","c:\temp\",.T.,"TMPFILES","TMPDIRS",.F.)
	SELECT 0
	USE C:\TEMP\TMPFILES
	SET ORDER TO
	SELECT TMPFILES
	GO TOP
	** scan the files in any order you like
	DO WHILE .NOT. EOF()
		** delete each file
		DELETE FILE (ALLTRIM(FULLNAME))
		SKIP
	ENDDO
	USE
	SELECT 0
	USE C:\TEMP\TMPDIRS
	SET ORDER TO
	GO BOTTOM
	** scan the folders in reverse order (deepest first)
	DO WHILE .NOT. BOF()
		IF DIRECTORY(ALLTRIM(FPATH))
			** if they are still there - remove the folder
			RMDIR (ALLTRIM(FPATH))
		ENDIF
		SKIP-1
	ENDDO
	USE
	IF DIRECTORY(m.FOLDERNAME)
		** if the folder is still there... you failed
		M.FLG = .F.
	ENDIF
	RETURN(m.FLG)



FUNCTION SUBFOLDERSEARCH
	PARAMETERS m.TOPLEVEL,m.MASK,m.TEMPDIR,m.PROGRESS,m.TEMPFILE,m.TMPDIRS,m.LEAVEOPEN
	PRIVATE m.TOPLEVEL,m.MASK,m.TEMPDIR,m.TEMPFILE,m.TMPDIRS,m.LEAVEOPEN,m.PROGRESS,I,X,m.LASTPATH,NUMFILES,NUMDIR
	PRIVATE m.OLDAREA
	DIMENSION ALIST[1,5],FLIST[1,5]
	IF PCOUNT() < 7
		M.LEAVEOPEN = .F.
	ENDIF
	IF PCOUNT() < 6
		M.TMPDIRS = "TMPDIRS"
	ENDIF
	IF PCOUNT() < 5
		M.TEMPFILE = "TMPFILES"
	ENDIF
	IF PCOUNT() < 4
		M.PROGRESS = .F.
	ENDIF
	IF PCOUNT() < 3
		M.TEMPDIR = "C:\TEMP\"
	ENDIF
	IF PCOUNT() < 2
		M.MASK = "*.*"
	ENDIF
	IF PCOUNT() < 1
		M.TOPLEVEL = ".\"
	ENDIF
	M.OLDAREA = SELECT()
	** CREATE A COUPLE OF TEMPORARY TABLES - ONE FOR THE FINAL FILENAME LIST (COMPLETE WITH PATHS
	SELECT 0
	CREATE TABLE (m.TEMPDIR+m.TEMPFILE) FREE (FULLNAME C(240) NOT NULL, FILESIZE N(12,0) NOT NULL, FILEDATE D NOT NULL, FILETIME C(8) NOT NULL, FILEATTR C(20) NOT NULL)
	USE (m.TEMPDIR+m.TEMPFILE) ALIAS TMPFILES EXCLUSIVE
	INDEX ON FULLNAME TAG TMPFILES
	** AND ONE FOR THE FOLDERS TO SEARCH
	SELECT 0
	CREATE TABLE (m.TEMPDIR+m.TMPDIRS) FREE (FPATH C(240) NOT NULL,PROCESSED C(1) NOT NULL)
	USE (m.TEMPDIR+m.TMPDIRS) ALIAS TMPDIRS EXCLUSIVE
	** USE AN INDEX TO KEEP CHECKING FOR UNPROCESSED SUBFOLDERS
	INDEX ON PROCESSED TAG TMPDIRS

	** START BY PUTTING THE 'WHERE TO START' IN LIST OF UNPROCESSED FOLDERS
	SELECT TMPDIRS
	SET ORDER TO TMPDIRS
	APPEND BLANK
	REPLACE FPATH WITH m.TOPLEVEL
	REPLACE PROCESSED WITH "N"
	GO TOP

	SELECT TMPDIRS
	SET ORDER TO TMPDIRS
	SEEK "N"
	** SCAN THE LIST OF SUBFOLDERS - LOOKING FOR UNPROCESSED ONES
	DO WHILE .NOT. EOF()
		** MAKE A NOTE OF THE FOLDERS PATH
		M.LASTPATH = ALLTRIM(FPATH)+IIF(RIGHT(ALLTRIM(FPATH),1)<> "\","\","")
		WAIT "Checking..." + m.LASTPATH WINDOW NOWAIT TIMEOUT 1
		** CHECK FOR FILES MATCHING MASK
		NUMFILES = ADIR(FLIST,m.LASTPATH+m.MASK)
		FOR I = 1 TO NUMFILES
			** IGNORE THE . AND .. ONES
			IF FLIST[i,1] <> "." .AND. FLIST[i,1] <> ".."
				** STICK THE FILES IN THE FILE LIST
				SELECT TMPFILES
				APPEND BLANK
				*REPLACE FPATH 	 WITH M.LASTPATH
				REPLACE FULLNAME WITH m.LASTPATH+FLIST[i,1]
				REPLACE FILESIZE WITH FLIST[i,2]
				REPLACE FILEDATE WITH FLIST[i,3]
				REPLACE FILETIME WITH FLIST[i,4]
				REPLACE FILEATTR WITH FLIST[i,5]
			ENDIF
		NEXT
		** MARK THAT FOLDER AS PROCESSED
		SELECT TMPDIRS
		REPLACE PROCESSED WITH "Y"
		** NOW CHECK THE FOLDER FOR SUBFOLDERS - HAVE TO DO THIS BECAUSE THE MASK
		** FOR FILES OF INTEREST WILL BE DIFFERENT TO THE ONE FOR FOLDERS
		NUMDIR = ADIR(ALIST,m.LASTPATH+"*.*","D")
		** FOR EACH FOLDER FOUND
		FOR I = 1 TO NUMDIR
			** IGNORE THE ONES THAT ARE NOT FOLDERS AND THAT ARE CALLED . OR ..
			IF "D"$ALIST[i,5] .AND. ALIST[i,1] <> "." .AND. ALIST[i,1] <> ".."
				SELECT TMPDIRS
				APPEND BLANK
				REPLACE FPATH WITH m.LASTPATH+ALIST[i,1]
				REPLACE PROCESSED WITH "N"
			ENDIF
		NEXT
		** LOOK FOR THE NEXT UNPROCESSED FOLDER
		WAIT CLEAR
		SELECT TMPDIRS
		SET ORDER TO TMPDIRS
		SEEK "N"
	ENDDO
	RELEASE FLIST
	RELEASE ALIST
	SELECT TMPDIRS
	USE
	SELECT TMPFILES
	IF !m.LEAVEOPEN
		USE
		SELECT(m.OLDAREA)
	ENDIF
	RETURN(m.TEMPDIR+m.TEMPFILE+IIF(UPPER(RIGHT(m.TEMPFILE,4)) <> ".DBF",".DBF",""))

Regards

Griff
Keep [Smile]ing
 
Martin,

You're correct about the need to recurse sub-folders. I didn't mention it because Paul didn't say he needed to do it.

I congratulate you on your impressive piece of code. However, I can think of a couple of easier ways of deleting a folder and all its subfolders. One way would be to RUN the DOS DELTREE command.

The other would be to use Windows Scripting Host (not the same as ShellExecute, that you mentioned):

Code:
ofs = CREATEOBJECT("Scripting.FileSystemObject")
odir = ofs.GetFolder("c:\MyFolder")
? odir.Delete

That will delete the folder and everything in it, including subfolders.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike,

It's not that impressive - it's mostly from the FAQ I wrote on searching subfolders, tucked into a wrapper. It's always good to reuse code!

I suggested the run command myself, but using RD /S/Q which does the same thing as deltree. The snag is, it's messy - flashing up a window.

Do all machines have the scripting host installed? Did I read somewhere that some corporates are removing it for 'security reasons'.

Regards

Griff
Keep [Smile]ing
 
Martin,

I've also read that some companies disable the Windows Scripting Host for security reasons, but I've never come across anyone who actually does that. I use WSH in all my apps, and don't recall it ever being a problem. I don't even know how to go about disabling it.

But no doubt someone will prove me wrong.

Paul: Are you still here? Your feedback on these suggestions would be appreciated.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I too haven't seen an actual example of the scripting being disabled... but I do have a client who disabled the right mouse buttons - thereby rendering excel context menus unusable - for the whole company (still don't get the rationale myself).

I *Think* I only have two apps using scripting, to install fonts for bar codes and the like.



Regards

Griff
Keep [Smile]ing
 
Hello all,

I'm using the following code

RD ((rstam)+'familie-boek\&selectiex') /S /Q

the folder and the 2 subfolders and all the files are deleted, even the folder name

thanks a lot

Paul
 
Cool, glad you able to get it sorted.

Watch out for the macro substitution in the middle there, and for folders with spaces in their names

Martin

Regards

Griff
Keep [Smile]ing
 
Hello all

I'm sorry, but with a new test this morning the following code doesn't work:

RD /S /Q ((rstam)+'familie-boek\&selectiex')

I want to do the following:

In my Familie-boek there is a folder SelectieX with at maximum 2 subfolders: Reserve and History

Selectiex contains a database and some other files, using delete database selectiex deletetables I can remove the database, but I want to remove in one time the files in the subfolders, the subfolders, the tables in the folder and the name of the folder.( at the moment I do the following cd to reserve del *.*, rd reserve ...)

How can I do it ?

Paul

 
Hi Paul,

Is it possible that there is an open file in one of the folders? That could stop you.

Also, I'm not sure why you have an ampersand in your code:

Code:
RD /S /Q ((rstam)+'familie-boek\&selectiex')

If you are doing this from VFP I would do something like this instead:

Code:
m.foldername = ((rstam)+'familie-boek\selectiex')
m.instruction = 'RD /s/q "'+m.foldername+""
STRTOFILE(m.instruction,"c:\tst.bat")
RUN c:\tst.bat



Regards

Griff
Keep [Smile]ing
 
Paul,

The code I showed you earlier will do that:

Code:
ofs = CREATEOBJECT("Scripting.FileSystemObject")
odir = ofs.GetFolder("c:\MyFolder")
? odir.Delete

But keep in mind Martin's point about administrators who disable Windows Scripting Host.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Griff,

&selectiex is the name of the folder ( in this case VONDEL )
How can I run a dod command without the black window appearing?

Paul
 
I don't *think* there is a way around the DOS window at least flashing up... which is why Mike's solution is generally better (or my native VFP code version).

It always looks a bit scruffy when your application flashes up a 'dirty DOS' box.

Martin

Regards

Griff
Keep [Smile]ing
 
Paul,

Just jumping in...I see that you're saying that the folder name is "&selectiex". I think that's going to cause problems because VFP of course uses the & to run a macro.

What is the actual error number & description that you get?


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hi Paul

I think that he's ok with this particular ampersand, because he is using it within quotations - although I would agree it seems an odd choice!

B-)

Regards

Griff
Keep [Smile]ing
 
Griff,

Er, it was Stewart who wrote the last post :)

Interesting. I did a few tests and found that that is correct except in the (OK rare) case of there being a character variable with the same name.

Try assigning a character value to a variable and then do ? [&myVar]. It doesn't actually execute what's in the string, but it does seem to return the string text.

Stewart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top