Jump to content
Sign in to follow this  
Willow

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

Recommended Posts

Willow

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

Share this post


Link to post
Share on other sites
BigDaddyO

because if it doesn't find any files, the variable is not an array.

You need to put in

if @error then

error message or something...

else

Run your loop

endif


Share this post


Link to post
Share on other sites
Willow

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.

Share this post


Link to post
Share on other sites
BigDaddyO

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

Share this post


Link to post
Share on other sites
UEZ

#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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.