Jump to content

Trouble with detect "//" symbol


Nubie
 Share

Recommended Posts

Here's my test

123
// ---
// abc
// +++
456

I want delete all lines have // symbol

#include <file.au3>
$file = @ScriptDir & "\test.txt"
$hFileOpen = FileOpen($file, 0)
For $i = 1 To _FileCountLines($file)
    $line = FileReadLine($hFileOpen, $i)
    If StringInStr($line, "//") Then
        _FileWriteToLine($file, $i, "", 1)
    EndIf
Next
FileClose($hFileOpen)

But its result isn't what I want

123
// abc

What's wrong with me?

Thanks :)

Edited by Nubie
Link to comment
Share on other sites

There are a million ways to do it, this is just the one I came up with first.

#Include <File.au3>
#Include <Array.au3>
Local $aFile
_FileReadToArray(@ScriptDir & "\test.txt", $aFile)

For $i = 1 to $aFile[0]
    If Not StringInStr($aFile[$i], "//") Then
        FileWrite(@ScriptDir & "\test2.txt", $aFile[$i] & @CRLF)
    EndIf
Next

Why yours is failing?  I am not sure maybe because you're trying to write to the same file that you're reading from.

I try to stay away from the "line" functions most of the time unless I have a specific reason I need them.

The other issue I think I see is your saying if the line has "//" save it by writing it to the file, when it should be NOT has "//" to get rid of those lines.  Finally not sure the effect but your writing to file by line number, your final file should have line 1, 2, 3, ... so you need to write then in that order, but since your line numbers are based on the value of $i and you will be skipping some lines because you want to delete them, the result is telling the function to write to a line number that does not exist and I am not sure how that behaves. 

Edit: Also looks like FileOpen() is used incorrectly.

You get a handle to use but you did not even associate a variable for the handle, instead your just referencing $file as the file path so your FileOpen() is not doing anything for you nor is FileClose()

Edited by ViciousXUSMC
Link to comment
Share on other sites

Oh yes,  FileOpen() FileClose() my fault when make quick write a simple test, I'll edit it. But it's not main my problem

Yes your way have work fine with me, but what's wrong in my logic do? It's work fine in MsgBox. I think problem at _FileWriteToLine

Edited by Nubie
Link to comment
Share on other sites

Oh, you're right I see what you did when I looked closer.  You are trying to just write a blank line over the top of the lines that contain "//" not remove them.  My logic was to only write the lines that did not contain "//" to the file.  Thus why I have NOT in my StringinStr()

Go ahead and put something in your _FileWriteToLine() such as

  _FileWriteToLine($file, $i, "test", 1)

 and you will see that its working, it leaves only the lines with "123" and "456" untouched, and it will replace the lines between.  The issue is most likely what I hinted at before, your writing to a specific line number and if you replace a line with "" it does not actually consume a line in the file causing your lines to get all messed up, and your reading/writing to that file at the same time.

Edited by ViciousXUSMC
Link to comment
Share on other sites

The only issue with the OP is that you are working forwards through a loop deleting things.   go backwards.

#include <file.au3>


$file = @ScriptDir & "\test.txt"

For $i = _FileCountLines($file) to 1 step - 1
    $line = FileReadLine($file, $i)
    If StringInStr($line, "//") Then
        _FileWriteToLine($file, $i, "", 1)
    EndIf
 Next

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

If you are not frightened by regular expressions this could be a nice way

$str = "123" & @crlf & "// ---" & @crlf & "// abc" & @crlf & "// +++" & @crlf & "456"
; $str = FileRead(@ScriptDir & "\test.txt")
; Msgbox(0,"", $str)
$new = StringRegExpReplace($str, '(?m)^\s*//.*\R?', "")
; Msgbox(0,"", $new)
FileWrite(@ScriptDir & "\test2.txt", $new)

 

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...