Sign in to follow this  
Followers 0
05YamahaR1

Delete lines in text file that dont end in .pdf

5 posts in this topic

I would like to delete all lines in a .txt file that dont end with .pdf. I found but it's doing the opposite of what I'm trying to accomplish. It is deleting every line that ends with .pdf. What do I need to do to be able to delete every line that dont end with .pdf?

Share this post


Link to post
Share on other sites



Read the whole file to an array with _FileReadToArray(). Then loop through the array using StringRight() to check the end of each line for ".pdf", and delete that element from the array if it's not there. Loop through the array in reverse to make it easier.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I would like to delete all lines in a .txt file that dont end with .pdf. I found but it's doing the opposite of what I'm trying to accomplish. It is deleting every line that ends with .pdf. What do I need to do to be able to delete every line that dont end with .pdf?

Are you familiar with Operators?

If True Then ...

If Not True Then ...

If A Then ...

If A Or B Then ...

If A And B Then ...

If A And Not B Then ...

The simplest way is to get a result, however you get that result, then examine it and see 1. is it the result you want and 2. how can you get the result better.

If you are seeing

If StringRight($InputLine, 4) = ".pdf" Then ...

Then the reverse will be

If Not StringRight($InputLine, 4) = ".pdf" Then ...

Or

If StringRight($InputLine, 4) <> ".pdf" Then ...

Mal


Are you telling me something I need to know or something I want to know?

Share this post


Link to post
Share on other sites

Depending on the size of the file you are looking at this should do it.

$sFile = "SomeFile.txt"
$sText = FileRead($sFile)
$aHold = StringRegExp($sText, "(?m:^)(.+\.pdf)(?:\v|$)+", 3);; Get all the lines that end with .pdf
If UBound($aHold) > 0 Then
    $sHold = ""
    For $i = 0 To Ubound($aHold) -1
        $sHold &= $aHold[$i] & @CRLF
    Next
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, StringStripWS($sHold, 2))
    FileClose($hFile)
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Depending on the size of the file you are looking at this should do it.

$sFile = "SomeFile.txt"
$sText = FileRead($sFile)
$aHold = StringRegExp($sText, "(?m:^)(.+\.pdf)(?:\v|$)+", 3);; Get all the lines that end with .pdf
If UBound($aHold) > 0 Then
    $sHold = ""
    For $i = 0 To Ubound($aHold) -1
        $sHold &= $aHold[$i] & @CRLF
    Next
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, StringStripWS($sHold, 2))
    FileClose($hFile)
EndIf

Thank you George, it works perfect!

Also, thank you to everyone else that replied! I tried looking at the help file of what you suggested, but since I'm new to this it was just too foreign to me. However, I am looking at the wiki FAQs and tutorials so I can grasp this because this is way too cool!

Again, thank you guys!

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  
Followers 0