Jump to content

Removing double quotes from CSV


Go to solution Solved by Skeletor,

Recommended Posts

Hi All,

I've been staring at this for long and have no idea how to resolve this.

I have a csv that Powershell created. It produces double quotes around the file. Im want to remove the double quotes using StringRegExpReplace.

$PShell_Path = "GetIPAddress.PS1"
FileWrite("GetIPAddress.PS1", "Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress | Export-Csv -Path 'results.csv' -NoTypeInformation")
$command = "powershell.exe -ExecutionPolicy Bypass -File " & $PShell_Path
Run(@ComSpec & " /c " & $command, "", @SW_HIDE)

; Read the contents of the CSV file into a string
Global $sContents = FileRead("results.csv")

; Replace all double quotes with a single quote using StringRegExpReplace()
$sContents = StringRegExpReplace($sContents, '"', "'", "G")

; Write the modified contents back to the CSV file
FileWrite("results.csv", $sContents)

I dont see the result, $sContents provides a blank file or doesnt even write to the file.

Kind Regards
Skeletor

"I need coffee to turn me back to a human"

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen

Link to post
Share on other sites
  • Solution

Thanks @ajag, removed it, but the actual issue was infact a delay in file creation.

I used RunWait and used brackets in my regex for the quotes and this resolved my issue.

$PShell_Path = "GetIPAddress.PS1"
FileWrite("GetIPAddress.PS1", "Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress | Export-Csv -Path 'results.csv' -NoTypeInformation")
$command = "powershell.exe -ExecutionPolicy Bypass -File " & $PShell_Path
RunWait(@ComSpec & " /c " & $command, "", @SW_HIDE)

; Read the contents of the CSV file into a string
Global $sContents = FileRead("results.csv")
; Replace all double quotes with a single quote using StringRegExpReplace()
$sContents = StringRegExpReplace($sContents, '["]', "")
; Write the modified contents back to the CSV file
FileOpen("results.csv", 2)
FileWrite("results.csv", $sContents)
FileClose("results.csv")

 

Edited by Skeletor

Kind Regards
Skeletor

"I need coffee to turn me back to a human"

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen

Link to post
Share on other sites

Hi Skeletor
Glad you solved your issue. I got a question : why are the brackets needed ?

If I run the following script, then I got the same result, with or without brackets :

$sSubject = '"123"' & "|" & '"456"'
ConsoleWrite($sSubject & @crlf & @crlf)

$sContent1 = StringRegExpReplace($sSubject, '"', "")
ConsoleWrite($sContent1 & @crlf)

$sContent2 = StringRegExpReplace($sSubject, '["]', "")
ConsoleWrite($sContent2 & @crlf)

Scite Console :

"123"|"456"

123|456
123|456

 

Link to post
Share on other sites

Excellent pixelsearch, thanks for picking this up. I was actually browsing the help files to find a solution and throwing everything into it. I seriously do not understand these expressions.

Kind Regards
Skeletor

"I need coffee to turn me back to a human"

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...