Jump to content

Skiping Whitespace in a File


Recommended Posts

Say "a.txt" has some lines of text, but some lines are just "enter" (CR\LF). The first msg box should say the total number of lines there are, and the rest should give the line number of the lines without only whitespace. The idea is to skip blank lines. Any thoughts?

#include <file.au3>

$toolBarAddress = @DesktopDir & "\a.txt"
$lineCount = _FileCountLines($toolBarAddress)

MsgBox(0,"File Line Count", "The count: " & $lineCount)

$fileHandle = FileOpen($toolBarAddress, 0)
For $i = 1 to $lineCount
    $tempLineRead = FileReadLine($fileHandle, $i)
    If StringIsSpace($tempLineRead) Then ContinueLoop
    MsgBox(0, "The value of $i is:", $i)
Next
FileClose($fileHandle)
Edited by simpleinventor
Link to comment
Share on other sites

Say "a.txt" has some lines of text, but some lines are just "enter" (CR\LF). The first msg box should say the total number of lines there are, and the rest should give the line number of the lines without only whitespace. The idea is to skip blank lines. Any thoughts?

#include <file.au3>

$toolBarAddress = @DesktopDir & "\a.txt"
$lineCount = _FileCountLines($toolBarAddress)

MsgBox(0,"File Line Count", "The count: " & $lineCount)

$fileHandle = FileOpen($toolBarAddress, 0)
For $i = 1 to $lineCount
    $tempLineRead = FileReadLine($fileHandle, $i)
    If StringIsSpace($tempLineRead) Then ContinueLoop
    MsgBox(0, "The value of $i is:", $i)
Next
FileClose($fileHandle)
'StringStripWS'

'StringStripCR'

Right here

Link to comment
Share on other sites

maybe

#include <GUIConstants.au3>
#include <File.au3>


$toolBarAddress = @DesktopDir & "\a.txt"
$lineCount = _FileCountLines($toolBarAddress)

MsgBox(0,"File Line Count", "The count: " & $lineCount)

$fileHandle = FileOpen($toolBarAddress, 0)
For $i = 1 to $lineCount
    $tempLineRead = FileReadLine($fileHandle, $i)
    If StringIsSpace($tempLineRead) Then ContinueLoop
    If $tempLineRead = "" Then ContinueLoop
    MsgBox(0, "The value of $i is:", $i)
Next
FileClose($fileHandle)

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

Why not just:

For $i = 1 to $lineCount
    $tempLineRead = FileReadLine($fileHandle, $i)
    If StringStripWS($tempLineRead, 8) <> '' Then MsgBox(0, "The value of $i is:", $i)
Next

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.

Link to comment
Share on other sites

$toolBarAddress = @DesktopDir & "\a.txt"
$lineCount = 0
$emptylines = 0

$fileHandle = FileOpen($toolBarAddress, 0)

While 1
    $line = FileReadLine($fileHandle)
    If @error = -1 Then ExitLoop
    $linecount = $linecount + 1
    If StringLen($line) = 0 Then $emptylines = $emptylines + 1
Wend

FileClose($fileHandle)

MsgBox(4096,"INFO","Lines: " & $linecount & " Empty lines: " & $emptylines)

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

$toolBarAddress = @DesktopDir & "\a.txt"
$lineCount = 0
$emptylines = 0

$fileHandle = FileOpen($toolBarAddress, 0)

While 1
    $line = FileReadLine($fileHandle)
    If @error = -1 Then ExitLoop
    $linecount = $linecount + 1
    If StringLen($line) = 0 Then $emptylines = $emptylines + 1
Wend

FileClose($fileHandle)

MsgBox(4096,"INFO","Lines: " & $linecount & " Empty lines: " & $emptylines)

Cheers

Kurt

A space still gives the illusion of a non-empty line if your using StringLen() and all there is on that line is a space.

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.

Link to comment
Share on other sites

A space still gives the illusion of a non-empty line if your using StringLen() and all there is on that line is a space.

That's right, but his assumption was: "but some lines are just "enter" (CR\LF)." With just @CRLF on a line StringLen() will return 0. My code only works for the special requirements he mentioned in the first post.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Why not just:
For $i = 1 to $lineCount
    $tempLineRead = FileReadLine($fileHandle, $i)
    If StringStripWS($tempLineRead, 8) <> '' Then MsgBox(0, "The value of $i is:", $i)
NextoÝ÷ Ûú®¢×°)赫­¢+Ø(¥¹±Õ±Ðí¥±¹ÔÌÐì)¥´ÀÌØí±¥¹
½Õ¹Ð((ÀÌØíѽ½±  ÉÉÍÌôͭѽÁ¥ÈµÀìÅÕ½ÐìÀäÈí¹ÑáÐÅÕ½Ðì)}¥±IQ½ÉÉä ÀÌØíѽ½± ÉÉÍÌ°ÀÌØí±¥¹
½Õ¹Ð¤()5Í ½à À°ÅÕ½Ðí¥±1¥¹
½Õ¹ÐÅÕ½Ðì°ÅÕ½ÐíQ¡½Õ¹ÐèÅÕ½ÐìµÀìÀÌØí±¥¹
½Õ¹ÑlÁt¤()½ÈÀÌØí¤ôÄѼÀÌØí±¥¹
½Õ¹ÑlÁt(%MÑÉ¥¹MÑÉ¥Á]L ÀÌØí±¥¹
½Õ¹ÑlÀÌØí¥t°à¤±ÐìÐìÌäìÌäìQ¡¸5Í    ½à À°ÅÕ½ÐíQ¡Ù±Õ½ÀÌØí¤¥ÌèÅÕ½Ðì°ÀÌØí¤¤)9áÐ(

if your going to re-write the script

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

why not

#include <File.au3>
Dim $lineCount

$toolBarAddress = @DesktopDir & "\a.txt"
_FileReadToArray($toolBarAddress, $lineCount)

MsgBox(0,"File Line Count", "The count: " & $lineCount[0])

For $i = 1 to $lineCount[0]
    If StringStripWS($lineCount[$i], 8) <> '' Then MsgBox(0, "The value of $i is:", $i)
Next

if your going to re-write the script

8)

lol@both of you... I was wondering why he wasn't using _FileReadToArray() but whenever I suggest an easier way, people always take the long way anyway.

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.

Link to comment
Share on other sites

Does anyone know why my code doesn't work? I'd really like to ignore a line with that contains only whitespace (meaning all kinds of whitespace.)

wrong logic. $i will allways be $lineCount, so you need another variable to store the empty lines. Next Problem StringIsSpace does not detect plain empty lines, only @CRLF (don't know why!).

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

FileReadLine() strips carriage return characters from end of lines.

Therefore, any lines that were empty lines, contain no end of line whitespace types after using FileReadLine(). This means that StringIsSpace will return 0 in checking a value of nothing.

:whistle:

Edited by MHz
Link to comment
Share on other sites

FileReadLine() strips carriage return characters from end of lines.

Therefore, any lines that were empty lines, contain no end of line whitespace types after using FileReadLine(). This means that StringIsSpace will return 0 in checking a value of nothing.

:)

yep, you're absolutely right! I did not remember that "stripping thing" :whistle:

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...