Jump to content

Recommended Posts

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

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

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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

 

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

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

 

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

  • Developers
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  

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

 

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