Deleting Folders & Files

BlitzPlus Forums/BlitzPlus Programming/Deleting Folders & Files

Sin of Nature(Posted 2003) [#1]
Hi,
Hi,

I've written a function that recurses a folder deleting files and folders (modified posting by theChange). Blitz+ Deleteing commands seem rather bugged. The routine simply scans for folders and files producing a list of files and a list of folders. Once the lists are produced, it deletes the files first. It is then supposed to delete the folders. The folder list is produced in the order so that a sub folder is never exists in the folder when DeleteDir() is called.

What I end up with is a few scattered files left over and folders that dont get deleted. Sure it does some of them. Under windows xp explorer I can press delete and they delete instantly, no read only warning or permissions issue. The lists that are produced are correct but the DeleteFile() and DeleteDir() functions seem bugged.

Appreciate if anyone would test this out. By the way, will Blitz+ delete readonly files and folders?

[CODE]
; #########################################
;
; F O L D E R R E M O V E R
;
; #########################################

; ---------------- TYPES -----------------

; File List
Type List
Field FileName$
End Type

; Directory List
Type DList
Field FileName$
End Type

; ---------------- TYPES -----------------

Global Files.List
Global Folders.DList

; ------------- MAIN PROGRAM -------------

ScanDir "C:\MyFolder" ; Scan Folder

Files.DList = First List

For Files.List = Each List ; Delete files
DebugLog(Files\FileName)
DeleteFile(Files\FileName)
Next

Folders.DList = First DList

For Folders.DList = Each DList ; Delete Folders
DebugLog(Folders\FileName)
DeleteDir(Folders\FileName)
Next

WaitKey()

End

; ----------- FUNCTIONS ----------------

Function ScanDir ( Dir$ )

;Print Dir ; Uncomment to see the directories being scanned
If Right ( Dir , 1 ) = "\" ; Dirspec trailing backslash
Dir = Left ( Dir , Len ( Dir ) - 1 )
End If


;- Directory scan
Local DirHandle = ReadDir ( Dir )
Local FileName$

Repeat
FileName = NextFile ( DirHandle )

If FileName <> ""

If FileName <> "." If FileName <> ".."

If FileType ( Dir + "\" + FileName ) = 2
; It's a directory!
Folders = New DList

; Inserts at front to ensure order is correct for deleting
Insert Folders Before First DList

; Add it to folder list
Folders\FileName = Dir + "\" + FileName

; Scan that folder
ScanDir Folders\Filename ; Go down one level
Else
; Its a file
Files = New List
; Add file to file list
Files\FileName = Dir + "\" + FileName
End If

End If

End If
Until FileName = "" ; No more files

CloseDir(Dir)

End Function
[/CODE]


Thanks

Sin


leeluna(Posted 2003) [#2]
you could add something like this into your deletefile loop

	 TempString$="Command /c ATTRIB -R "+ Chr(34) + FileName + Chr(34)
;	 This is a bit dodgy, but you can set the attributes of your directory so..
	 ExecFile(TempString$)
;	 This uses the Dos command ATTRIB to set the read only flag too off in your
;	 file


that may help out but not too sure.

Luna


Sin of Nature(Posted 2003) [#3]
I had thought of that approach but I really dont want command prompt windows opening. I could write this in a complex batch file otherwise.

I've just checked another persons delete folder routine from BlitzCoder and it has exactly the same problems as mine. The DeleteDir() and DeleteFile() commands seem flawed. The folders I'm deleting are quite big.


leeluna(Posted 2003) [#4]
This could work:-

Have Decls.

.lib "kernel32.dll"

api_SetFileAttributes% (lpFileName$, dwFileAttributes%) : "SetFileAttributesA"



Then add the following line to the file delete loop:-

api_SetFileAttributes(Files\FileName, 128)


Luna.


ShadowTurtle(Posted 2003) [#5]
Here is a small code, that delete directory files etc.:
Function DeleteDirectory(Dir_$)
  Local MyDir, MyFile$

  MyDir = ReadDir(Dir_$)
  MyFile$ = NextFile$(MyDir)
  MyFile$ = NextFile$(MyDir)
  Repeat
    MyFile$ = NextFile$(MyDir)
    If MyFile$ = "" Then Exit

    If FileType(Dir_$ + "\" + MyFile$) = 2 Then
      DeleteDir Dir_$ + "\" + MyFile$
      DeleteDirectory(Dir_$ + "\" + MyFile$)
    ElseIf FileType(Dir_$ + "\" + MyFile$) = 1 Then
      DeleteFile Dir_$ + "\" + MyFile$
      CopyFile Dir_$ + "\" + MyFile$
    End If
  Forever
  CloseDir(MyDir) 
End Function


http://www.shadowturtle.de/sites/shadowturtle/page.php?portal=blitzinfo&page=page_Codes_DeleteDirectory


Sin of Nature(Posted 2003) [#6]
Is that CopyFile command supposed to be there?!?

Sin


ShadowTurtle(Posted 2003) [#7]
oh. Here is the correct code. Sorry:

Function DeleteDirectory(Dir_$)
  Local MyDir, MyFile$

  MyDir = ReadDir(Dir_$)
  MyFile$ = NextFile$(MyDir)
  MyFile$ = NextFile$(MyDir)
  Repeat
    MyFile$ = NextFile$(MyDir)
    If MyFile$ = "" Then Exit

    If FileType(Dir_$ + "\" + MyFile$) = 2 Then
      DeleteDir Dir_$ + "\" + MyFile$
      DeleteDirectory(Dir_$ + "\" + MyFile$)
    ElseIf FileType(Dir_$ + "\" + MyFile$) = 1 Then
      DeleteFile Dir_$ + "\" + MyFile$
    End If
  Forever
  CloseDir(MyDir) 
End Function



Mr.Waterlily(Posted 2003) [#8]
Hi,

Iīve also done a "DelTree" function in blitz.
And it took me a while to figure out why DeleteDir just didnīt remove certain directories.
My solution was that I inserted a CloseDir just before every new ReadDir and not just in the end of the function. I guess that having lots of opened ReadDir handles is NOT good ;).

Try it!

//Andreas..