Jump to content

StringReplace question


Recommended Posts

Trying to replace commas with a space in a file filled with directly paths that include commas, but its just hanging. What am I missing? I'm outputting to csv and the commas are throwing the columns off so I want to replace them before output.

$i=0
$dirlist=FileOpen("c:\temp\dirlist.txt",1)

While 1
$dirline=FileReadLine($dirlist) 
    If StringInStr($dirline,",") Then
        StringReplace($dirline,","," ")
    EndIf
    If @error=-1 Then ExitLoop
WEnd
Link to comment
Share on other sites

$dirline = StringReplace($dirline,","," ")

EDIT:

$i=0
$dirlist=FileOpen("c:\temp\dirlist.txt",1)

While 1
$dirline=FileReadLine($dirlist) 
    If @error=-1 Then ExitLoop
    If StringInStr($dirline,",") Then
        $dirline = StringReplace($dirline,","," ")
    EndIf
WEnd

but you don't use modified $dirline

Edited by Zedna
Link to comment
Share on other sites

  • Moderators

You're not changing anything...

Are you trying to write them back to the file?

Are you trying to store the values in a variable?

Local $s_no_comma = StringReplace(FileRead("c:\temp\dirlist.txt"), ",", " ")
FileWrite("c:\temp\no_comma_dirlist.txt", $s_no_comma)

Edit:

BTW, the reason you probably hang is you need to check the @error "After" FileReadLine() in your code, but it still won't work because you don't save StringReplace() to a variable.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks for the replies guys, but its still not working.

I modified the code to look like this:

$dirlist=FileOpen("c:\temp\dirlist.txt",1)

While 1
$dirline=FileReadLine($dirlist) 
    If @error=-1 Then ExitLoop
    If StringInStr($dirline,",") Then
        Local $s_no_comma = StringReplace(FileRead($dirlist), ",", " ")
            FileWrite($dirlist,$s_no_comma) 
    EndIF
WEnd
FileClose ($dirlist)
Link to comment
Share on other sites

I'll add all the relative code: Seems when I use the "1" option for the FileOpen it just loops. If I change it to a 0 (Read), the script finishes, but doesn't replace the strings, which would make sense since the file would only be open for reading.

#Include <Process.au3>
_RunDos("dir c:\temp\*. /s /b > c:\temp\dirlist.txt")
$dirlist=FileOpen("c:\temp\dirlist.txt",1)

While 1
$dirline=FileReadLine($dirlist) 
    If @error=-1 Then ExitLoop
    If StringInStr($dirline,",") Then
        Local $s_no_comma = StringReplace(FileRead($dirlist), ",", " ")
            FileWrite($dirlist,$s_no_comma) 
    EndIF
WEnd
FileClose ($dirlist)
Link to comment
Share on other sites

  • Moderators

I'll add all the relative code: Seems when I use the "1" option for the FileOpen it just loops. If I change it to a 0 (Read), the script finishes, but doesn't replace the strings, which would make sense since the file would only be open for reading.

#Include <Process.au3>
_RunDos("dir c:\temp\*. /s /b > c:\temp\dirlist.txt")
$dirlist=FileOpen("c:\temp\dirlist.txt",1)

While 1
$dirline=FileReadLine($dirlist) 
    If @error=-1 Then ExitLoop
    If StringInStr($dirline,",") Then
        Local $s_no_comma = StringReplace(FileRead($dirlist), ",", " ")
            FileWrite($dirlist,$s_no_comma) 
    EndIF
WEnd
FileClose ($dirlist)
Try running just this:

RunWait(@Comspec & " /c dir c:\temp\* /s /b > c:\temp\dirlist.txt", "", @SW_HIDE)
Local $s_read_file = FileRead("C:\temp\dirlist.txt")
FileClose(FileOpen("C:\temp\dirlist.txt", 2))
FileWrite("C:\temp\dirlist.txt", StringReplace($s_read_file, ",", " "))

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Try running just this:

RunWait(@Comspec & " /c dir c:\temp\* /s /b > c:\temp\dirlist.txt", "", @SW_HIDE)
Local $s_read_file = FileRead("C:\temp\dirlist.txt")
FileClose(FileOpen("C:\temp\dirlist.txt", 2))
FileWrite("C:\temp\dirlist.txt", StringReplace($s_read_file, ",", " "))
That worked Smoke.

You know, I actually originally had the "RunWait" with the @SW_HIDE option but it wasn't running when I used the @SW_HIDE option...ran fine without it. Strange.

I'll make the necessary adjustments and incorporate into my script.

Thanks again!

Link to comment
Share on other sites

  • Moderators

That worked Smoke.

You know, I actually originally had the "RunWait" with the @SW_HIDE option but it wasn't running when I used the @SW_HIDE option...ran fine without it. Strange.

I'll make the necessary adjustments and incorporate into my script.

Thanks again!

Your command was wrong... you had "*.", should have been "*" :D ...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Actually I only wanted folders, not files. Its working great now. :D

using the switch

/d

Could accomplish that for you.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...