Jump to content

Files Not Deleting


Recommended Posts

Trying to delete all files from a dir that this is running in - cannot get anything to delete though !!

Do I need a FileFindNextFile somewhere ?

$t = 0
$file = FileFindFirstFile("*.*")
while $t = 0
If $file = "keep me" Then
    ContinueLoop
Else
    FileDelete($file)
EndIf
$t = $t + 1
WEnd
FileClose($file)
Link to comment
Share on other sites

Do I need a FileFindNextFile somewhere ?

yes, otherwise $file will always be the same. see help file samples of FileFindNextFile().

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Got a bit further, it now deletes files but not files in the directories or the directories - anyone got any suggestions......?

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
$t = 1
While $t = 1
    $file = FileFindNextFile($search) 
    If $file = "Keep me" Then ContinueLoop
    FileDelete($file)
    $t = $t + 1 
WEnd
FileClose($search)

I need a Dirremove in it somewhere too......me thinks....

yes, otherwise $file will always be the same. see help file samples of FileFindNextFile().

Cheers

Kurt

<{POST_SNAPBACK}>

Edited by MattX
Link to comment
Share on other sites

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
$t = 1
While $t = 1
    $file = FileFindNextFile($search) 
    If $file = "keep me" Then ContinueLoop
    DirRemove($file, 1)
    $t = $t + 1 
WEnd
FileClose($search)

Whoops, it deletes everything !! I need it to keep the 'keep me' folder...

Good thing I tested it on a dummy account first.. :)

Link to comment
Share on other sites

Someone please point to me my error here - I thought with a continue loop it would just find the file / folder keep me and then carry on deleting everything else but not 'keep me' - where am I going wrong ?

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
$t = 1
While $t = 1
    $file = FileFindNextFile($search) 
    If $file = "keep me" Then ContinueLoop
    DirRemove($file, 1)
    $t = $t + 1 
WEnd
FileClose($search)

Whoops, it deletes everything !! I need it to keep the 'keep me' folder...

Good thing I tested it on a dummy account first.. :)

<{POST_SNAPBACK}>

Link to comment
Share on other sites

FileFindNextFile will return "." and ".."

... make sure you ignore these. They represent up 1 folder and the current folder...

Lar.

<{POST_SNAPBACK}>

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
$t = 1
While $t = 1
    $file = FileFindNextFile($search) 
    If $file = "keep me" Then ContinueLoop
    If $file = "." Then ContinueLoop
    If $file = ".." Then ContinueLoop
    DirRemove($file, 1)
    $t = $t + 1 
WEnd
FileClose($search)

If I add those to exceptions Lar and run it then it does not delete anything. Now I'm really confused !!

I just commented them out and it goes back to what it did before - which was deleting everything.

Link to comment
Share on other sites

Hang on, its doing something, its deleting some of the stuff as to start with in my test folder there was 48 meg and 486 files, when this is run there is 6 meg and only 102 files, can I assume its doing this in some sort of order and then stopping ?

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
$t = 1
While $t = 1
    $file = FileFindNextFile($search) 
    If $file = "keep me" Then ContinueLoop
    If $file = "." Then ContinueLoop
    If $file = ".." Then ContinueLoop
    DirRemove($file, 1)
    $t = $t + 1 
WEnd
FileClose($search)

If I add those to exceptions Lar and run it then it does not delete anything. Now I'm really confused !!

I just commented them out and it goes back to what it did before - which was deleting everything.

<{POST_SNAPBACK}>

Link to comment
Share on other sites

As usual Larry you are the expert, something I've been working on for a few hours, you knock up in about 5 mins !!

Many many thanks - I am now gonna work out what the:

If StringInStr(FileGetAttrib(@SCRIPTDIR & "\" & $file) , "D")

does now.

I am not worthy.....

DirRemove is for folders... try this...

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile(@SCRIPTDIR & "\*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error <> 0 Then ExitLoop
    If $file = "keep me" Then ContinueLoop
    If $file = "." Or $file = ".." Then ContinueLoop
    If StringInStr(FileGetAttrib(@SCRIPTDIR & "\" & $file) , "D") Then
        DirRemove(@SCRIPTDIR & "\" & $file, 1)
    Else
        FileDelete(@SCRIPTDIR & "\" & $file)
    EndIf
WEnd
FileClose($search)

<{POST_SNAPBACK}>

Edited by MattX
Link to comment
Share on other sites

I have just checked that out - I would not have thought in a million years to use a string and then a file attrib to work out its a folder etc.

I guess if you don't [ like me ] use strings a great deal you just don't understand how they can work for you etc. Again many thanks, hopefully this will embed in my brain for future problems I may come up with.

Again many thanks.

If StringInStr(FileGetAttrib(@SCRIPTDIR & "\" & $file) , "D") Then
means if it is a folder...

<{POST_SNAPBACK}>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...