Jump to content

Line -1: Error: Subscript used with non-Array variable


 Share

Recommended Posts

When running this code, it will randomly fail with the error message: Line -1: Error: Subscript used with non-Array variable.

Any help in finding the cause is appreciated.

$archive_folder = "C:\Archived backups"
IF NOT FileExists($archive_folder) THEN DirCreate($archive_folder)
$arc = "C:\data"
$FoldersToMove = "backup*"
$FolderArray = _FileListToArray( $arc, $FoldersToMove, 2)
For $i = 1 To $FolderArray[0]
    DirMove($arc & "\" & $FolderArray[$i], $archive_folder & "\" & $FolderArray[$i], 1)
Next
Link to comment
Share on other sites

Thanks, Mike,

Not sure I quite follow what you mean. I now realize the array is failing because there are no files,however I don't need to display an error message, the code just needs to continue past this as it will be running unattended and finding no file(s) is not a worry as sometimes there will be files, other times there will be not.

Link to comment
Share on other sites

ok,

in that case I would use if Not @error then. I don't think your supposed to use if not due to some performance issues in the milliseconds but i havn't had a problem.

So your code should be like

$archive_folder = "C:\Archived backups"
IF NOT FileExists($archive_folder) THEN DirCreate($archive_folder)
$arc = "C:\data"
$FoldersToMove = "backup*"
$FolderArray = _FileListToArray( $arc, $FoldersToMove, 2)
if not @error then
For $i = 1 To $FolderArray[0]
    DirMove($arc & "\" & $FolderArray[$i], $archive_folder & "\" & $FolderArray[$i], 1)
Next
endif
Edited by MikeOsdx
Link to comment
Share on other sites

#Include <File.au3>
#Include <Array.au3>
$archive_folder = "C:\Archived backups"
IF NOT FileExists($archive_folder) THEN DirCreate($archive_folder)
$arc = "C:\data"
$FoldersToMove = "backup*"
$FolderArray = _FileListToArray( $arc, $FoldersToMove, 2)
If Not @error And IsArray($FolderArray) Then
    For $i = 1 To $FolderArray[0]
        DirMove($arc & "\" & $FolderArray[$i], $archive_folder & "\" & $FolderArray[$i], 1)
    Next
Else
    MsgBox(16, "Error", "An error has occured!")
EndIf

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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