saywell Posted December 8, 2008 Posted December 8, 2008 Hi,I'm trying to make a password updater for a batch file.I need to replace -s"old password" with -s"new password"Where old password and new password are strings using any keyboard character except " or '.I can open the file, read it into a string (it's not very long) and, as a test, replace a fixed string with a new password entered from a text box, but that's not good enough, and I'm having great difficulty moving on to the next step.I've tried using "-s\"" & $newpassword & "\""and also '-s\"' & $newpassword & '"' (both directly in the stringreplace command, and also by setting them as an intermediate variable, and using that in the stringreplace).but neither works as the replacement string.What am I doing wrong?Also, I'm not sure how to select the string to replace - I had though "-s\"?\""or '-s"?"' but I think they will have the same problem (and I'm assuming that ? will select any number of any character between the two inverted commas?)Could anyone please help a newbie?!Thanks,William
Pain Posted December 8, 2008 Posted December 8, 2008 (edited) I'm not that good with StringRegExp so here is another version. #include <String.au3> $string = '-s"old password"' $new = 'new password' $aArray = _StringBetween($string, 's"', '"') ConsoleWrite(StringReplace($string, $aArray[0], $new)) Edited December 8, 2008 by Pain
saywell Posted December 8, 2008 Author Posted December 8, 2008 I'm not that good with StringRegExp so here is another version. #include <String.au3> $string = '-s"old password"' $new = 'new password' $aArray = _StringBetween($string, 's"', '"') ConsoleWrite(StringReplace($string, $aArray[0], $new)) Thanks - a different approach that looks basically simpler. However, it didn't work. I've adapted your script to my variables, and broken it down with a msbox to see what's happening at each stage. $password is the new password $sRead is the string read in from the file script snippet:- #include <String.au3> $start = ('-s"') MsgBox (0, "test $start", $start ) $end = ('" ') MsgBox (0, "test $end", $end ) $aArray = _StringBetween($sRead, $start, $end) MsgBox (0, "test $aArray", $aArray ) StringReplace($sRead, $aArray, $password) end snippet The message boxes show $sRead, $start and $end as intended but $aArray fails to show the text between $start and $end. What have I done wrong? William
Pain Posted December 8, 2008 Posted December 8, 2008 (edited) #include <String.au3> $sRead = '-s"old password"' $password = 'new password' $start = '-s"' ;~ MsgBox (0, "test $start", $start) $end = '"' ;~ MsgBox (0, "test $end", $end) $aArray = _StringBetween($sRead, $start, $end) ;~ MsgBox (0, "test $aArray", $aArray ) ; You can't show an array without UBound, use UBound in a loop or _ArrayDisplay($aArray) MsgBox(0, "", StringReplace($sRead, $aArray[0], $password)); Same as above, you can't show an array if you don't pick an element. In this case it's 0 Edited December 8, 2008 by Pain
saywell Posted December 8, 2008 Author Posted December 8, 2008 Sorry, you've lost me there... So I can't display the array easily. How do I replace the array in the original string with the new password string? (Trying to display the array was only so I could debug) William
Pain Posted December 8, 2008 Posted December 8, 2008 I guess I have to do it all for you, good for you I feel generous today. Uncomment the 3 last lines to save it back to your batch file. #include <String.au3> $sRead = 'dfg dgd gdgsfsfdghdgsdg -s"old password"sf sdfsdfsdfsdfsdf'; the text from FileRead() $password = 'new password'; I hope it's obvious what this is... $start = '-s"'; where to start search from $end = '"'; where to end the search at $aArray = _StringBetween($sRead, $start, $end); search in the string MsgBox(0, "", $aArray[0]); found the old password $newString = StringReplace($sRead, $aArray[0], $password); replace MsgBox($newString); the new string ;~ $file = FileOpen("test.txt", 1) ;~ FileWrite($file, $newString) ;~ FileClose($file)
saywell Posted December 9, 2008 Author Posted December 9, 2008 I guess I have to do it all for you, good for you I feel generous today. Uncomment the 3 last lines to save it back to your batch file. #include <String.au3> $sRead = 'dfg dgd gdgsfsfdghdgsdg -s"old password"sf sdfsdfsdfsdfsdf'; the text from FileRead() $password = 'new password'; I hope it's obvious what this is... $start = '-s"'; where to start search from $end = '"'; where to end the search at $aArray = _StringBetween($sRead, $start, $end); search in the string MsgBox(0, "", $aArray[0]); found the old password $newString = StringReplace($sRead, $aArray[0], $password); replace MsgBox($newString); the new string ;~ $file = FileOpen("test.txt", 1) ;~ FileWrite($file, $newString) ;~ FileClose($file) Many thanks, Pain. That works a treat. I'll just add a few lines to rename original file to xxx.old, create new file with name of the original, and write $sNewString to that. Just makes it safer if there's a glitch while the password is updating. We newbies are always grateful for the help of the more experienced - I've learned a lot doing this. Trouble is, I get too ambitious and keep attempting to do things beyond my current knowledge! Regards, William
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