Jump to content

Recommended Posts

Posted (edited)

Hi,

I tried the following script to delete an empty files (size=0) from DelTest dir.

I get error "Wend" statement not match "While" statement.

I appreciate any help

Opt('TrayIconDebug', 1)

$search = FileFindFirstFile(@ScriptDir & "/DelTest/*.*")  
$i = 0
; Check if the search was successful
_main()
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
func _main()
while 1
    $file = FileFindNextFile($search) 
    $file_size = FileGetSize(@ScriptDir & "/DelTest/" & $file) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "/DelTest/" & $file)
        $i = $i +1
wend
EndFunc

; Close the search handle
Func _map()

EndFunc
FileClose($search)
Edited by coucou
Posted (edited)

TNX t0ddie,

I tested the script, it delete all the files without taking in account the size.

Where i'm wrong?

whats this?

$i = $i +1

EDIT: just wanted to say this function works just fine

$file = "delete.txt"
    $file_size = FileGetSize(@ScriptDir & "/" & $file) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "/" & $file)
endif
Edited by t0ddie

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

Posted

It does not delete any. Moreover it stop at

$file_size = FileGetSize(@ScriptDir & "/" & $file)

Here the final script

Opt('TrayIconDebug', 1)

; Check if the search was successful
_main()
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
func _main()
while 1
$file = FileFindFirstFile(@ScriptDir & "/DelTest/*.*") 
    $file_size = FileGetSize(@ScriptDir & "/" & $file) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "/" & $file)
endif
wend
EndFunc

; Close the search handle
Func _map()

EndFunc
FileClose($search)
Posted

here is what I see in your script...

Starts loop

finds first file

gets first file's size

if size is 0 delete the file

finds first file

gets first file's size....

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Posted

This what it should do! but it doesn't delete any :mellow:

What goes wrong with the script?

What I was saying is that each time it runs through the loop, it is only looking at the first file found (same file every time) and if that file is not 0 size, it will not delete, then it will check the same file again, and again, and again.

perhaps an array instead of filefindfirstfile (which should have a filefindnextfile)

another problem is that your "/" are going the wrong way they should be "\" for directories

func _main()
$file = _filelisttoarray(@ScriptDir & "\DelTest\", "*",1)
for $i=1 to UBound($file)-1 
    $file_size = FileGetSize(@ScriptDir & "\DelTest\" & $file[$i]) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "\DelTest\" & $file[$i])
endif
next
EndFunc

the above is an example using the array

it will run through each file 1 time in the folder and delete any that are 0 size. (not tested)

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Posted

add #include<array.au3> at the top of your code

still same error

Unkown function name

$file = _filelisttoarray(@ScriptDir & "\DelTest\", "*",1)
Posted

if you plan on checking folders that might be empty (no files)

then changing to this will keep the script from giving error

func _main()
$file = _filelisttoarray(@ScriptDir & "\DelTest\", "*",1)
if isarray($file) then ;checks to see if $file returned a list of files
for $i=1 to UBound($file)-1 
    $file_size = FileGetSize(@ScriptDir & "\DelTest\" & $file[$i]) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "\DelTest\" & $file[$i])
endif
next
endif
EndFunc

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Posted

It WORKSSSS

Many TNX Posted Image

Here the final final script

#Include <File.au3>

Opt('TrayIconDebug', 1)

$search = FileFindFirstFile(@ScriptDir & "/DelTest/*.*")  
; Check if the search was successful
_main()
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

func _main()
$file = _filelisttoarray(@ScriptDir & "\DelTest\", "*",1)
if isarray($file) then ;checks to see if $file returned a list of files
for $i=1 to UBound($file)-1 
    $file_size = FileGetSize(@ScriptDir & "\DelTest\" & $file[$i]) 
    if $file_size = 0 Then 
    FileDelete(@ScriptDir & "\DelTest\" & $file[$i])
endif
next
endif
EndFunc
Posted (edited)

Hi,

a new invented wheel with function of BugFix:

;#include <array.au3> ; if you want to see array uncomment
;folder to check for filenames
Global $checkdir = @ScriptDir & "\DelTest"
;array to hold filenames
Global $arfiles
;search folder recursively for filenames
$arfiles = _GetFilesFolder_Rekursiv ($checkdir, "*", 0)
;_ArrayDisplay ($arfiles) ; uncomment if you want to see array, -> remarks at #include.....

;Loop over array
For $i = 1 To UBound ($arfiles) - 1
    If FileGetSize ($arfiles [$i]) = 0 Then FileDelete ($arfiles [$i])          
Next


;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

;-))

Stefan

Edited by 99ojo
Posted (edited)

It does not delete any. Moreover it stop at

$file_size = FileGetSize(@ScriptDir & "/" & $file)

Here the final script

...

I am not sure you understand your code much. This code may help. Notice that FileFindFirstFile gets you just a handle for using with FileFindNextFile which you did not have in your script.

Look at this script changes and ask what you do not understand

Opt('TrayIconDebug', 1)

_main(@ScriptDir & "\DelTest")
If @error Then
    ; No files/directories matched the search pattern
EndIf

Func _main($path_to_search)
    Local $handle_search, $file_found, $dir_size, $file_size
    ; Returns a handle according to file search string (using FileFindFirstFile alias FFFF)
    $handle_search = FileFindFirstFile($path_to_search & '\*.*')
    ; Check if search string is valid
    If $handle_search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Return SetError(1)
    EndIf
    ; Use the handle to process the file search
    While 1
        ; FileFindNextFile gets the actual filename strings (use handle from FFFF)
        $file_found = FileFindNextFile($handle_search)
        ; @error means no more files to process so exit the loop
        If @error Then
            ExitLoop
        EndIf
        ConsoleWrite($path_to_search & "\" & $file_found & @CRLF)
        ; Check if a folder, else it is a file
        If StringInStr(FileGetAttrib($path_to_search & "\" & $file_found), "D") Then
            ; Recursion done here
            _main($path_to_search & "\" & $file_found)
            ; Remove 0 size folder
            $dir_size = DirGetSize($path_to_search & "\" & $file_found)
            If $dir_size = 0 Then
                DirRemove($path_to_search & "\" & $file_found)
            EndIf
        Else
            ; Remove 0 size file
            $file_size = FileGetSize($path_to_search & "\" & $file_found)
            ConsoleWrite($file_size & @crlf)
            If $file_size = 0 Then
                FileDelete($path_to_search & "\" & $file_found)
            EndIf
        EndIf
    WEnd
    ; Close handle from FFFF
    FileClose($handle_search)
EndFunc   ;==>_main

Func _map()
    ; something about map
EndFunc   ;==>_map

Edit:

Fixed issue with wild card in FileFindNextFile() which should be in FileFindFirstFile().

Edited by MHz

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
  • Recently Browsing   0 members

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