Jump to content

Replace double quotes with single quotes


Go to solution Solved by 20Ice18,

Recommended Posts

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

  • Solution

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

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

×
×
  • Create New...