antmar904

Remove Duplicate Folder/Files Names

18 posts in this topic

#1 ·  Posted

Hi

I have a folder that contains 10,000 .zip folders and some of them have the same name with a different date and time stamp.  I would like to remove the duplicate folders and keep just one preferably the newest one.

Exp of .zip folder names:

Inventory_%computername%_datetime.zip

Any help is much appreciated.

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#RequireAdmin

$FolderList = _FileListToArray("C:\Temp\Audit", "*", "", "")

If @error = 1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
    Exit
EndIf
If @error = 2 Then
    MsgBox($MB_SYSTEMMODAL, "", "Invalid filter.")
    Exit
EndIf
If @error = 3 Then
    MsgBox($MB_SYSTEMMODAL, "", "Invalid flag.")
    Exit
EndIf
If @error = 4 Then
    MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
    Exit
EndIf

$FolderArray = _ArrayDisplay($FolderList)

;For $List = 1 To $FolderList[0]

 

Capture.JPG

Share this post


Link to post
Share on other sites



#2 ·  Posted

@antmar904

How about trying to get the filename using FileFindFirstFile() and FileFindNextFile() then get their FileGetSize() and FileGetTime() and do file compare if @ComputerName matches in  then do the FileDelete() by getting the latest file result in FileGetTime().

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi @KickStarter15

I don't care about the date or which one is newer as both folder contain the same exact data.  I am just trying to remove the duplicate folders.

I am trying to get a list of names in the folder then compare them but that is what I am having issues with.

How do I split a string with multiple delimiters?

#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    $hSearch = FileFindFirstFile("C:\Temp\Test\*.*")

    While 1
        $File = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        $Split = StringSplit($File, '_' & "" & '_', 1)
        For $i = 1 To $Split[0]
            ConsoleWrite("New Name Is : " & $Split[$i] & @CRLF)
        Next
    WEnd

EndFunc

 

Edited by antmar904

Share this post


Link to post
Share on other sites

#4 ·  Posted

Ok, this works I am able to get a list of fold names excluding the date and time but takes a bit long (30 sec).

Can I make this return the folder name (Inventory_%computername%) faster?

I then want to delete just the duplicate folder names.

#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    Local $List[1] = [""]

    $hSearch = FileFindFirstFile("C:\Temp\Audit\*.*")

    While 1
        $Files = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        _ArrayAdd($List, $Files)
    WEnd

    ;_ArrayDisplay($List)

    $List[0] = UBound($List) - 1

    For $i = 1 To $List[0]
        Local $Name = StringTrimRight($List[$i], 19)
        ConsoleWrite($Name & @CRLF)
    Next

EndFunc   ;==>Find

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

how would I step through the array to find the same folder names them delete the duplicates but keep one of the folders?

Share this post


Link to post
Share on other sites

#6 ·  Posted

What am I doing wrong?

#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    Local $List[1] = [""]

    $hSearch = FileFindFirstFile("C:\Temp\Test\*.*")

    While 1
        $Files = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        _ArrayAdd($List, $Files)
    WEnd

    ;_ArrayDisplay($List)

    For $i = 1 To UBound($List, 1) - 1
        Local $Name = StringTrimRight($List[$i], 2)
        ConsoleWrite($Name & @CRLF)
        If StringInStr($Name, $Name) Then
            ConsoleWrite("Deleting " & $List[$i] & @CRLF)
        Else
            ConsoleWrite($List[$i] & @CRLF)
        EndIf
    Next

EndFunc   ;==>Find

Here is my test folder content and console output.

 

cap1.JPG

cap2.JPG

Share this post


Link to post
Share on other sites

#7 ·  Posted

I think the seconds While-Wend loop should look like this (untested)

$SaveName=""
    For $i = 1 To UBound($List, 1) - 1
        Local $Name = StringTrimRight($List[$i], 2)
        ConsoleWrite($Name & @CRLF)
        If StringInStr($SaveName, $Name) Then
            ConsoleWrite("Deleting " & $List[$i] & @CRLF)
        Else
            ConsoleWrite($List[$i] & @CRLF)
        EndIf
        If $SaveName <> $Name then $SaveName = $Name 
    Next

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted

User @KaFu has released a duplicate file finder which you can find here:

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#9 ·  Posted

@Jos

Thank you once again!

That worked.

But I can't get the rmdir syntax correct!

#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    Local $List[1] = [""]

    $hSearch = FileFindFirstFile("C:\Temp\test\*.*")

    While 1
        $Files = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        _ArrayAdd($List, $Files)
    WEnd

    FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Started cleanup.")

    $SaveName = ""
    For $i = 1 To UBound($List, 1) - 1
        Local $Name = StringTrimRight($List[$i], 2)
        ;ConsoleWrite($Name & @CRLF)
        If StringInStr($SaveName, $Name) Then

            FileWriteLine(@ScriptDir & "\CleanUp.txt", "Deleting " & $List[$i] & @CRLF)
            RunWait(@ComSpec & ' /c rmdir /s /q ' $List[$i], "", @SW_HIDE)
        EndIf
        If $SaveName <> $Name Then $SaveName = $Name
    Next

FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Ended cleanup.")

EndFunc   ;==>Find

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

1 minute ago, water said:

User @KaFu has released a duplicate file finder which you can find here:

 

Hi @water

I just found that today but have not had a chance to test it out!

Thanks again!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

7 minutes ago, antmar904 said:

But I can't get the rmdir syntax correct!

Something like this maybe:? 

RunWait(@ComSpec & ' /c rmdir /s /q "' & $List[$i] & '"', "", @SW_HIDE)

Jos :)

 

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted

@Jos

That did not work.

Share this post


Link to post
Share on other sites

#13 ·  Posted

mmm...  you really need to slowly step up to the plate and start showing some initiative in your feedback as "doesn't work" doesn't mean anything to me. ;)

You need to do the testing and debugging as it is your setup ....so what doesn't work? What is the error?  What is the proper commandline when typed manually?
Get the gist? :)

Jos  

 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

The folders are still there and do not get deleted.

the proper command is cmd.exe rmdir /s /q c:\temp\test\inventory_a_2

#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    Local $List[1] = [""]

    $hSearch = FileFindFirstFile("C:\Temp\test\*.*")

    While 1
        $Files = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        _ArrayAdd($List, $Files)
    WEnd

    FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Started cleanup.")

    $SaveName = ""
    For $i = 1 To UBound($List, 1) - 1
        Local $Name = StringTrimRight($List[$i], 2)
        ;ConsoleWrite($Name & @CRLF)
        If StringInStr($SaveName, $Name) Then
            FileWriteLine(@ScriptDir & "\CleanUp.txt", "Deleting " & $List[$i] & @CRLF)
            RunWait(@ComSpec & ' /c rmdir /s /q "' & $List[$i] & '"', "", @SW_HIDE)
            ConsoleWrite($List[$i] & " Error code: " & @error & @CRLF)
        EndIf
        If $SaveName <> $Name Then $SaveName = $Name
    Next

FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Ended cleanup.")

EndFunc   ;==>Find

 

Capture.JPG

Edited by antmar904

Share this post


Link to post
Share on other sites

#15 ·  Posted

Ok ...  you still didn't do what I asked but make this change and see what the error is:

ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') cmd:' & @ComSpec & ' /c rmdir /s /q "' & $List[$i] & '"' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
            RunWait(@ComSpec & ' /k rmdir /s /q "' & $List[$i] & '"')

This show the total command ran and will open the CMD window visible and stay open for you to see any errors.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#16 ·  Posted

ah I see what the issue is.

 

Capture.JPG

Share this post


Link to post
Share on other sites

#18 ·  Posted

Thanks again everyone, I really appreciate the help!

Here is the working script:

#RequireAdmin
#include <Array.au3>
#include <StringConstants.au3>

Find()

Func Find()

    Local $List[1] = [""]

    $hSearch = FileFindFirstFile("C:\Temp\test\*.*")

    While 1
        $Files = FileFindNextFile($hSearch, 0)
        If @error Then ExitLoop
        _ArrayAdd($List, $Files)
    WEnd

    FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Started cleanup.")

    $SaveName = ""
    For $i = 1 To UBound($List, 1) - 1
        Local $Name = StringTrimRight($List[$i], 2)
        If StringInStr($SaveName, $Name) Then
            FileWriteLine(@ScriptDir & "\CleanUp.txt", "Deleting " & "C:\Temp\test\" & $List[$i] & @CRLF)
            ;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') cmd:' & @ComSpec & ' /c rmdir /s /q "' & "C:\Temp\test\" & $List[$i] & '"' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
            RunWait(@ComSpec & ' /c rmdir /s /q "' & "C:\Temp\test\" & $List[$i] & '"', "", @SW_HIDE)
        EndIf
        If $SaveName <> $Name Then $SaveName = $Name
    Next

FileWriteLine(@ScriptDir & "\CleanUp.txt", @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN & ":" & @SEC & " Ended cleanup.")

EndFunc   ;==>Find

 

Share this post


Link to post
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