Jump to content

Cleanup Downloads folder


MattHiggs
 Share

Recommended Posts

I download files a lot and end up with a lot of duplicate downloaded files with the appended "(n)" where n is a digit.  I wrote this script to scan the downloads folder for files which have the aforementioned sub-string in the file name and, if there is a file with the same name, but without the aforementioned sub-string, the script will compare the time stamps of the two files, delete the older one, and if the file with the appended sub-string is the newer one, will rename it so that the file name no longer has the sub-string (so it will be named the same as the old file that was deleted.  The end product is a folder that (as far as file names are concerned) that has no duplicate names (meaning no "(n)").  Pretty simple, but here is the code anyway:

; *** Start added by AutoIt3Wrapper ***
#include <FileConstants.au3>
; *** End added by AutoIt3Wrapper ***
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.15.0 (Beta)
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <File.au3>
#include <Date.au3>
#include <Array.au3>
#include <Constants.au3>
ProgressOn("Gathering file list", "Please Wait", "Please Wait", Default, Default, 18)
$files = _FileListToArray(@UserProfileDir & "\Downloads", "*(?).*", $FLTA_FILES)
If @error Then
    SetError(0)
    ProgressOff()
    MsgBox($MB_OK + $MB_ICONHAND, "No duplicate files", 'There were no files in your downloads folder which appear to be duplicates (determined by the presence of "(n)" at the end of the file name, where "n" is a digit).  Try running this tool again when there are duplicate files in your downloads folder.')
    Exit
Else
    _ArrayDelete ( $files, 0 )
    $string = ""
    $del = 0
    For $i = 0 To UBound ( $files ) - 1 Step 1

        $ext = _GetFilenameExt ( @UserProfileDir & "\Downloads\" & $files[$i] )
        $spli1 = StringSplit ( _GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]) & "." & $ext, "(" )
            $spli2 = StringSplit ( $spli1[2], ")" )
            $newname = StringStripWS ( $spli1[1], 3 ) & StringStripWS ( $spli2[2], 3 )
        $string = $string & $newname & ";"
    Next

    _ArrayAdd($files, StringTrimRight($string, 1), Default, ";")
    $files = _ArrayUnique($files)
    _ArrayDelete ( $files, 0 )
    _ArraySort($files)

    #cs
        Local $new[UBound ( $files )][5]
        For $i = 0 To UBound ( $files ) - 1 Step 1
        $ext = _GetFilenameExt ( @UserProfileDir & "\Downloads\" & $files[$i] )
        $split = StringSplit ( $files[$i], "(" )
        If @error Then
        SetError ( 0 )
        $new[$i][0] = $files[$i]
        $new[$i][1] = "0"
        Else
        $new[$i][0] = StringStripWS ( $split[1], 3 ) & "." & $ext
        $new[$i][1] = StringLeft ( $split[2], 1 )
        EndIf
        $new[$i][1] = FileGetSize ( @UserProfileDir & "\Downloads\" & $files[$i] )
        Next
    #ce
    _ArrayDisplay($files)
    Local $fin[UBound($files)]
    $st = 0
    For $i = 0 To UBound($files) - 1 Step 1
        ProgressSet(($i / UBound ( $files )) * 100, _GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]), "Moving Files")
        $ext = _GetFilenameExt(@UserProfileDir & "\Downloads\" & $files[$i])
        $pa = StringInStr($files[$i], "(")
        If $pa = 0 Then
            ContinueLoop
        Else
            $spli1 = StringSplit ( _GetFilename(@UserProfileDir & "\Downloads\" & $files[$i]) & "." & $ext, "(" )
            $spli2 = StringSplit ( $spli1[2], ")" )
            $newname = StringStripWS ( $spli1[1], 3 ) & StringStripWS ( $spli2[2], 3 )
            If Not FileExists(@UserProfileDir & "\Downloads\" & $newname ) Then
                FileMove(@UserProfileDir & "\Downloads\" & $files[$i], @UserProfileDir & "\Downloads\" & $newname)
                $fin[$st] = $newname
                $st += 1
            Else
                $dup = _Date_Time_GetFileTime(@UserProfileDir & "\Downloads\" & $files[$i])
                $ol = _Date_Time_GetFileTime(@UserProfileDir & "\Downloads\" & $newname)
                $comp = _Date_Time_CompareFileTime($dup[2], $ol[2])
                If $comp = -1 Then
                    Do
                        FileDelete(@UserProfileDir & "\Downloads\" & $files[$i])
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $files[$i])
                    $fin[$st] = $newname
                    $st += 1
                Else
                    FileMove(@UserProfileDir & "\Downloads\" & $files[$i], @UserProfileDir & "\Downloads\" & $newname, 9)
                    $fin[$st] = $newname
                    $st += 1
                EndIf
            EndIf
            #cs
            $min = 0
            If StringIsSpace(StringMid($files[$i], Int($pa) - 1, 1)) Then
                $min = 2
            Else
                $min = 1
            EndIf

            If Not FileExists(@UserProfileDir & "\Downloads\" & StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext) Then
                FileMove(@UserProfileDir & "\Downloads\" & $files[$i], @UserProfileDir & "\Downloads\" & StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext)
                $fin[$st] = StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext
                $st += 1
            Else
                $dup = _Date_Time_GetFileTime(@UserProfileDir & "\Downloads\" & $files[$i])
                $ol = _Date_Time_GetFileTime(@UserProfileDir & "\Downloads\" & StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext)
                $comp = _Date_Time_CompareFileTime($dup[2], $ol[2])
                If $comp = -1 Then
                    Do
                        FileDelete(@UserProfileDir & "\Downloads\" & $files[$i])
                    Until Not FileExists(@UserProfileDir & "\Downloads\" & $files[$i])
                    $fin[$st] = StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext
                    $st += 1
                Else
                    FileMove(@UserProfileDir & "\Downloads\" & $files[$i], @UserProfileDir & "\Downloads\" & StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext, 9)
                    $fin[$st] = StringMid($files[$i], 1, Int($pa) - $min) & "." & $ext
                    $st += 1
                EndIf
            EndIf
            #ce
        EndIf
    Next
    ProgressOff()
    _ArrayDisplay($fin)
EndIf
Func _GetFilename($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.FileName
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilename

Func _GetFilenameExt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Extension
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameExt

Func _GetFilenameInt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Name
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameInt

Func _GetFilenameDrive($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return StringUpper($oObjectFile.Drive)
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameDrive

Func _GetFilenamePath($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Path
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenamePath

 

Edited by MattHiggs
Link to comment
Share on other sites

  • 3 months later...

This is, what I was looking for.
Unfortunately this did not work for me.
The line

Local $fin[$files[0]]

gave me an error message ( ==> Subscript used on non-accessible variable.:)

This here did the trick at the end:

FileChangeDir(RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}"))

$sSearch = FileFindFirstFile("*(*).*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

 

Link to comment
Share on other sites

On 8/16/2017 at 4:44 PM, martrix said:

This is, what I was looking for.
Unfortunately this did not work for me.
The line

Local $fin[$files[0]]

gave me an error message ( ==> Subscript used on non-accessible variable.:)

This here did the trick at the end:

FileChangeDir(RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}"))

$sSearch = FileFindFirstFile("*(*).*")
If $sSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($sSearch)
        If @error Then ExitLoop
        FileDelete($sFile)
    WEnd
EndIf
FileClose($sSearch)

 

I modified the script substantially from what it was before.  I edited the original post.  Try it one more time.

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