Iraj Posted June 15, 2023 Share Posted June 15, 2023 Hello Greetings! I need some assistance in below code. I am trying to replace all the double quotes with single quotes for a text in a .sql file. I tried using StringReplace & RegReplace but no luck. #include <Date.au3> $sip = @IPAddress1 $user = "test-admin" $FileHandle = FileOpen(@ScriptDir &"\commands.sql", 2) FileWrite($FileHandle,"insert into serverAccess values ("""&@UserName&""","""&$sip&""","""&$user&""","""& _Now()&""")") StringReplace(FileRead($FileHandle), """", "'") Your assistance will be very helpful. Thanks Link to comment Share on other sites More sharing options...
Solution 20Ice18 Posted June 15, 2023 Solution Share Posted June 15, 2023 In your code, you are using FileOpen to open the commands.sql file and then FileWrite to write the SQL insert statement to the file. However, you are not closing the file before reading its content using FileRead and attempting to replace the double quotes. try this and please let me know if it worked : #include <Date.au3> $sip = @IPAddress1 $user = "test-admin" $filePath = @ScriptDir & "\commands.sql" $FileHandle = FileOpen($filePath, 2) If $FileHandle = -1 Then MsgBox(16, "Error", "Failed to open file.") Exit EndIf FileWrite($FileHandle, "insert into serverAccess values ('" & @UserName & "','" & $sip & "','" & $user & "','" & _Now() & "')") FileClose($FileHandle) $fileContent = FileRead($filePath) $fileContent = StringReplace($fileContent, '"', "'") $FileHandle = FileOpen($filePath, 2) If $FileHandle = -1 Then MsgBox(16, "Error", "Failed to open file.") Exit EndIf FileWrite($FileHandle, $fileContent) FileClose($FileHandle) ❤️ Link to comment Share on other sites More sharing options...
Andreik Posted June 15, 2023 Share Posted June 15, 2023 You are confused about what are you trying to achieve. The text is in the file or it's some text that you already have it in a variable? When the words fail... music speaks. Link to comment Share on other sites More sharing options...
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