Jump to content

FileDelete and DirRemove problem


Borje
 Share

Recommended Posts

Is there anybody can help me I placed this code in a program before I exit the program when ended the program

FileDelete(@TempDir & "\Test.ini")

FileDelete (@TempDir & "\English.lng")

DirRemove (@TempDir & "\Test",1)

Exit

When I run my code and when I Exit the code I want this to work.. Sometimes it works but not every time why ?

Link to comment
Share on other sites

Is there anybody can help me I placed this code in a program before I exit the program when ended the program

FileDelete(@TempDir & "\Test.ini")

FileDelete (@TempDir & "\English.lng")

DirRemove (@TempDir & "\Test",1)

Exit

When I run my code and when I Exit the code I want this to work.. Sometimes it works but not every time why ?

Try with @error to see if you get an error.

When the words fail... music speaks.

Link to comment
Share on other sites

Thanks Andreik

I have try the @Error but I have no error and it still not working.

Do a simple test. Check with FileExists() if the file exist.

And then something like this:

While FileExists(@TempDir & "\Test.ini") Then
FileDelete(@TempDir & "\Test.ini")
Wend

EDIT:

Or you can use DOS:

#include <Process.au3>
_RunDOS('del "' & @TempDir & '\Test.ini' & '"')

Hope that helps and work fine. :)

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Ok I should have put in some proper error checking and a timeout, try this

$sDir = @TempDir
$sFiles = "Test.ini,English.lng,\Test"
$aFiles = StringSplit($sFiles, ",")
For $j = 1 To $aFiles[0]
    $i = 50
    While $i > 0
        If StringLeft($aFiles[$j], 1) = "\" Then
            If DirRemove($sDir & $aFiles[$j]) Then ExitLoop
            If FileExists($sDir & $aFiles[$j] & "\*.*") Then
                If Not FileDelete($sDir & $aFiles[$j] & "\*.*") Then
                    MsgBox(262160, "Delete Error", "Error deleting files in folder " & $aFiles[$j])
                    ExitLoop
                EndIf
            EndIf
        Else
            If FileDelete($sDir & '\' & $aFiles[$j]) Then ExitLoop
        EndIf
        Sleep(100)
        $i -= 1
        If $i = 0 Then
            MsgBox(262160, "Delete Error", "Error deleting " & $aFiles[$j] & "  @error=" & @error)
            ExitLoop
        EndIf
    WEnd
Next
Either a file must still be open or being accessed by your AutoIt app or some other app or folder Test is not empty.

Edited by picaxe
Link to comment
Share on other sites

  • 3 years later...

I am also struggling with this

I am writing a program that keeps a directory size below a limit by deleting the oldest file:

while dirgetsize($targetdir) > $max

find the oldest file $fold

filedelete($fold)

wend

the oldest file does not get deleted this way, it is still there.

I checked attributes.

Why is this such a problem ? Anybody has a suggestion ?

Link to comment
Share on other sites

  • Moderators

silver217,

Can you please post the full code you have tried - your logic seems fine so it might be a syntax problem. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I don't know how to put code in this forum as a popup so here it is as text:

While 1
    dodir($targetdir)
    tolog("=====================================================")
    tolog('fold = ' & $fold)
    $d = InputBox('Delete ?', $fold & @CRLF & $attrib & @CRLF & $tz)
    If $d <> 1 Then
        Exit
    Else
        tolog($attrib)
        If $attrib = ' Directory' Then
;           DirRemove($fold)
        Else
            While Not FileDelete($fold)
                Sleep(100)
            WEnd
        EndIf
    EndIf
WEnd
; log_to_screen()
Exit

Func dodir($p)
    FileChangeDir($p)
    ;tolog("dir: " & @WorkingDir)
    $search = FileFindFirstFile("*.*")
    If $search = -1 Then
        ;   tolog("   Empty dir !")
        ;   Exit
        ;   EndIf
    Else
        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            ;   tolog($file)
            If StringInStr(FileGetAttrib($file), "S") Or StringInStr(FileGetAttrib($file), "H") Or StringInStr(FileGetAttrib($file), "D") Then
                ;       tolog("S H or Dir: " & $file)
            EndIf
            If StringInStr(FileGetAttrib($file), "D") Then gosub($file)
            $t = FileGetTime($file, 0, 1) ; date modified = 0
            If $t < $oo Then
                $oo = $t
                $fold = $p & '' & $file
            EndIf
        WEnd
    EndIf
    FileClose($search)
    ;tolog("dir: " & @WorkingDir)
    ;tolog("--------------------------------------------------------")

    $attrib = FileGetAttrib($fold)
    If @error Then
        ;   tolog('Error: Could not obtain attributes of oldest file: ' & $fold)
    Else
        $input = StringSplit("R,A,S,H,N,D,O,C,T", ",")
        $output = StringSplit("Read-only /, Archive /, System /, Hidden /" & _
                ", Normal /, Directory /, Offline /, Compressed /, Temporary /", ",")
        For $i = 1 To 9
            $attrib = StringReplace($attrib, $input[$i], $output[$i], 0, 1)
            ; last parameter in StringReplace means case-sensitivity
        Next
        $attrib = StringTrimRight($attrib, 2) ;remove trailing slash
        ;   tolog("oldest: " & $fold & ", attributes: " & $attrib)
    EndIf
EndFunc   ;==>dodir

Func gosub($sub)
    ;tolog('subdir: ' & $sub)
    $oldwd = @WorkingDir
    dodir(@WorkingDir & "" & $sub)
    FileChangeDir($oldwd)
EndFunc   ;==>gosub
Edited by silver217
Link to comment
Share on other sites

  • Moderators

silver217,

I don't know how to put code in this forum as a popup

You put [autoit] before and [/autoit] after your code - which I am looking at now. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

silver217,

I am not debugging that code - it is incomplete and I hate debugging recursive calls to FileFindFirst/NextFile. Please read the Recursion tutorial in the Wiki to see why - and to find out a better way to do it. :oops:

Here is a function I just threw together using my RecFileListToArray UDF (look in my sig for the link). I have tested it a few times and it seems to work fine:

#include <RecfileListToArray.au3>
#include <Array.au3>

; Set the root path
$sRoot = "N:Sizer"

; Get a list of files including in subdirs with full path
$aFileList = _RecFileListToArray($sRoot, "*", 1, 1, 0, 2)
; Now create an array of the filetimes and fill it
Global $aTimes[$aFileList[0] + 1] = [$aFileList[0]]
For $i = 1 To $aFileList[0]
    $aTimes[$i] = Number(FileGetTime($aFileList[$i], 0, 1))
Next

; Just for testing
_ArrayDisplay($aFileList, "Start")
_ArrayDisplay($aTimes, "Start")

; This is the folder size at the start
MsgBox(0, "Initial Size", DirGetSize($sRoot))

; Set a suitable size here
While DirGetSize($sRoot) > 20000000

    ; Reset the initial values
    $iOldest = 99999999999999
    $sOldest = ""
    $iIndex = 0
    ; Now go through the times to find the oldest
    For $i = 1 To $aTimes[0]
        If $aTimes[$i] < $iOldest Then
            $iOldest = $aTimes[$i]
            $sOldest = $aFileList[$i]
            $iIndex = $i
        EndIf
    Next
    ; And delete the file
    FileDelete($sOldest)
    ; Make sure it has gone
    Do
        Sleep(10)
    Until Not FileExists($sOldest)
    ; And remove it from the arrays
    _ArrayDelete($aFileList, $iIndex)
    $aFileList[0] -= 1
    _ArrayDelete($aTimes, $iIndex)
    $aTimes[0] -= 1

    ; Just to check
    _ArrayDisplay($aFileList, "In loop")

WEnd

; And this is the folder size at the end
MsgBox(0, "Final Size", DirGetSize($sRoot))

It should not be too hard to amend it to fit your script, but do come back if you run into problems. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

silver217,

I did tell you in the post above: :oops:

my RecFileListToArray UDF (look in my sig for the link)

Or you can go directly to it by clicking here. :D

M23

Edit: Added direct link. :rip:

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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