Jump to content

How can I re-write this code and make it more shorter and professional


Recommended Posts

Ok here is my code below.. U will never believe that it took me an entire day to do it. I can somehow see that there must be a better way to write this code. But my confusion is because of the different paths. if the path was the same then i could have simply used a for next statement....the code below gives me the expected results but for some reason i keep thinking that what i have done is not professional programming..thank you for your help...,

#include <file.au3>
     
     $metal = round(DirGetSize("\\drive a\tools\metal")/1000/1000/1000,2)
     $plastic = round(DirGetSize ("\\drive a\tools\plastic")/1000/1000/1000,2)
     $metal1 = round(DirGetSize ("\\drive a\boxes\metal1")/1000/1000/1000,2)
     $plastic2 = round(DirGetSize ("\\drive a\boxes\plastic2")/1000/1000/1000,2)
     
     $bigtable = round(DirGetSize ("\\drive a\tables\big table")/1000/1000/1000,2)
     $smalltable = round(DirGetSize("\\drive a\tables\small table")/1000/1000/1000,2)
     $flattable = round(DirGetSize("\\drive a\tables\flat table")/1000/1000/1000,2) 
     
     $total = $metal + $plastic + $metal1 + $plastic2
     
     $sLogPath = ("c:\mylogfile.txt")
     $sLogMsg1 = ("drive       n - (metal)    : " & $metal& " GiB")
     $sLogMsg2 = ("drive       m - (plastic) : " & $plastic& " GiB" )
     $sLogMsg3 = ("drive       o - (metal1)  : " & $metal1& " GiB")
     $sLogMsg4 = ("drive       p - (plastic2)    : " & $plastic2 & " GiB")
     $sLogMsg5 = ("total space used   : " & $total& " GiB")
     
     $sLogMsg6 = ("big table         : " & $bigtable & " GiB")
     $sLogMsg7 = ("small table    : " & $smalltable & " GiB")
     $sLogMsg8 = ("flat table              : " & $flattable & " GiB")
     
      _FileWriteLog($sLogPath, $sLogMsg1 )
      _FileWriteLog($sLogPath, $sLogMsg2 )
      _FileWriteLog($sLogPath, $sLogMsg3 )
      _FileWriteLog($sLogPath, $sLogMsg4 )
      _FileWriteLog($sLogPath, $sLogMsg5 )

      _FileWriteLog($sLogPath, $sLogMsg6 )
      _FileWriteLog($sLogPath, $sLogMsg7 )
      _FileWriteLog($sLogPath, $sLogMsg8 )
Link to comment
Share on other sites

ZacUSNYR, you beat me to it.

#include <file.au3>

$metal = round(DirGetSize("\\drive a\tools\metal")/1000/1000/1000,2)
$plastic = round(DirGetSize ("\\drive a\tools\plastic")/1000/1000/1000,2)
$metal1 = round(DirGetSize ("\\drive a\boxes\metal1")/1000/1000/1000,2)
$plastic2 = round(DirGetSize ("\\drive a\boxes\plastic2")/1000/1000/1000,2)

$bigtable = round(DirGetSize ("\\drive a\tables\big table")/1000/1000/1000,2)
$smalltable = round(DirGetSize("\\drive a\tables\small table")/1000/1000/1000,2)
$flattable = round(DirGetSize("\\drive a\tables\flat table")/1000/1000/1000,2) 

$total = $metal + $plastic + $metal1 + $plastic2

$sLogPath = ("c:\mylogfile.txt")
Local $aLogMsg[8]
$aLogMsg[0] = ("drive       n - (metal)    : " & $metal& " GiB")
$aLogMsg[1] = ("drive       m - (plastic) : " & $plastic& " GiB" )
$aLogMsg[2] = ("drive       o - (metal1)  : " & $metal1& " GiB")
$aLogMsg[3] = ("drive       p - (plastic2)    : " & $plastic2 & " GiB")
$aLogMsg[4] = ("total space used   : " & $total& " GiB")
$aLogMsg[5] = ("big table         : " & $bigtable & " GiB")
$aLogMsg[6] = ("small table    : " & $smalltable & " GiB")
$aLogMsg[7] = ("flat table              : " & $flattable & " GiB")

For $i = 0 To 7
     _FileWriteLog($sLogPath, $aLogMsg[$i] )
Next
Link to comment
Share on other sites

Even one step further...reducing the Array across 3 lines instead of 9!

#include <file.au3>

$metal = Round(DirGetSize("\\drive a\tools\metal") / 1000 / 1000 / 1000, 2)
$plastic = Round(DirGetSize("\\drive a\tools\plastic") / 1000 / 1000 / 1000, 2)
$metal1 = Round(DirGetSize("\\drive a\boxes\metal1") / 1000 / 1000 / 1000, 2)
$plastic2 = Round(DirGetSize("\\drive a\boxes\plastic2") / 1000 / 1000 / 1000, 2)

$bigtable = Round(DirGetSize("\\drive a\tables\big table") / 1000 / 1000 / 1000, 2)
$smalltable = Round(DirGetSize("\\drive a\tables\small table") / 1000 / 1000 / 1000, 2)
$flattable = Round(DirGetSize("\\drive a\tables\flat table") / 1000 / 1000 / 1000, 2)

$total = $metal + $plastic + $metal1 + $plastic2

$sLogPath = ("c:\mylogfile.txt")
Local $aLogMsg[8] = ["drive       n - (metal)    : " & $metal & " GiB", "drive       m - (plastic) : " & $plastic & " GiB", "drive       o - (metal1)  : " & $metal1 & " GiB", _
        "drive       p - (plastic2)    : " & $plastic2 & " GiB", "total space used   : " & $total & " GiB", "big table         : " & $bigtable & " GiB", "small table    : " & $smalltable & " GiB", _
        "flat table              : " & $flattable & " GiB"]

For $i = 0 To 7
    _FileWriteLog($sLogPath, $aLogMsg[$i])
Next

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Improved code from post #3

Used function for repetitive task ;-)

#include <file.au3>

$metal = _GetSize("\\drive a\tools\metal")
$plastic = _GetSize("\\drive a\tools\plastic")
$metal1 = _GetSize("\\drive a\boxes\metal1")
$plastic2 = _GetSize("\\drive a\boxes\plastic2")

$bigtable = _GetSize("\\drive a\tables\big table")
$smalltable = _GetSize("\\drive a\tables\small table")
$flattable = _GetSize("\\drive a\tables\flat table")

$total = $metal + $plastic + $metal1 + $plastic2

$sLogPath = ("c:\mylogfile.txt")
Local $aLogMsg[8]
$aLogMsg[0] = ("drive       n - (metal) : " & $metal& " GiB")
$aLogMsg[1] = ("drive       m - (plastic) : " & $plastic& " GiB" )
$aLogMsg[2] = ("drive       o - (metal1)  : " & $metal1& " GiB")
$aLogMsg[3] = ("drive       p - (plastic2)  : " & $plastic2 & " GiB")
$aLogMsg[4] = ("total space used   : " & $total& " GiB")
$aLogMsg[5] = ("big table       : " & $bigtable & " GiB")
$aLogMsg[6] = ("small table : " & $smalltable & " GiB")
$aLogMsg[7] = ("flat table              : " & $flattable & " GiB")

For $i = 0 To 7
    _FileWriteLog($sLogPath, $aLogMsg[$i] )
Next

Func _GetSize($path)
    Return round(DirGetSize($path)/1000/1000/1000,2)
EndFunc
Link to comment
Share on other sites

If "\\drive a\" is always the same then it can be simplified even more

#include <file.au3>

$metal = _GetSize("tools\metal")
$plastic = _GetSize("tools\plastic")
$metal1 = _GetSize("boxes\metal1")
$plastic2 = _GetSize("boxes\plastic2")

$bigtable = _GetSize("tables\big table")
$smalltable = _GetSize("tables\small table")
$flattable = _GetSize("tables\flat table")

$total = $metal + $plastic + $metal1 + $plastic2

$sLogPath = ("c:\mylogfile.txt")
Local $aLogMsg[8]
$aLogMsg[0] = ("drive       n - (metal) : " & $metal& " GiB")
$aLogMsg[1] = ("drive       m - (plastic) : " & $plastic& " GiB" )
$aLogMsg[2] = ("drive       o - (metal1)  : " & $metal1& " GiB")
$aLogMsg[3] = ("drive       p - (plastic2)  : " & $plastic2 & " GiB")
$aLogMsg[4] = ("total space used   : " & $total& " GiB")
$aLogMsg[5] = ("big table       : " & $bigtable & " GiB")
$aLogMsg[6] = ("small table : " & $smalltable & " GiB")
$aLogMsg[7] = ("flat table              : " & $flattable & " GiB")

For $i = 0 To 7
    _FileWriteLog($sLogPath, $aLogMsg[$i] )
Next

Func _GetSize($path)
    Return round(DirGetSize("\\drive a\" & $path)/1000/1000/1000,2)
EndFunc
Link to comment
Share on other sites

The next iteration would be to get rid of the hard-code Strings and separate code from data by using an ini or xml file strings that refer to the directories that you wish to analyze and then pass the file(s) into your code via command-line parameter. This way the same code could be used to analyze many different sets of directories.

Link to comment
Share on other sites

now why is is that this command seems to be extremely slow

$metal = Round(DirGetSize("\\drive a\tools\metal") / 1000 / 1000 / 1000, 2)
. If i then attempt to run the entire code that you have been helping me to shorten, it literally comes to a halt and doesnt do anything. I have also tested my statements one by one and it seems that the dirgetsize starts out slow and finally doesnt do anything...even i put a sleep statement it doesnt help anything...

Edited by goodbyeplanet
Link to comment
Share on other sites

If we're going to make it more complicated for the sake of making it complicated I want in on it!

As mentioned by Fubarable. You could replace the array declarations with INI file reads or whichever for external data.

#include <file.au3>
#include <Array.au3>

Global $sLogPath = "c:\mylogfile.txt"
Global $asLogMsg[1] = ["New Log File Entry - Start"]

Global $iDirTotal
Global $asDirList[4][3] = [["\\drive a\tools\metal", "n"],["\\drive a\tools\plastic", "m"], _
        ["\\drive a\tools\metal1", "o"],["\\drive a\tools\plastic2", "p"]]

Global $asTableList[3][2] = [["\\drive a\tables\big table"],["\\drive a\tables\small table"],["\\drive a\tables\flat table"]]

For $i = 0 To UBound($asDirList) - 1
    $asDirList[$i][2] = Round(DirGetSize($asDirList[$i][0]) / 1000 / 1000 / 1000, 2)
    $iDirTotal += $asDirList[$i][2]
    _ArrayAdd($asLogMsg, "drive     " & $asDirList[$i][1] & "  (" & _GetFolderName($asDirList[$i][0]) & ")   :  " & $asDirList[$i][2] & " GiB")
Next

_ArrayAdd($asLogMsg, "total space used   : " & $iDirTotal & " GiB")

For $i = 0 To UBound($asTableList) - 1
    $asTableList[$i][1] = Round(DirGetSize($asTableList[$i][0]) / 1000 / 1000 / 1000, 2)
    _ArrayAdd($asLogMsg, _GetFolderName($asTableList[$i][0]) & "      :    " & $asTableList[$i][1] & " GiB")
Next

For $i = 0 To UBound($asLogMsg) - 1
    _WriteLog($asLogMsg[$i])
Next

Func _GetFolderName($asPath)
    $sFolder = StringSplit($asPath, "\")
    Return $sFolder[$sFolder[0]]
EndFunc   ;==>_GetFolderName

Func _WriteLog($sLogMsg)
    _FileWriteLog($sLogPath, $sLogMsg)
EndFunc   ;==>_WriteLog
Link to comment
Share on other sites

now why is is that this command seems to be extremely slow

$metal = Round(DirGetSize("\\drive a\tools\metal") / 1000 / 1000 / 1000, 2)
. If i then attempt to run the entire code that you have been helping me to shorten, it literally comes to a halt and doesnt do anything. I have also tested my statements one by one and it seems that the dirgetsize starts out slow and finally doesnt do anything...even i put a sleep statement it doesnt help anything...

I think DirGetSize must go through all subdirectories/files and calculate sum

so it can take a while especially if you use network drive.

Link to comment
Share on other sites

Minor (math) speed note:

round($x) -> int type.

but

round($x,n) -> float/double type.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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