Sign in to follow this  
Followers 0
BigDaddyO

Suggestions to compress my Code

5 posts in this topic

I am writing a script to verify that a program or patch has been installed properly. Because of this I am checking many files and version numbers etc. this is part of the script and I am wondering if there is a way to compress this down a little.

if $iniFileExist1 <> "Blank" Then
  if FileExists($iniFileExist1)Then
     FileWriteLine($FILE, "Exists:   " & $iniFileExist1)
  else
     FileWriteLine($FILE, ">Does Not Exist:   " & $iniFileExist1)
  Endif
    Endif
    
    if $iniFileExist2 <> "Blank" Then
  if FileExists($iniFileExist2)Then
     FileWriteLine($File, "Exists:   " & $iniFileExist2)
  else
     FileWriteLine($File, ">Does Not Exist:  " & $iniFileExist2)
  Endif
    Endif
    
    if $iniFileExist3 <> "Blank" Then
  if FileExists($iniFileExist3)Then
     FileWriteLine($File, "Exists:   " & $iniFileExist3)
  else
     FileWriteLine($File, ">Does Not Exist:  " & $iniFileExist3)
  Endif
    Endif

I check for 4 seperate things and each check has 15 sub items. Above is 3 of the sub items from one of the checks.

because the basic code is the same throughout each item Can I do some variable thing and just go through 1-15?

Thanks,

Mike


Share this post


Link to post
Share on other sites



Best way would be to use an array:

Dim $iniFileExist[15]

$iniFileExists[0] = "whatever the value of this variable was"
$iniFileExists[1] = "whatever the value of this variable was"
$iniFileExists[2] = "whatever the value of this variable was"
;....
$iniFileExists[14] = "whatever the value of this variable was"

For $i = 0 to 14
   if $iniFileExist[$i] <> "Blank" Then
      if FileExists($iniFileExist[$i])Then
         FileWriteLine($FILE, "Exists:   " & $iniFileExist[$i])
      else
         FileWriteLine($FILE, ">Does Not Exist:   " & $iniFileExist[$i])
      Endif
   Endif
Next

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Another way to handle have the same complex task to be executed several times is to create a function and call the function for each execution of the task.

Func TestFile($Filename)
   if $Filename <> "Blank" Then
      if FileExists($Filename)Then
         FileWriteLine($FILE, "Exists:   " & $Filename)
         Return 1  ; true
      else
         FileWriteLine($FILE, ">Does Not Exist:   " & $Filename)
         Return 0  ; False
      Endif
   Endif
EndFunc

TestFile("File 01")
TestFile("File 02")
TestFile("File 03")
. . .
TestFile("File 15")

The return value of the function can be used to indicate whether the function succeeded or not (finding the file).


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Best way would be to use an array:

Dim $iniFileExist[15]

$iniFileExists[0] = "whatever the value of this variable was"
$iniFileExists[1] = "whatever the value of this variable was"
$iniFileExists[2] = "whatever the value of this variable was"
;....
$iniFileExists[14] = "whatever the value of this variable was"

For $i = 0 to 14
   if $iniFileExist[$i] <> "Blank" Then
      if FileExists($iniFileExist[$i])Then
         FileWriteLine($FILE, "Exists:   " & $iniFileExist[$i])
      else
         FileWriteLine($FILE, ">Does Not Exist:   " & $iniFileExist[$i])
      Endif
   Endif
Next
CyberSlug,

The array worked great.

It took my total lines of code from 1276 down to 281. :ph34r:

I prefered to use the Array over the function because I am somewhat familliar with the array from my limited VB6 experience

Thanks alot,

Mike


Share this post


Link to post
Share on other sites

can you post what each of the sub_items are?


[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

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  
Followers 0