Jump to content

Array subscripts error (I know why, how do I fix?)


Recommended Posts

Hi folks, I am moving away from Windows 'batch' to Autoit. A lot more power with Autoit, and flexibility. Also a lot more to learn :)

I wrote a script that automatically backups something. It makes a back up every time I launch the script. It then deletes all backups except three.

That is the problem. Everything works great till the deletion part. If there are files to delete, it works great. If there are not files to delete I get this error 'Array variable has incorrect number of subscripts or subscript dimension range exceeded'.

I'm fairly sure that is has something to do with the 'Ubound' part. From what I read, do I need to resize the array? Or how do I move past the error. I am new to this, and not sure how do error checking.

Thanks for any input.

 

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

Dim $szDrive, $szDir, $szFName, $szExt


$openfile = 'D:\Z-Users\Last-Time\AppData\Roaming\Something\Saves'
$foldertoworkin = 'G:\Temp_D1\'

$filename = _PathSplit($openfile, $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay ($filename,"")
$Filenamevalue = _ArrayToString($filename,'',3,3)


; MsgBox(0, "Test",  $szFName)
FileChangeDir($openfile)
Local $param01 = "C:\Program Files\7-Zip\7z.exe"
Local $param02 = " a -mx1 -r "
Local $param03 = $foldertoworkin
; Local $param04 = $szFName
Local $param04 = 'Something--'
Local $param05 = ".zip"

;Create zip
Local $sRun = $param01&$param02&$param03&$param04 &"-"&  @MON &"-"& @MDAY  &"-"&  @YEAR  &"_"&  @HOUR  &"-"&  @MIN   &"-"&  @SEC&$param05
ConsoleWrite(' $sRun >'&$sRun&'<'&@CRLF) ; ..this way you can see what is gonna execute
RunWait($sRun, "", @SW_HIDE)
;##############################################################################################
Local $sFilePath = $foldertoworkin
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 = 3 To UBound($aFilePath, 1) ; '3 to ubound' takes the first '3'  off the top of the list returned form array (make sure what you want is on the top)
    ;ConsoleWrite("FileDelete: " & $aFilePath[$m][0] & @CRLF) ; instead of real deletion only saying what will be deleted - just for my security
    ;if _FileInUse($myfile[$n], 0) And MsgBox(0,"",@error) then continueloop; ADDED
    ;if $aFilePath[$m][0] And MsgBox(0,"",@error) then continueloop; ADDED
    FileDelete($aFilePath[$m][0])
    ;if FileDelete($aFilePath[$m][0]) And @error = 1 then continueloop; ADDED
Next

;##############################################################################

ProcessSetPriority("Something.exe", 3)

 

Edited by NoobieAutoitUser
All references to a game have been removed
Link to comment
Share on other sites

  • Moderators

NoobieAutoitUser,

Asking for help with scripts that launch games usually means an instant thread lock, however, as the question you are asking is not directly related to the game itself, I am prepared to leave this one open. However, in return I expect you to read the Forum rules carefully before you post again and in future do not make such mistakes again.

M23

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

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

; Try this?
local $currentarchives = UBound($aFilePath, 1) ; '3 to ubound' takes the first '3'  off the top of the list returned form array (make sure what you want is on the top)
If $currentarchives > 3 Then ; --- wrap the loop in an If Test
For $m = 3 To $currentarchives Step 1 ; now loop
    ;ConsoleWrite("FileDelete: " & $aFilePath[$m][0] & @CRLF) ; instead of real deletion only saying what will be deleted - just for my security
    ;if _FileInUse($myfile[$n], 0) And MsgBox(0,"",@error) then continueloop; ADDED
    ;if $aFilePath[$m][0] And MsgBox(0,"",@error) then continueloop; ADDED
    FileDelete($aFilePath[$m][0])
    ;if FileDelete($aFilePath[$m][0]) And @error = 1 then continueloop; ADDED
Next
EndIf ; --- wrap the loop in a n If Test

Skysnake

Skysnake

Why is the snake in the sky?

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