Jump to content
Sign in to follow this  
legend

reverse file

Recommended Posts

legend

Hi.

I got this script that will take 1.txt and reverse the text and write it to 2.txt:

$sText = FileRead('1.txt')
$timer = TimerInit()
$aText = StringSplit($sText, '')
$sText = ''
For $i = $aText[0] To 0 Step -1
$sText &= $aText[$i]
Next
Local $file = FileOpen("2.txt", 1)
FileWrite($file, $sText)

The thing is, how would I make it do the opposite thing?

so make a 3.txt where it instead of string split, then joins it, to make the reverse, normal text again.

(also the reversed text is being put in just 1 line, and not 2, if there is 2 lines, in 1.txt)

Edited by legend

Share this post


Link to post
Share on other sites
JohnOne

I don't know why but although it reads an enter press as @LF when written back to file, it needs to be @CRLF.the reverse process is the same process on reversed string.

$sText = FileRead('1.txt')
$timer = TimerInit()
$aText = StringSplit($sText, '', 2)

$sText = ''
For $i = UBound($aText) -1  To 0 Step -1
If $aText[$i] = @LF Then
$sText = @CRLF
Else
$sText = $aText[$i]
EndIf
Next
Local $file = FileOpen("2.txt", 1)
FileWrite($file, $sText)

Also _StringReverse() <- help file.

EDIT: if you opt not to use _StringReverse() then kylomas code below is safer.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
kylomas

@johnone,

Because @CRLF is two chars and stringsplit with a blank delimiter will split on each character. You should be seeing both @lf's and @cr's in the split file.

kylomas


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
kylomas

legend,

Here's one way to do this

local $str = 'a bunch of random characters' & @crlf & 'some more characters' & @lf & @CR & 'yet another line' & @crlf & 'finally, the last line'
if fileexists(@scriptdir & 'temp.txt') then filedelete(@scriptdir & 'temp.txt')
filewrite(@scriptdir & 'temp.txt',$str)
$sText = FileRead(@scriptdir & 'temp.txt')
msgbox(0,'',$stext)
$reverse = _reverse_file($sText)
msgbox(0,'',$reverse)
$reverse = _reverse_file($reverse)
msgbox(0,'',$reverse)
func _reverse_file($str)
 $str = stringreplace($str,@crlf,0x0)
 $str = stringreplace($str,@lf,0x1)
 $str = stringreplace($str,@cr,0x2)
 local $aText = StringSplit($str, '')
 $str = ''
 For $i = $aText[0] To 1 Step -1
  $str &= $aText[$i]
 Next
 $str = stringreplace($str,string(0x0),@crlf)
 $str = stringreplace($str,string(0x1),@lf)
 $str = stringreplace($str,string(0x2),@cr)
 return $str
endfunc

kylomas


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
kylomas

JohnOne,

Yes, I'm sure it does. I've just presented another way to do it, preserving mixed EOL characters, if they exist.

Splitting @CRLF (0x0d & 0x0a) should result in two string characters as demonstrated by this code

#include <array.au3>
local $a10 = stringsplit(@crlf,'',2)
for $i = 0 to ubound($a10) - 1
consolewrite('+ element # ' & $i & ' = ' & string(asc($a10[$i])) & @lf)
next

kylomas

edit: the string function within the consolewrite is not neccessary

Edited by kylomas

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

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  

×