Can I use IfInString for Carriage Returns and Form Feeds?
#1
Steved
Posted 24 May 2004 - 04:44 PM
But can you InString it?
Thanks,
#2
Posted 24 May 2004 - 05:36 PM
Line Feed = `n
thanks,
beardboy
#3
Posted 24 May 2004 - 05:38 PM
test = `n IfInString, test, `r`n MsgBox Yes1 test = `r IfInString, test, `r`n MsgBox Yes2 test = `r`n IfInString, test, `r`n MsgBox Yes3The 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
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
Posted 05 August 2004 - 06:49 PM
#6
BoBo
Posted 05 August 2004 - 09:26 PM
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
Posted 05 August 2004 - 09:37 PM
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
Posted 05 August 2004 - 09:58 PM
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
Posted 06 August 2004 - 10:46 AM
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
Posted 06 August 2004 - 12:48 PM
Finally, you could rewrite the file entirely in Unix mode
Hey dude, I've done that
:!: Thanks for the time you spend to support us :!:
#11
BoBo
Posted 06 August 2004 - 03:47 PM
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
Posted 06 August 2004 - 05:23 PM
#13
Posted 06 August 2004 - 08:34 PM
You can get the hex value of a character as in this example:Means, A_LoopField won't return the field content, but its Hex Value.
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
Posted 06 August 2004 - 09:32 PM
Thx again. Really appreciate your effort to support us.




