Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Detect and delete blank lines in notepad


  • Please log in to reply
6 replies to this topic
Redeemed07
  • Members
  • 15 posts
  • Last active: Jan 06 2010 10:07 PM
  • Joined: 17 Jun 2009
I have large lists of information I'm manipulating. I'm using notepad because the formatting makes lists very easy to work with. I've searched the forums and Google™ and have not found a solution. I'm sorry if this is a duplicate.

I'd like to work into my script the ability to remove blank lines in the text. It now looks like:

Blah, blah 43636753 05/01/1964

Blah, blah 45636443 05/01/1964

And I want that empty line removed:

Blah, blah 43636753 05/01/1964
Blah, blah 45636443 05/01/1964

This is my third day using AHK and I absolutely love it. My boss loves it because it's saved many hours of repetative work. I'm trying to figure out the scripting but it's been a looong time since I've dabbled in coding of any kind.

Thank you so much for your help here and in the rest of the forum.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Try this:
ControlGetText,text,Edit1,ahk_class Notepad

Loop,parse,text,`n, `r

If (A_LoopField!="")

	newText.=A_LoopField "`r`n"

ControlSetText,Edit1,%newText%,ahk_class Notepad


Redeemed07
  • Members
  • 15 posts
  • Last active: Jan 06 2010 10:07 PM
  • Joined: 17 Jun 2009
Perfect!

Thank you so much!

sagar
  • Members
  • 1 posts
  • Last active: Feb 20 2012 04:55 AM
  • Joined: 20 Feb 2012
SCRIPT: Dim lngValue1

Dim lntValue1
Dim lntValue2
Dim lntValue3
Dim strGeneratedFilePath
Dim strDirPath
Dim strDistrictCode
Dim strIndustryCode
Dim strCurrentDate
Dim strpcbdate
Dim strpcbtime

Dim fileSys
Dim fileText
Dim strPath
Dim intAppend
Dim getName
Dim strKILNString
Dim time1


lngValue1=HmiRuntime.SmartTags("AI_2").Value


If lngValue1 ="null" Or lngValue1="" Then lntValue1 =0 Else lntValue1=lngValue1 End If

lntValue2=FormatNumber(lntValue1,2)
lntValue3= Right("00" & lntValue2, 5)

strDirPath = "D:\stack_pollution\"
strDistrictCode ="gntr"
strIndustryCode="bcltgdp"

strCurrentDate = DatePart("yyyy",Date) & Right("0" & DatePart("m",Date),2) & Right("0" & DatePart("d",Date),2)

strpcbdate = Right("0" & DatePart("m",Date),2)& "/" & Right("0" & DatePart("d",Date),2)& "/" & DatePart("yyyy",Date)



time1 = Right("0" & Time, 11)
strpcbtime =Left(time1,5)& ":00" & Right(time1,3)

'* Generate file path
strGeneratedFilePath = strDirPath & "stack" & strCurrentDate & ".txt"

'* Constant for appending the file
intAppend = 8

'* Create the string to be printed to the file
strKILNString = strDistrictCode& "," & strIndustryCode & ",2," & strpcbdate & " " & strpcbtime & "," & lntValue3 & ",NA,NA,NA"


'* Create the file and print the text string
Set fileSys = CreateObject("Scripting.FileSystemObject")

If fileSys.FileExists(strGeneratedFilePath) Then
Set fileText = fileSys.OpenTextFile(strGeneratedFilePath,intAppend,True)
fileText.WriteLine(strKILNString)
fileText.Close()
Else
Set fileText = fileSys.CreateTextFile(strGeneratedFilePath,True)
strPath= fileSys.GetAbsolutePathName(strGeneratedFilePath)
getName = fileSys.GetFileName(strPath)
fileText.WriteLine(strKILNString)
fileText.Close()
End If

Alangar Manickam
  • Members
  • 40 posts
  • Last active: Apr 17 2015 10:28 PM
  • Joined: 03 Oct 2007

Awesome code :)

 

 

F11::

ControlGetText,text,Edit1,ahk_class Notepad

Loop,parse,text,`n, `r

If (A_LoopField!="")

newText.=A_LoopField "`r`n" C

ControlSetText,Edit1,%newText%,ahk_class Notepad

return

 

But how do I scan a notepad file & remove the blank lines ?

Eg: if my notepad file is in C:\test.txt

 

Pls help

 

Alan


Alangar Manickam, Bangalore, India

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
F11::
  FileSelectFile,file,,,,*.txt
  If !file
    return
  Loop,Read,%file%
    If (A_LoopReadLine!="")
      newText.=A_LoopReadLine "`r`n" C
  MsgBox,4,Replace File?,Are you sure you want to remove empty lines in %file%?
  IfMsgBox No
    return
  FileDelete,%file%
  FileAppend,%newText%,%file%
return


Alangar Manickam
  • Members
  • 40 posts
  • Last active: Apr 17 2015 10:28 PM
  • Joined: 03 Oct 2007
F11::
  FileSelectFile,file,,,,*.txt
  If !file
    return
  Loop,Read,%file%
    If (A_LoopReadLine!="")
      newText.=A_LoopReadLine "`r`n" C
  MsgBox,4,Replace File?,Are you sure you want to remove empty lines in %file%?
  IfMsgBox No
    return
  FileDelete,%file%
  FileAppend,%newText%,%file%
return

Thank you so much HotKeyit.


Alangar Manickam, Bangalore, India