DavidW

Script debugging

8 posts in this topic

#1 ·  Posted (edited)

Hi everyone,

this is my first own big script that i made with help of the forum ofcourse.

 i've made a script that needs 4 arguments/parameters entered in order to run.   

  1.  the map location
  2. delete files older then xx minutes
  3. check subfolders or only mainfolders
  4. direct of trash bin delete

But for some reason it doesn't seem to work and cant find out why. 

When i press F5 it says directory not exist pretty obv, because no folder location is given, then i compile and run with cmd arguments "C:\school  25 subfolder trashbin" and there is the error

The error code im getting is : error line 7253 

Error: Subscript used on non-accessible variable

 

Hope you guys can help me or point me into the right direction

Also i'd love to hear suggestions to improve this script since i think it can be easier build but dont know how.

;;;;;;;;;;;;;
#RequireAdmin
#include <File.au3>
#include <Date.au3>
#include <FileConstants.au3>
#include <AutoItConstants.au3>
;;;;;;;

;;; variabelen
$maplocatie = ""
$number = ""
$filelist0= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
$filelist1= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
$folder = ""
$consequence = ""
;;;;;;;;;;

;command line parameters
If $cmdline[0]>0 Then
    $maplocatie= $cmdline[1]
    Switch $maplocatie
        Case ""
            Assign("maplocatie", $maplocatie)
    EndSwitch
EndIf

if $cmdline[0]>1 Then
    $number = $cmdline[2]
    Switch $number
        Case 1 to 9999
    EndSwitch
EndIf

if $cmdline[0]>2 Then
    $antwoord = $cmdline [3]
    Switch $antwoord
        Case "mainfolder"
            Assign ("folder", "mainfolder")


        case "subfolder"
            assign ("folder", "subfolder")

        case Else
            msgbox(0, "", "Only 'mainfolder or subfolder available.")
            EndSwitch
EndIf
if $cmdline[0]>3 Then
    $deleten = $cmdline[4]
    Switch $deleten
        Case "direct"
            assign("consequence", "direct")

        case "trashbin"
            assign("consequence", "trashbin")
        case Else
            msgbox(0, "", "Only direct or trashbin available")
            EndSwitch
EndIf
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If Not FileExists($maplocatie) Then ;check if given maplocation exists
    msgbox(0, "", "the directory does not exists")
EndIf

If $folder = "mainfolder" Then  ;;;;; decides to run loop 0 (only scans the mainfolder)
    call("loop0")
ElseIf $folder = "subfolder" Then ;;;;; or loop 1(scans the subfolders aswell)
    call("loop1")
EndIf

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

func loop0()
for $o = 1 to $filelist0[0] ;;;; loop which runs through all files in the mainfolder
    $tijd = FileGetTime($filelist0 [$o],1,1) ;;;; gets filetime and converts into a string ( YYYYMMDDHHMMSS)
    $tijd = StringRegExpReplace ($tijd, "(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "$1/$2/$3 $4:$5:$6") ;;;;;;;;; converts the YYYYMMDDHHMMSS string into year/month/day 00:00:00
    If _datediff('n', $tijd, _nowcalc()) > $number And $consequence = "direct" Then ;;;;;;;;; checks the date diffs between now and the number in minutes and check if $deleten is direct then proceed to do a direct file delete)
        filedelete($filelist0[$o])
    ElseIf _datediff('n', $tijd, _nowcalc()) > $number and $consequence = "trashbin" Then ;;;;;;;;; checks the date diffs between now and the number in minutes and check if $deleten is trashbin then proceed to do a trashbin file delete)
        FileRecycle($filelist0[$o])
    Else
        msgbox(0, "", "wrong parameters entered")
    EndIf
    Next
EndFunc

func loop1()
    For $i = 1 to $filelist1[0] ;;;; loop which runs through all files in the main subfolders
    $tijd = FileGetTime($filelist1 [$i], 1,1)  ;;;; gets filetime and convertsit to a string ( YYYYMMDDHHMMSS)
    $tijd = StringRegExpReplace($tijd,"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "$1/$2/$3 $4:$5:$6") ;;;;;;;;; converts the YYYYMMDDHHMMSS string into year/month/day 00:00:00
    If  _DateDiff('n', $tijd,  _NowCalc()) > $number and $consequence = "direct" Then ;;;;;;;;; checks the date diffs between now and the number in minutes and check if $deleten is direct then proceed to do a direct file delete)
        filedelete($filelist1[$i])
    ElseIf _datediff ('n', $tijd, _nowcalc()) > $number and $consequence = "trashbin" Then ;;;;;;;;; checks the date diffs between now and the number in minutes and check if $deleten is trashbin then proceed to do a trashbin file delete)
        filerecycle($filelist1[$i])
    Else
        msgbox(0, "", "wrong parameters entered")
        EndIf
        Next
    EndFunc

 

Edited by DavidW

Share this post


Link to post
Share on other sites



you don't need to compile in order to pass parameters to your script: SciTE menu bar > View > Parameters

also, note that if you have a parameter with a whitespace in it, you must enclose it in double-quotes.

Share this post


Link to post
Share on other sites
37 minutes ago, orbs said:

you don't need to compile in order to pass parameters to your script: SciTE menu bar > View > Parameters

also, note that if you have a parameter with a whitespace in it, you must enclose it in double-quotes.

Hello thanks for the tip! lol is going to save me some time in the future :D

What exactly do you mean with the second one? i dont seem to understand since all my parameters are already enclosed? i think or did i miss something? 

im new in this language sorry.

Share this post


Link to post
Share on other sites

The problem is that you had $filelist0 reading a location not set by the parameter, it is above the cmdline therefor it will retrieve an error.

Move this bit to under the cmd line like so:
 

If $cmdline[0]>0 Then
    $maplocatie= $cmdline[1]
    Switch $maplocatie
        Case ""
            Assign("maplocatie", $maplocatie)
    EndSwitch
EndIf

$filelist0= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
$filelist1= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

Now it will set the var $maplocatie to something, a folder, and it will check how many files there are there.

1 person likes this

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

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

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

8 minutes ago, careca said:

The problem is that you had $filelist0 reading a location not set by the parameter, it is above the cmdline therefor it will retrieve an error.

Move this bit to under the cmd line like so:
 

If $cmdline[0]>0 Then
    $maplocatie= $cmdline[1]
    Switch $maplocatie
        Case ""
            Assign("maplocatie", $maplocatie)
    EndSwitch
EndIf

$filelist0= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
$filelist1= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

Now it will set the var $maplocatie to something, a folder, and it will check how many files there are there.

It fixed the problem, Thank you so much!!

Could i perhaps ask why it doesn't work if i make it a global variables and add it in the ;;;; variabelen list? Since if i declare it global it should work in the whole script right?

 

Sorry low experience in programming. AutoIT is my first language

Edited by DavidW

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Im not sure i understand the question. You can set it as global or local, but afterwards you have to make it so that it gets changed to what you want.

Example:

Local $maplocatie ;Var declared

If $cmdline[0]>0 Then
    $maplocatie= $cmdline[1] ;var set to parameter value
    Switch $maplocatie
        Case "" ;because the parameter is empty we better exit now than wait for filecount to crash.
            msgbox error blah blah
            exit 
    EndSwitch
EndIf

$filelist0= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;var used in file count
$filelist1= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) 

;if the var gets set to string, when it comes to here, will error.

My sugestion is to add error checking so that it stops as soon as there is a parameter not recognized.

Like you did here:

if $cmdline[0]>2 Then
    $antwoord = $cmdline [3]
    Switch $antwoord
        Case "mainfolder"
            Assign ("folder", "mainfolder")


        case "subfolder"
            assign ("folder", "subfolder")

        case Else
            msgbox(0, "", "Only 'mainfolder or subfolder available.")
            EndSwitch
EndIf

Only closing uppon error.

if $cmdline[0]>2 Then
    $antwoord = $cmdline [3]
    Switch $antwoord
        Case "mainfolder"
            Assign ("folder", "mainfolder")


        case "subfolder"
            assign ("folder", "subfolder")

        case Else
            msgbox(0, "Error", "Only 'mainfolder or subfolder available. Param 3") ;inform the parameter at fault.
            Exit ;Exit when error
            EndSwitch
EndIf

 

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

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

 

Share this post


Link to post
Share on other sites
4 minutes ago, careca said:

Im not sure i understand the question. You can set it as global or local, but afterwards you have to make it so that it gets changed to what you want.

Example:

Local $maplocatie ;Var declared

If $cmdline[0]>0 Then
    $maplocatie= $cmdline[1] ;var set to parameter value
    Switch $maplocatie
        Case ""
            Assign("maplocatie", $maplocatie) ;var set to string if parameter empty
    EndSwitch
EndIf

$filelist0= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;var used in file count
$filelist1= _fileListToArrayRec($maplocatie, "*" ,$FLTAR_FILES,$FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) 

;if the var gets set to string, when it comes to here, will error.

My sugestion is to add error checking so that it stops as soon as there is a parameter not recognized.

Like you did here:

if $cmdline[0]>2 Then
    $antwoord = $cmdline [3]
    Switch $antwoord
        Case "mainfolder"
            Assign ("folder", "mainfolder")


        case "subfolder"
            assign ("folder", "subfolder")

        case Else
            msgbox(0, "", "Only 'mainfolder or subfolder available.")
            EndSwitch
EndIf

Only closing uppon error.

if $cmdline[0]>2 Then
    $antwoord = $cmdline [3]
    Switch $antwoord
        Case "mainfolder"
            Assign ("folder", "mainfolder")


        case "subfolder"
            assign ("folder", "subfolder")

        case Else
            msgbox(0, "Error", "Only 'mainfolder or subfolder available. Param 3") ;inform the parameter at fault.
            Exit ;Exit when error
            EndSwitch
EndIf

 

ah kk got it, thanks for your time and explaining.

you're a big lad! thanks again!

good karma will strike upon you, you have my blessings

Share this post


Link to post
Share on other sites

Edited the post, if you want to take a look.

best regards.


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

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

 

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