Sign in to follow this  
Followers 0
Alpinestar

Compact Code please

7 posts in this topic

#1 ·  Posted (edited)

Hello

Below is an example of some of my code, I was wondering if this code could be made more compact instead of all the repeating that is going on. It is easier to read for me, just wanted to see if there was a better way..

Thanks in advance

Local $SimsServer = EnvGet("SimsServer")
Local $Sapassword = EnvGet("SaPassword")
Local $SimsUser = EnvGet("SimsUser")
Local $SimsPassword = EnvGet("SimsPassword")
;*************************************************
If Not $SimsServer = " " Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims Server Var not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - Sims Server identified : " & $SimsServer)
EndIf
If Not $Sapassword = " " Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  SA password not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - SA password identified")
EndIf
If Not $SimsUser = " " Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims User not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - Sims User identified : " & $SimsUser)
EndIf
If Not $SimsPassword = " " Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims User password not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - Sims User password identified")
EndIf
Edited by Alpinestar

Share this post


Link to post
Share on other sites



It's not the correct condition to use if I understand it correctly:

$Something = '3'
If Not $Something = '2' Then
    ; True
Else
    ; False
EndIfoÝ÷ Ù.ßÚÞë-{ޮاZ·MúJ¶§Ê'µ¨§jwméÜ¢jZ­ëk¹ê+}©ly¨{-+^­©m{m«fk*Þ²ém{k¹è¶¬¶¸§ëÞÛ½©nj׬¶k¹æ§vøz÷«±¤áȬ¶¢ºÞrÚ'y©m +wöÈî²Ö޶׫¶n¶Ø^z[q«~*ì¶)ìµæ¡óNObayÊ'v+b¢v®¶­sbb33cµ6öÖWFærÒb33³2b33°¤bæ÷Bb33cµ6öÖWFærÒb33³"b33²FVࢲG'VP¤VÇ6P¢²fÇ6

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the quick reply. The reason I had it that way round was because of the negative results I was getting. I have done some testing and amend the code below. I was assuming that having

" "
would be a null variable, but it seems that not true, a null variable without a space is a null variable. Probably a very silly mistake on my behalf.

Local $SimsServer = EnvGet("SimsServer")
Local $Sapassword = EnvGet("SaPassword")
Local $SimsUser = EnvGet("SimsUser")
Local $SimsPassword = EnvGet("SimsPassword")
;*************************************************

If NOT ($SimsServer = '') Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims Server Var not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - Sims Server identified : " & $SimsServer)

EndIf
If ($Sapassword = '') Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  SA password not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - SA password identified")
EndIf
If ($SimsUser = '') Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims User not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\Backup.log", "Success - Sims User identified : " & $SimsUser)
EndIf
If ($SimsPassword = '') Then
    _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  Sims User password not Found ")
    _OutPut()
Else
    _FileWriteLog(@ScriptDir & "\4SBackup.log", "Success - Sims User password identified")
EndIf
Edited by Alpinestar

Share this post


Link to post
Share on other sites

So is there any way to reduce this code at all ??

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Not sure why you would want to shorten this code but here is a shorter (untested) version:

TestVars("SimsServer")
TestVars("Sapassword")
TestVars("SimsUser")
TestVars("SimsPassword")
;
Func TestVars($inputVar)
    Assign($inputVar,EnvGet($inputVar),2)
    If Eval($inputVar) = '' Then
        _FileWriteLog(@ScriptDir & "\Backup.log", "Error -  " & $inputVar & " not Found ")
        _OutPut()
    Else
        _FileWriteLog(@ScriptDir & "\Backup.log", "Success - " & $inputVar & " identified")
    EndIf
EndFunc
Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

#include <File.au3>

Local $sEnv = 'SimsServer|SaPassword|SimsUser|SimsPassword'
Local $avSplit = StringSplit($sEnv, '|')
Local $sFile = @ScriptDir & '\Backup.log'

For $i = 1 To $avSplit[0]
    Local $avWords = StringRegExp($avSplit[$i], '[A-Z]+[a-z]+', 3)
    Local $sVar = EnvGet($avSplit[$i])
    
    If $sVar = '' Then
        _FileWriteLog($sFile, 'Error - ' & $avWords[0] & ' ' & $avWords[1] & ' Var Not Found')
        _OutPut()
    Else
        _FileWriteLog($sFile, 'Success - ' & $avWords[0] & ' ' & $avWords[1] & ' identified : ' & $sVar)
    EndIf
Next

...

Edit: StringRegExp...

Edited by Authenticity

Share this post


Link to post
Share on other sites

Thanks for the replies.

Will give it a go, its interesting to see the different ways to complete the same thing. :D

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