Jump to content
Sign in to follow this  
mmletzko

StringReplace question

Recommended Posts

mmletzko

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

Share this post


Link to post
Share on other sites
Zedna

$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

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
mmletzko

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)

Share this post


Link to post
Share on other sites
mmletzko

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)

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
mmletzko

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!

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
mmletzko

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

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

Share this post


Link to post
Share on other sites
SmOke_N

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.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.