Jump to content

Size Comparison


 Share

Recommended Posts

Ah sorry, i misunderstood.. I thought you wanted to keep at least one file.

So it's over complicated to do it with the same method, especially if you want to have a comprehensible code.

A good way should be to use _FileListToArray first, then create a 2D array containing the file names and file size. Next, _ArraySort the array by file size, and finally, make a loop in the whole array and FileDelete each file having the same size.

Here is an example of what could be done with my previous method, but it will me more complicated to understand than my first code (sorry all, it's a jumble)

Local $sPath = "D:\tmp"
Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

Local $sSizes

While 1
    $sFile = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    If @extended Then ContinueLoop
    
    $iSize = FileGetSize($sPath & "\" & $sFile)
    $aDouble = StringRegExp($sSizes, ";([^,]*)," & $iSize & ";", 1)
    If IsArray($aDouble) Then
        If $aDouble[0] <> "" Then
            $sSizes = StringReplace($sSizes, ";" & $aDouble[0] & ",", ";,")
            ; => FileDelete the first file
            ConsoleWrite("FileDelete('" & $sPath & "\" & $aDouble[0] & "')" & @CRLF)
        EndIf
        ; => FileDelete the next file
        ConsoleWrite("FileDelete('" & $sPath & "\" & $sFile & "')" & @CRLF)
    Else
        $sSizes &= $sFile & "," & $iSize & ";"
    EndIf
WEnd

FileClose($hSearch)

 

Edited by jguinch
Link to comment
Share on other sites

I tried it, and it doesn't work.. I will try to make the array thingy now; how do I put the size in the array ? why is there always some 0 in my array which is 3906 ?

okay so i found out there is arrayadd and the 0 is the amount of things i got in there

arrayadd just adds something to an array, no dimension .. q-q

I found the array wiki article, maybe i should have read that.. well, I will tomorrow. thanks for helping me to get so far!

 

Edited by Siryx
Link to comment
Share on other sites

Siryx,

 

jguinch's example modified slightly.  See comments in code...

#include <array.au3>

Local $sPath = "k:\temp"        ;   path name
local $aFiles[10000][2]         ;   array to hold files col 0 = filename, col 1 = size
local $idx = 0                  ;   index to offset array while processing files

Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

; find all files in path and populate array with fully qualified name and size

While 1
    $aFiles[$idx][0] = $sPath & '\' & FileFindNextFile($hSearch)
    If @error Then ExitLoop

    If @extended Then ContinueLoop

    $aFiles[$idx][1] = FileGetSize($aFiles[$idx][0])
    $idx += 1
WEnd



FileClose($hSearch)             ; close search handle

redim $aFiles[$idx][2]          ; trim array to size actually used (count is in $idx)

; sort by size ascending

_arraysort($aFiles,0,0,0,1)

_arraydisplay($aFiles)

; iterate array deleting duplicately sized files

for $1 = 1 to ubound($aFiles) - 1
    if $aFiles[$1][1] = $aFiles[$1-1][1] then
        ConsoleWrite('delete file = ' & $aFiles[$1][0] & ' size = ' & $aFiles[$1][1] & @CRLF)
    EndIf

next

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Siryx,

 

jguinch's example modified slightly.  See comments in code...

#include <array.au3>

Local $sPath = "k:\temp"        ;   path name
local $aFiles[10000][2]         ;   array to hold files col 0 = filename, col 1 = size
local $idx = 0                  ;   index to offset array while processing files

Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

; find all files in path and populate array with fully qualified name and size

While 1
    $aFiles[$idx][0] = $sPath & '\' & FileFindNextFile($hSearch)
    If @error Then ExitLoop

    If @extended Then ContinueLoop

    $aFiles[$idx][1] = FileGetSize($aFiles[$idx][0])
    $idx += 1
WEnd



FileClose($hSearch)             ; close search handle

redim $aFiles[$idx][2]          ; trim array to size actually used (count is in $idx)

; sort by size ascending

_arraysort($aFiles,0,0,0,1)

_arraydisplay($aFiles)

; iterate array deleting duplicately sized files

for $1 = 1 to ubound($aFiles) - 1
    if $aFiles[$1][1] = $aFiles[$1-1][1] then
        ConsoleWrite('delete file = ' & $aFiles[$1][0] & ' size = ' & $aFiles[$1][1] & @CRLF)
    EndIf

next

kylomas

​after the arraydisplay theres nothing going on, no matter what I press. I tried to disable the arraydisplay by

;_arraydisplay($aFiles)

but it does not delete a file... :/ I still try to figure out how to do array with 2 dimensions..

redim $aFiles[$idx][2]

this?

 

Link to comment
Share on other sites

Nothing happens because kylomas and I used ConsoleWrite to show the FileDelete step. You have to add the FileDelete line to your code, in place of ConsoleWrite :

FileDelete($aFiles[$1][0])  ; for kylomas's code
FileDelete($sPath & "\" & $sFile)  ; for my code

 

Link to comment
Share on other sites

Nothing happens because kylomas and I used ConsoleWrite to show the FileDelete step. You have to add the FileDelete line to your code, in place of ConsoleWrite :

FileDelete($aFiles[$1][0])  ; for kylomas's code
FileDelete($sPath & "\" & $sFile)  ; for my code

 

consolewrite doesn't work for me, not in the array tutorials nor here; it's writing nothing anywhere; is that intended?

so kylomas code only deletes 2 files and if there are more than 2 it leaves one over (yes my folder is messy af >.<) and i'm trying out yours now

same... I don't know. So I tried to run them multiple times but yeah then it leaves one over.. maybe I should do it manually ._____.

Local $sPath = "D:\Musik - Kopie2"
Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

Local $sSizes

While 1
    $sFile = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    If @extended Then ContinueLoop

    $iSize = FileGetSize($sPath & "\" & $sFile)
    $aDouble = StringRegExp($sSizes, ";([^,]*)," & $iSize & ";", 1)
    If IsArray($aDouble) Then
        If $aDouble[0] <> "" Then
            $sSizes = StringReplace($sSizes, ";" & $aDouble[0] & ",", ";,")
            ; => FileDelete the first file
            FileDelete($sPath & "\" & $sFile)
        EndIf
        ; => FileDelete the next file
        FileDelete($sPath & "\" & $sFile)
    Else
        $sSizes &= $sFile & "," & $iSize & ";"
    EndIf
WEnd

FileClose($hSearch)
#include <array.au3>

Local $sPath = "D:\Musik - Kopie2"      ;   path name
local $aFiles[10000][2]         ;   array to hold files col 0 = filename, col 1 = size
local $idx = 0                  ;   index to offset array while processing files

Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

; find all files in path and populate array with fully qualified name and size

While 1
    $aFiles[$idx][0] = $sPath & '\' & FileFindNextFile($hSearch)
    If @error Then ExitLoop

    If @extended Then ContinueLoop

    $aFiles[$idx][1] = FileGetSize($aFiles[$idx][0])
    $idx += 1
WEnd



FileClose($hSearch)             ; close search handle

redim $aFiles[$idx][2]          ; trim array to size actually used (count is in $idx)

; sort by size ascending

_arraysort($aFiles,0,0,0,1)

_arraydisplay($aFiles)

; iterate array deleting duplicately sized files

for $1 = 1 to ubound($aFiles) - 1
    if $aFiles[$1][1] = $aFiles[$1-1][1] then
        FileDelete($aFiles[$1][0])
    EndIf

next

Edited by Siryx
Link to comment
Share on other sites

 

#include <array.au3>

Local $sPath = "D:\Musik - Kopie2"      ;   path name
Global $aFiles[10000][2]            ;   array to hold files col 0 = filename, col 1 = size
local $idx = 0                  ;   index to offset array while processing files

Local $hSearch = FileFindFirstFile($sPath & "\*.*")
If $hSearch = -1 Then Exit

; find all files in path and populate array with fully qualified name and size

While 1
    $aFiles[$idx][0] = $sPath & '\' & FileFindNextFile($hSearch)
    If @error Then ExitLoop

    If @extended Then ContinueLoop

    $aFiles[$idx][1] = FileGetSize($aFiles[$idx][0])
    $idx += 1
WEnd



FileClose($hSearch)             ; close search handle

redim $aFiles[$idx][2]          ; trim array to size actually used (count is in $idx)

; sort by size ascending

_arraysort($aFiles,0,0,0,1)

; iterate array deleting duplicately sized files

for $1 = 1 to ubound($aFiles) - 1
    if $aFiles[$1][1] = $aFiles[$1-1][1] then
        FileDelete($aFiles[$1][0])
        FileDelete($aFiles[$1-1][0])
    EndIf
$aFiles =
next

 

would this delete both files $1 and $1-1 which are identical  ?

€: it worked! - had to delete the

$aFiles =

though

 

fc4a2510775b8c5b13c349908d11bb8d[1].jpg

its not even my code but hey

 

Edited by Siryx
Link to comment
Share on other sites

Siryx,

 

Not sure where you got the

$aFiles =

from it was not in the code I posted.

As far as consolewrite's are concerned.  Are you running this code from SciTE?

Regarding 2D arrays, look at them like spread sheets.  In this case the file name would be in the "A" column and the size would be in the "B" column.  AutoIt offsets arrays from 0 so a reference to $aFiles[4][1] will give you the size of the fifth file.  In the posted code the loop increment variable ($1) is used to point at the row, the columns are referenced explicitly.

The Wiki has several informative tutorials concerning arrays and other AutoIt subjects.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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