Jump to content
Sign in to follow this  
williamk

Replace String in File removing paragraphs

Recommended Posts

williamk

Hi,

Have a text file that is full of tabs, spaces, and carriage returns. I am trying to remove all those with the following code:

include <GUIConstants.au3>
#include <File.au3>
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @TAB, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @LF, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", ' ', '')

The spaces and tabs are removed, but the carriage returns remain. I have tried using @CR, @CRLF, and @LF, but none of those work. If I copy and paste my resulting text into Microsoft Word and show the formatting marks I see the backward P symbol which I believe means "Paragraph". I thought a carriage return and backwards P were the same thing. So how do I remove those?

Share this post


Link to post
Share on other sites
evilertoaster

have you tried StringStripWS() and StringStringCR()?

edit: beat...

Edited by evilertoaster

Share this post


Link to post
Share on other sites
williamk

Hey Guys,

I tried the commands you suggested, but still can't get them to go away. Here is the code I tried. Did I code it right?

#include <GUIConstants.au3>
#include <File.au3>
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @TAB, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @LF, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", ' ', '')
$file = FileOpen("C:\sqlreports\copies\Image_Result.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
    $chars = FileRead($file)
    If @error = -1 Then ExitLoop
    StringStripCR ($chars)
    StringStripws($chars, 8)
    MsgBox(0, "Char read:", $chars)
FileClose($file)

Share this post


Link to post
Share on other sites
evilertoaster

can you upload the txt file your using?

Share this post


Link to post
Share on other sites
williamk

can you upload the txt file your using?

Sure can. Here is the file I'm trying to modify with all the spaces, tabs, and character returns included. Thanks.

Copy_of_Image_Result.txt

Share this post


Link to post
Share on other sites
ResNullius

Hey Guys,

I tried the commands you suggested, but still can't get them to go away. Here is the code I tried. Did I code it right?

#include <GUIConstants.au3>
#include <File.au3>
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @TAB, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", @LF, '')
_ReplaceStringInFile ("C:\sqlreports\copies\Image_Result.txt", ' ', '')
$file = FileOpen("C:\sqlreports\copies\Image_Result.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
    $chars = FileRead($file)
    If @error = -1 Then ExitLoop
    StringStripCR ($chars)
    StringStripws($chars, 8)
    MsgBox(0, "Char read:", $chars)
FileClose($file)

Two problems: first, looking inside the File.au3 UDF it appears that the _ReplaceStringInFileFunction preserves @CRLF, @CR, and @LF, so that's why that's not going to work for you. Better off to use the regular StringReplace function.

Second, in the last part of your script, your StringStripWS() and StringStripCR() aren't ByRef functions, you need to make your $Chars variable capture the results of them. So, this modification of your script works for me on your test file:

#include <GUIConstants.au3>
$file = FileOpen("Image_Result.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
    $chars = FileRead($file)
    If @error = -1 Then ExitLoop
    $chars = StringReplace($Chars, @TAB, '')
    $chars = StringReplace($Chars, @LF, '')
    $chars = StringReplace($Chars, ' ', '')
    $chars = StringStripCR ($chars)
    MsgBox(0, "Char read:", $chars)
FileClose($file)

Note the $chars= prefixes on the StringReplace and StringStrip lines.

Also , you might want to think about the use of StringStripWS($chars,8) and StringReplace($Chars, ' ', '') as that will even remove spaces from within/between words. IE: if your file said "yakima washington" it would return that as "yakimawashington". If you're just wanting to remove superflous spaces, better to run a StringReplace on " " with " ". Unless you don't have that kind of data, in which case, never mind.

Share this post


Link to post
Share on other sites
williamk

Live and learn. Both ways suggested work, just have to figure out which will work best for what I am shooting for. This is just a small part of a project I am working on. One of these days I need to sit down and actually learn how to write code. I'm just the type that would rather learn by doing and ask questions when I get stuck rather than read a book. Thanks much guys.

Share this post


Link to post
Share on other sites
williamk

Ok now another little problem with this. Change of plan. I have been able to strip the file down so it looks like this:

7562078a.tif

7562078b.tif

7562131a.tif

7562131b.tif

7562263a.tif

7562264a.tif

7562265a.tif

7562266a.tif

7562266b.tif

7562470aa.tif

7562470ab.tif

7562470ac.tif

7562866a.tif

7562867a.tif

7562878a.tif

7562878b.tif

7563074a.tif

7563074b.tif

7563074c.tif

7563074d.tif

7563346a.tif

7563346b.tif

7563684a.tif

7563764a.tif

7563782a.tif

I need to remove all the .tifs as well as any non numeric characters. After that is done I want to remove all the duplicate number lines so I have a list of unique numbers. I can't trim the string from the left becuase sometimes I would need to trim 5 characters and other times 6.

The .tif is easy to remove but how to get rid of the non numerics and the duplicates has me stumped. Any ideas?

Share this post


Link to post
Share on other sites
ResNullius

I need to remove all the .tifs as well as any non numeric characters. After that is done I want to remove all the duplicate number lines so I have a list of unique numbers. I can't trim the string from the left becuase sometimes I would need to trim 5 characters and other times 6.

The .tif is easy to remove but how to get rid of the non numerics and the duplicates has me stumped. Any ideas?

Example to remove non-numerics from a line (will take care of the .tif extensions too):

$line = "7562078a.tif"
$line = StringRegExpReplace($line,"\D","")
msgBox(0,"",$line)

Note: the "\D" is case sensitive, see StringRegExp() in the help file.

For removing duplicates, see this for an idea & code: http://www.autoitscript.com/forum/index.ph...st&p=321713

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  

×