boji Posted April 1, 2008 Posted April 1, 2008 Hello! I am working on my first major script and I have some questions for all you veterans here. I'm worried my syntax is inefficient or just plain dumb. Would you mind reviewing my code and making any suggestions? Thank you so much! Below is my attempt to trim and strip a string, and prepare it for StringCompare(FileRead($sFile1), FileRead($sFile2)) Warning, there may be wrongheaded or unnecessary code here, so thanks for your patience. =) $sFile1 = "TM Difference File1.txt" $sFile2 = "TM Difference File2.txt" IF $sFile1 Then FileDelete($sFile1) EndIf IF $sFile2 Then FileDelete($sFile2) EndIf $var1 = ClipGet() FileOpen($sFile1, 1) FileWriteLine($sFile1,$var1) FileClose($sFile1) $var2 = ControlGetText("TM", "Contact Manager Toolbar", "") If $sFile2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileOpen("TM Difference File2.txt", 1) $trim10 = StringTrimLeft($var2, 10) $var2 = $trim10 ;<-------------------------not sure why I need this but it seems to make it work >.< $strip = StringStripWS($var2, 2) $var2 = $strip ;<----------------------------not sure why I need this but it seems to make it work >.< FileWriteLine($sFile2,$var2) FileClose($sFile2)oÝ÷ Øíz»aË-®)àb¶Wvɶç- «JÚ⨫xX¥ywMú°X¥{Qbä^iÝ7êÁbí When I then try to use $sFile1 modified, and query the database as such: $data2 = StringSplit($var1, "");Autotypes clipboard For $I = 1 To Ubound($data2)-1 Sleep(10);increasing sleep time slows typing into clipboard Send($data2[$I]) It does not seem to give me the modified string, it keeps the old string, $var1 = ClipGet() Any help would of course be immensely appreciated, and thanks for supporting this very helpful forum!
weaponx Posted April 1, 2008 Posted April 1, 2008 (edited) This can be shortened in various ways: $trim10 = StringTrimLeft($var2, 10) $var2 = $trim10;<-------------------------not sure why I need this but it seems to make it work >.< $strip = StringStripWS($var2, 2) $var2 = $strip;<----------------------------not sure why I need this but it seems to make it work >.< Could be... $var2 = StringTrimLeft($var2, 10) $var2 = StringStripWS($var2, 2) - or - $var2 = StringStripWS(StringTrimLeft($var2, 10), 2) This code can also be shortened: Select Case StringCompare(FileRead($sFile1), FileRead($sFile2)) = 1 Unison_TM_do_not_match() Case StringCompare(FileRead($sFile1), FileRead($sFile2)) = -1 is_ampersand_present() Case StringCompare(FileRead($sFile1), FileRead($sFile2)) = 0 Copy_Company_into_notepad() Case Else EndSelect Could be... Switch StringCompare(FileRead($sFile1), FileRead($sFile2)) Case 1 Unison_TM_do_not_match() Case -1 is_ampersand_present() Case 0 Copy_Company_into_notepad() EndSwitch P.S. - This Line: FileOpen("TM Difference File2.txt", 1) Should be... FileOpen($sFile2, 1) Edited April 1, 2008 by weaponx
boji Posted April 1, 2008 Author Posted April 1, 2008 (edited) lol ok you are amazing, that response took all of 30sec. I'll def take your simplifications to task. However I'm still a bit confused about how to get the $sFile1 after modification $retval = _ReplaceStringInFile($sFile1,$find,$replace) To be something I can run $data2 = StringSplit($var1, "") For $I = 1 To Ubound($data2)-1 Sleep(10) Send($data2[$I]) I'm afraid to post the entire script, as it will reveal my noobness, but I'd rather have a working script and be made fun of, than have a boneheaded program and a silly ego . Would anyone mind if I put it here? Edited April 1, 2008 by boji
weaponx Posted April 1, 2008 Posted April 1, 2008 It would help if you posted more since I can't really grasp whats happening with the various snippets. I'm guessing you just need to do another FileRead() after you write your file out to reload its values.
boji Posted April 1, 2008 Author Posted April 1, 2008 The code is part of a much larger project where I will have tools to make query of multiple databases more efficient. I work at a call center and we have 2 dbase systems that never match, in part due to user error. I cannot modify files at the datalayer because our systems are on nodes and I do not have the authority manipulate at that level I am forced to work at the application layer and run things during off peak hours. This is one tool where I query dbase1 and open another dbase (dbase2), use the query from dbase1's output to make another query in dbase2, and then compare files so as to get them to match. Ok. here goes. Please keep this in context; this is my first real program after "hello world" and I have little programming experience prior to this, so expect silly redundancies, unnecessary lines and confounding logic. >.< I'm due for a C++ class in the summer so thank you for any early schooling you'd like to give me here. CODE#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=..\Au3 Macros\Unison to TM nfo.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** Opt("WinWaitDelay",100) Opt("WinTitleMatchMode",4) Opt("WinDetectHiddenText",1) Opt("MouseCoordMode",0) #include <File.au3> $sFile1 = "TM Difference File1.txt" $sFile2 = "TM Difference File2.txt" IF $sFile1 Then FileDelete($sFile1) EndIf IF $sFile2 Then FileDelete($sFile2) EndIf ;The two Below Files will be compared ;If they match then forward program to add Telemagic Entry ;If they do not match then begin steps to identifying discrepancy ;with future functions ;=================================================== _FileCreate("TM Difference File1.txt") _FileCreate("TM Difference File2.txt") ;=================================================== WinWait("Customer Search and Entry","") If Not WinActive("Customer Search and Entry","") Then WinActivate("Customer Search and Entry","") WinWaitActive("Customer Search and Entry","") MouseMove(367,159,0) MouseDown("left") MouseUp("left") MouseMove(371,159,0) MouseDown("left") MouseMove(99,159,3) MouseUp("left") Send("{CTRLDOWN}c{CTRLUP}") Sleep(1000) $var1 = ClipGet() If $sFile1 = -1 Then MsgBox(4096, "Error", "Unable to open file", 2) Exit EndIf IF $var1 = "" Then MsgBox(0, "Error", "No Customer Found, Please Enter Customer in Unison and try again.") Exit EndIf FileOpen($sFile1, 1) FileWriteLine($sFile1,$var1) FileClose($sFile1) If Not WinActive("TeleMagic Enterprise","") Then WinActivate("TeleMagic Enterprise","") WinWaitActive("TeleMagic Enterprise","") MouseMove(22,67,0) MouseDown("left") MouseUp("left") $winact1 = Winactive("Contact Manager","") IF $winact1 = 1 Then MouseMove(132,99) MouseDown("left") MouseUp("left") MsgBox(4096, "Error", "", 2) Sleep(5000) If Not WinActive("TeleMagic Enterprise","Home for Help") Then WinActivate("TeleMagic Enterprise","Home for Help") WinWaitActive("TeleMagic Enterprise","Home for Help") MouseMove(547,124,0) MouseDown("left") MouseUp("left") EndIf IF $winact1 = 0 Then WinActive("TeleMagic Enterprise","Home for Help") WinWaitActive("TeleMagic Enterprise","Home for Help") MouseMove(547,124,0) MouseDown("left") MouseUp("left") EndIf $autotype_tm_data = StringSplit(ClipGet(), "");Autotypes clipboard For $I = 1 To Ubound($autotype_tm_data)-1 Sleep(10);increasing sleep time slows typing into clipboard Send($autotype_tm_data[$I]) Next Send("{ENTER}");Enters off of TM Query, auto populates company and contacts MouseMove(526,126,3) MouseDown("left") MouseUp("left") Sleep(3000);this sleep allows File2 ControlGetText to be read ;WinWaitActive("TeleMagic Enterprise","") ;====================== ;Telemagic Query 1 Finished, Next Identify "&" and Look for differences in TM and Unison Cust name ;====================== $var2 = ControlGetText("TeleMagic Enterprise", "Contact Manager Toolbar", "") ;ControlGetText("TeleMagic Enterprise", "Contact Manager Toolbar", "") ; Check if file opened for reading OK If $sFile2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileOpen("TM Difference File1.txt", 1) $var2 = StringStripWS(StringTrimLeft($var2, 10), 2) FileWriteLine($sFile2,$var2) FileClose($sFile2) $sns1 = StringInStr($sFile1,"&") ;====================== ;Compare TM Dif File 1 and File 2 ;====================== Run("notepad.exe " & $sFile1);for debugging Run("notepad.exe " & $sFile2);for debugging Switch StringCompare(FileRead($sFile1), FileRead($sFile2)) Case 1 Unison_TM_do_not_match() Case -1 is_ampersand_present() Case 0 Copy_Company_into_notepad() EndSwitch ;if two files are equal no need to look for ampersand ;if it finds no ampersand then this defaults to Unison_TM_do_not_match() ;Unison (file1) is greater than TM then its probably not an "& space" issue so use Unison_TM_do_not_match() (future logic) ;however if Unison (file1) is less than TM (file2) then it might be that TM has spaces btw ampersand in company. ; this will need to frst be proven, then modified in TM. After modification, another difference file will need to be run to prove match before save to tm Func Copy_Company_into_notepad() WinWait("TM DATAENTRY.txt - Notepad","") If Not WinActive("TM DATAENTRY.txt - Notepad","") Then WinActivate("TM DATAENTRY.txt - Notepad","") WinWaitActive("TM DATAENTRY.txt - Notepad","") MouseMove(479,64,1) MouseDown("left") MouseUp("left") Send("{CTRLDOWN}v{CTRLUP}{ENTER}d{ENTER}p{ENTER}n{ENTER}misc{ENTER}misc{ENTER}n{ENTER}n") Exit EndFunc ;====================================================================================== Func is_ampersand_present() ;$findnone = "" $find = "&" $replace = " & " msgbox(0,"Before",$sFile1) $retval = _ReplaceStringInFile($sFile1,$find,$replace) msgbox(0,"After",$sFile1) ;msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $File3 & " Error: " & @error) If $retval = -1 then Unison_TM_do_not_match() Else msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " & $find & " in the file: " & $sFile1) Select Case StringCompare(FileRead($sFile1), FileRead($sFile2)) = 0 lookagain() Case Else Unison_TM_do_not_match() EndSelect EndIf EndFunc Func lookagain() If Not WinActive("TeleMagic Enterprise","") Then WinActivate("TeleMagic Enterprise","") WinWaitActive("TeleMagic Enterprise","") MouseMove(22,67,0) MouseDown("left") MouseUp("left") EndIf $winact1 = Winactive("Contact Manager","") IF $winact1 = 1 Then MouseMove(132,99) MouseDown("left") MouseUp("left") MsgBox(4096, "Error", "", 2) Sleep(5000) If Not WinActive("TeleMagic Enterprise","Home for Help") Then WinActivate("TeleMagic Enterprise","Home for Help") WinWaitActive("TeleMagic Enterprise","Home for Help") MouseMove(547,124,0) MouseDown("left") MouseUp("left") EndIf IF $winact1 = 0 Then WinActive("TeleMagic Enterprise","Home for Help") WinWaitActive("TeleMagic Enterprise","Home for Help") MouseMove(547,124,0) MouseDown("left") MouseUp("left") EndIf $autotype_tm_data2 = StringSplit($var2, "");Autotypes clipboard For $I = 1 To Ubound($autotype_tm_data2)-1 Sleep(10);increasing sleep time slows typing into clipboard Send($autotype_tm_data2[$I]) Next Send("{ENTER}");Enters off of TM Query, auto populates company and contacts MouseMove(526,126,3) MouseDown("left") MouseUp("left") Sleep(3000);this sleep allows File2 ControlGetText to be read Copy_Company_into_notepad() ;====================== ;Telemagic Query 1 Finished, Next Identify "&" and Look for differences in TM and Unison Cust name ;====================== EndFunc ;$sFile3 = FileRead($File3, 1000) ;msgbox(0,"AFTER",$sFile3) ;FileDelete($File3) ;====================================================================================== Func Unison_TM_do_not_match() ; WinWait("TeleMagic Enterprise","") ; If Not WinActive("TeleMagic Enterprise","") Then WinActivate("TeleMagic Enterprise","") ; WinWaitActive("TeleMagic Enterprise","") ; ;If Not WinActive("TeleMagic Enterprise","Home for Help") Then MouseMove(549,125,3) MouseDown("left") MouseUp("left") If Not WinActive("TeleMagic Enterprise","Home for Help") Then WinActivate("TeleMagic Enterprise","Home for Help") WinWaitActive("TeleMagic Enterprise","Home for Help") MouseMove(749,188,1) MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseDown("left") MouseUp("left") MouseMove(748,188,0) MouseDown("left") MouseUp("left") EndFunc Exit
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now