Jump to content


Can I use IfInString for Carriage Returns and Form Feeds?


  • Please log in to reply
13 replies to this topic

#1 Steved

Steved
  • Guests

Posted 24 May 2004 - 04:44 PM

I know you can Send {asc 13}{asc 10}

But can you InString it?


Thanks,

#2 beardboy

beardboy
  • Members
  • 443 posts

Posted 24 May 2004 - 05:36 PM

Carriage Return = `r
Line Feed = `n

thanks,
beardboy

#3 Chris

Chris
  • Administrators
  • 10727 posts

Posted 24 May 2004 - 05:38 PM

I think that's CR+LF, so you can check for it this way. I ran this test to make sure it actually behaves correctly:
test = `n
IfInString, test, `r`n
	MsgBox Yes1
test = `r
IfInString, test, `r`n
	MsgBox Yes2
test = `r`n
IfInString, test, `r`n
	MsgBox Yes3
The above will display only Yes3.

Edit: You beat me :) I did the above test because I wasn't 100% sure that `r and/or `n would be treated as whitespace and auto-trimmed.

#4 BoBo

BoBo
  • Guests

Posted 05 August 2004 - 06:18 PM

... that `r and/or `n would be treated as whitespace and auto-trimmed.


Is it correct that the same won't work within a loop for the above reason?

Loop, Read, %MyFile%
{
IfInString, A_LoopReadLine, `r`n
MsgBox CR+LF
}

#5 Chris

Chris
  • Administrators
  • 10727 posts

Posted 05 August 2004 - 06:49 PM

It does work, I was just testing to be sure. Only literal spaces and tabs are automatically trimmed as described here: http://www.autohotke...ds/AutoTrim.htm

#6 BoBo

BoBo
  • Guests

Posted 05 August 2004 - 09:26 PM

I wasn't able to make it. :(

FileName = LFTest.txt

Loop, 10  ; 1st. I've created a file the way it's recommended in the help for testing.
{
FileAppend, Another line.`n, *C:\%FileName%
}
Run, explorer,C:\,Max

-----

Loop, 10  ; 2nd . I've used a FileReadLine loop to detect the line ending character, unsuccessfully

{
FileReadLine, Line, C:\%FileName%, %A_Index%
MsgBox, 0, LoopTest1, %Line%`nLine: %A_Index%, 1
	IfInString, Line,`r, MsgBox, CR 1
	IfInString, Line,`n, MsgBox, LF 1
}
ExitApp

-----

Loop, Read, %FileName% ; 3rd. I've used a LoopRead to detect the line ending character, unsuccessfully
{
MsgBox, 0, LoopTest2, %A_LoopReadLine%`nLine: %A_Index%, 1
	IfInString, A_LoopReadLine,`r, MsgBox, CR
	IfInString, A_LoopReadLine,`n, MsgBox, LF
}
ExitApp


#7 Chris

Chris
  • Administrators
  • 10727 posts

Posted 05 August 2004 - 09:37 PM

Sorry about that, I misinterpreted your post, thinking you were just asking for confirmation about the detection of `r`n. The answer is that `r and `n are both omitted from the output variable when you use either FileReadLine or a file-reading loop. In other words, you have to remember to include a `n (if desired) when writing the variable back out to a file using FileAppend.

Note: When a `n is written with FileAppend, the operating system internally translates it to `r`n, which is the standard end-of-line marker for Windows text files.

#8 BoBo

BoBo
  • Guests

Posted 05 August 2004 - 09:58 PM

Chris,
thx 4 that superfast response.
Primarily I want to accomplish the validation of an input file. If a customer has sent a file (e.g. from a Unix box, which includes an illegal char or lacks another one e.g. a CR) it should be rejected. In this case a conversion isn't needed nor allowed as any change on that (source) data would have to be explicitly confirmed by its owner, the customer.

Thx.

#9 Chris

Chris
  • Administrators
  • 10727 posts

Posted 06 August 2004 - 10:46 AM

Since lines ending in either LF or CR+LF are both seen the same way when reading them, there is currently no direct way in the syntax to determine which is present. However, you might be able to do it indirectly as follows:

line_count = 0
file_size_calc = 0
Loop, read, MyFile.txt
{
     line_count++
     StringLen, line_length, A_LoopReadLine
     file_size_calc += line_length
}
file_size_calc += line_count  ; Add one LF to the count for each line (Unix).
FileGetSize, file_size, MyFile.txt
if file_size > file_size_calc
     MsgBox The actual file size (%file_size%) is greater than expected (%file_size_calc%). It probably contains illegal CRs.
The above is untested and might get thrown off a little if the very last line in the file lacks an LF entirely. An alternative would be to use a command line utility that searches an entire file for a specific character, namely CR. Finally, you could rewrite the file entirely in Unix mode to ensure it is a clean Unix file. This would be done by including an asterisk in the file-reading loop:
; Recreate the file:
Loop, read, MyFile.txt, *MyFileNew.txt  ; Write in binary vs. text mode.
     FileAppend, %A_LoopReadLine%`n  ; `n tranlates to plain LF in binary mode.
None of the above examples checks for naked CR's, which I believe are typical only in a Macintosh environment. You could use IfInString, string, `r to explicitly check for CRs that lack an LF.

#10 Guests

  • Guests

Posted 06 August 2004 - 12:48 PM

Finally, you could rewrite the file entirely in Unix mode


Hey dude, I've done that :D see my post from 10:26 8)

:!: Thanks for the time you spend to support us :!:

#11 BoBo

BoBo
  • Guests

Posted 06 August 2004 - 03:47 PM

Loop, Parse, InputVar [, Delimiters, OmitChars, FutureUse]

Well, what about H aka Hex as a first "FutureUse" param ? :wink:
Means, A_LoopField won't return the field content, but its Hex Value.
So a LF/CR char should be easily detectable !(?)

Thx.

#12 BoBo

BoBo
  • Guests

Posted 06 August 2004 - 05:23 PM

OK. :cry: The LF\CR are not parsed as single characters , therefore it's stupid to expect its Hex value ... :roll:

#13 Chris

Chris
  • Administrators
  • 10727 posts

Posted 06 August 2004 - 08:34 PM

You can't even get that far because both FileReadLine and a file-reading loop omit the CR+LF from the output string. In other words, it is impossible get an LF (Unix format) or a CR+LF (Windows format) when reading a file with AHK. However, I believe it is possible to get a naked CR (Macintosh format) if a file contains any.

Means, A_LoopField won't return the field content, but its Hex Value.

You can get the hex value of a character as in this example:
Transform, OutputVar, Asc, A ; Get the ASCII code of the letter A.

Actually, the above gets it in decimal vs. hex format, but you could insert a SetFormat, integer, hex right before the command, which fetches hex instead.

#14 BoBo

BoBo
  • Guests

Posted 06 August 2004 - 09:32 PM

Hope Dies Last :wink:
Thx again. Really appreciate your effort to support us.