Jump to content
Sign in to follow this  

Replace empty space with comma in txt file

Recommended Posts

Hey, I need some help from you guys.

For example my text

text just text wait wut
after script work:

Maybe I need to use StringRegExp()? I never used it before.

So ok my code:

Global $pw = FileOpen("textwithspaces.txt")
Global $hOutPutFile = FileOpen("nospaces.txt", $FO_OVERWRITE)

$hash = FileRead($pw)

;I need array loop or something?
;I would try to read line by line and use stringinstr or smth but I beleive there is faster way


Share this post

Link to post
Share on other sites

Easy task ;)

#include <File.au3>

$sData = "text just text wait wut"
$FileName = "Test.txt"

$hFile = FileOpen($FileName, 2)
FileWrite($hFile, $sData)

$hFileOpen = FileOpen($FileName)
$ReadData = FileRead($hFileOpen)
$sNewFile = StringReplace($ReadData, chr(32), ',')
$hFileOpen = FileOpen($FileName, 2)
FileWrite($hFileOpen, $sNewFile)

Edited by MyEarth

Share this post

Link to post
Share on other sites


In case there are multiple spaces between the tokens, a comma already present or you want to span multiple lines.

#include <Constants.au3>


; create test file

$str = 'token1     token2 token3 token4      token5' & @crlf & _
       'token6,  token7,token8' & @crlf & _
       'token9' & @crlf & _
       'token10,token11      token12'

local $hfl = fileopen (@scriptdir & '\test.txt',2)
filewrite ($hfl,$str)

; read file and replace all spaces between words with a ',', if one does not exist

msgbox($mb_ok,'Text Before',fileread(@scriptdir & '\test.txt'))
msgbox($mb_ok,'Text After ',stringregexpreplace(fileread(@scriptdir & '\test.txt'),'\b(\x20)+\b',','))


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post

Link to post
Share on other sites

good call...or:

$string = stringRegExpReplace ("text just text wait wut", chr(32)&"+",",")

although, yours would be good if you don't want to replace leading/trailing spaces

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...