Jump to content

(Solved) How to delete files 'read' in to an array ?


Recommended Posts

Hi everybody. I'm trying to delete files I have read into an array. Everything works, other than the deleting.

I have scoured the forums, and tried all kinds of thing. Most errors I get are 'Array variable has incorrect number of subscripts error'.

I am not sure what I am doing with arrays, and am quite proud I got the delete lines to work :)

Any pointers how to use the info form the array to delete these files found ?

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

Local $sFilePath = "K:\zxzx"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
; If @error Then Exit


; _ArrayDisplay($aFilePath)


Local $aFileTime[1][2]

For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next

_ArraySort ($aFileTime,1,1,"",1)   ;sort by date

; Deletes first two results (newest is ontop)
_ArrayDelete($aFileTime, "1-2")

_ArrayDisplay($aFileTime)

 

Edited by NoobieAutoitUser
Link to comment
Share on other sites

Yes

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $sFilePath = "C:\Users\careca\Desktop\temp"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next
;=============================================================================
_ArraySort ($aFileTime,1,1,"",1)   ;sort by date
_ArrayDelete($aFileTime, "1-2")
_ArrayDisplay($aFileTime)
;=============================================================================
For $i = 1 To $aFilePath[0]
FileDelete($aFilePath[$i])
Next
;=============================================================================

Deletes all, is that what you want?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Yes, if you want to delete all of the files referenced by the array, then you would need to loop through the array and process each of the desired elements.

In your code, you are actually building two different arrays. Did you need to examine the files date/time to determine if it should be deleted?

Link to comment
Share on other sites

 

@careca

This code looks real nice :) Unfortunately its deletes all (which you said it would). I would like to only delete the values that display in '_ArrayDisplay($aFileTime)'.

This is where I get stuck, how do I use the values in '_ArrayDisplay($aFileTime)' to delete files? Thanks a lot for your help.

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $sFilePath = "K:\zxzx"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next
;=============================================================================
_ArraySort ($aFileTime,1,1,"",1)   ;sort by date
_ArrayDelete($aFileTime, "1-2")
_ArrayDisplay($aFileTime)
;=============================================================================
For $i = 1 To $aFilePath[0]
FileDelete($aFilePath[$i])
Next
;=============================================================================

 

@Danp2

Yes, my goal is to only delete the top two in the array display (the newest files). Then using what is left in the array (after deleting). This is for a auto delete 'batch'. I have used other scripts before, but if something goes wrong, and after a period of time, all your files get deleted. After this works, then it always keeps at least two files for backup. I hope this makes sense :)

 

 

 

Link to comment
Share on other sites

Delete all BUT the 2 most recent ones? try this

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $sFilePath = "C:\Users\careca\Desktop\temp"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next
;=============================================================================
_ArraySort ($aFileTime,1, 0, 0, 1)   ;sort by date
_ArrayDisplay($aFileTime)
;=============================================================================
For $i = 1 To $aFilePath[0]-2
FileDelete($aFilePath[$i])
Next
;=============================================================================

EDIT: Almost, but something is not good, one is not the most recent.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

@ careca & @ Danp2

Thank you both for taking the time to assist. Unfortunately their are errors.

Its '$aFilePath'. No matter what I do with this, it always returns an error.  Not always the same error, but '$aFilePath' it is always something to do with this. After a few hours, I came here to for help lol :) Both arrays use '$i' as a variable? Might that that be the trouble ?

Error
"M:\xvcbvctyuiyt76.au3" (8) : ==> Subscript used on non-accessible variable.:
For $i = 1 To $aFilePath[0]
For $i = 1 To $aFilePath^ ERROR

 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $sFilePath = "K:\zxzx"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next
;=============================================================================
_ArraySort ($aFileTime,1,1,"",1)   ;sort by date
; _ArrayDelete($aFileTime, "1-2")
_ArrayDisplay($aFileTime)
;=============================================================================
For $i = 2 To UBound ($aFileTime, 1)
    FileDelete($aFileTime[$i][0])
Next
;=============================================================================

 

Link to comment
Share on other sites

2 minutes ago, NoobieAutoitUser said:

Both arrays use '$i' as a variable? Might that that be the trouble ?

Nope. Not an issue.

Error
"M:\xvcbvctyuiyt76.au3" (8) : ==> Subscript used on non-accessible variable.:
For $i = 1 To $aFilePath[0]
For $i = 1 To $aFilePath^ ERROR

You should check the value of @error after the call to _FileListToArrayRec

Link to comment
Share on other sites

Neither nor. Read the help file and you can change it to your interest.

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

I understand how to sort by date now :)

In the scrip their are two arrays, first is '$aFilePath' and second is '$aFileTime'. '$aFileTime' give me the correct things to delete, but the the script uses '$aFilePath' to delete the files. I try switching the arrays and I get a subscript error, or it does nothing.  What can I do to use '$aFilePath' as the array the delete files?

Thanks for all the input, I slowly learn my way around :)

The below example deletes nothing.

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Local $sFilePath = "K:\zxzx"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
For $i = 1 To $aFilePath[0]
    _ArrayAdd($aFileTime, $aFilePath[$i] & "|" & FileGetTime($aFilePath[$i], 0, 1))
Next
;=============================================================================
; _ArraySort ($aFileTime,1, 0, 0, 1)   ;sort by date

_ArraySort ($aFilePath,1, 0, 0, 1)   ;sort by date

; _ArrayDisplay($aFileTime)
_ArrayDisplay($aFilePath)
;=============================================================================
For $m = 1 To $aFilePath[0]-2
FileDelete($aFilePath[$m])
Next
;=============================================================================

 

Link to comment
Share on other sites

Hi. You get a 1D-Array with _FileListToArrayRec. That means there is only one column. With your kind of _ArrayAdd you try to fill in two columns. That is not working. I insert a second column first:

#include <Array.au3>
#include <File.au3>
Local $sFilePath = @ScriptDir
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
_ArrayColInsert($aFilePath, 1) ; insert a second column
For $i = 1 To $aFilePath[0][0] ; now it's [0][0] - row index 0, column index 0
    $aFilePath[$i][1] = FileGetTime($aFilePath[$i][0], 0, 1) ; in column 1 (remember 0-indexed - if you start counting by one it's your second)
Next
;=============================================================================
_ArrayDelete($aFilePath, 0) ; delete number of indexes - there is no filepath inside
_ArraySort ($aFilePath,1, 0, 0, 1)   ;sort by date
_ArrayDisplay($aFilePath)
;=============================================================================
For $m = 0 To 1 ; you only want to delete the newest and second newest file (remember row index 0 and 1)
    ConsoleWrite("FileDelete: " & $aFilePath[$m][0] & @CRLF) ; instead of real deletion only saying what will be deleted - just for my security
Next

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Thanks to all of you that helped. I start to see writing the script the easy, its the debugging that is a PITA (pain in the azz).

@ Simpel - Thanks for cleaning up my script and the pointers. What is a 1D array? As opposed to a 2D array?

@ Danp2 - It was the top two (by creation date). Thanks for your post, as I was going nuts trying to figure how to make that work :)

I changed to it to sort by 'creation date'.

I will start to use the 'console write' more, as it really helps to see what it will do before it does it.

Again thanks.

 

#include <Array.au3>
#include <File.au3>
Local $sFilePath = "K:\zxzx"
Local $aFilePath = _FileListToArrayRec($sFilePath, "*", 1,0,0,2)
Local $aFileTime[1][2]
;=============================================================================
_ArrayColInsert($aFilePath, 1) ; insert a second column
For $i = 1 To $aFilePath[0][0] ; now it's [0][0] - row index 0, column index 0
    $aFilePath[$i][1] = FileGetTime($aFilePath[$i][0], 1, 1) ; in column 1 (remember 0-indexed - if you start counting by one it's your second)
Next
;=============================================================================
_ArrayDelete($aFilePath, 0) ; delete number of indexes - there is no filepath inside
_ArraySort ($aFilePath,1, 0, 0, 1)   ;sort by date
_ArrayDisplay($aFilePath)
;=============================================================================
For $m = 2 To UBound($aFilePath, 1) ; you only want to delete the newest and second newest file (remember row index 0 and 1)
    ; ConsoleWrite("FileDelete: " & $aFilePath[$m][0] & @CRLF) ; instead of real deletion only saying what will be deleted - just for my security
    ; ConsoleWrite("FileDelete: " & $aFilePath[$m][0] & @CRLF) ; instead of real deletion only saying what will be deleted - just for my security
    FileDelete($aFilePath[$m][0])
Next

 

Link to comment
Share on other sites

Hi. Glad to help you. 1D-Array means only one dimension. Only one column with a lot of rows. 2D-Array means two dimensions. Many columns and many rows. Seldom used but possible even 3D-Arrays. Put just a third dimension. There are some more dimensions possible but hard to imagine and so not used so much.

1D: $aArray[n]

2D: $aArray[n][m]

3D: $aArray[n][m][k]

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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