Jump to content

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

[Script] AHK No BlanK Lines


  • Please log in to reply
13 replies to this topic
awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009

Just drop file/folders and all blank lines in .ahk files will be removed. Close with Escape Keyboard Key. Destination files in the same folder as source files. Source Files unchanged. New files named : SourceFileName(No Blank Lines).ahk
ahknoblanklines.png

Back up your files just in case.

/*
------------- AHK No BlanK Lines ---------------
By awannaknow
http://www.autohotkey.net/~awannaknow/
AHK_L 32 UTF8
-is surely AHK Classic Compatible. Has to be tested Yet !
-----------------------------------------------------
*/
/*
------------- Friday 23rd of September 2011 ----------
I Passed by and liked what Nazzal had done : AHKCaser
http://www.autohotkey.com/forum/topic72146.html&highlight=ahkcaser
I used his idea to make this one.
That's my first Software !
-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed. Close with Escape Keyboard Key.
Destination files in the same folder as source files.
Source Files unchanged.
New files named :
SourceFileName(No Blank Lines).ahk

Back up your files just in case.
------------------------------------------------------
*/
/*
------------------- Examples of use ------------------
It's quite useful for removing comments by decompilation since that leaves a lot of blank lines.
From jpjazzy : http://www.autohotkey.com/forum/viewtopic.php?p=482661#482661
*/
/*
----------------------------- Credits -----------------------------
Special thanks to all knowledgeable Members which helped me, to say the least, or participated in any way or from which I borrowed code from their previous works, to get it done.
Truly, I only bring the idea and all the real work was done by :
Nazzal for his AHKCaser which gave me this idea, Tuncay, Morpheus, closed, emmanuel d
----- Feel free to contact me if I forgot to mention your name -----
/*
----------------------------- AutoHotKey -----------------------------
Of course nothing of this would have been possible without AutoHotKey aka AHK :
http://www.autohotkey.com
Created by : Chris Mallet
And all new Versions/Branch
created by Lexicos
*/
#NoEnv
#Persistent
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
Gui, +AlwaysOnTop -Border
Gui, Font, s13
Gui, Color, Purple
Gui, Add, Pic, x10 y15 icon2, %A_AhkPath%
Gui, Add, Text, x+0 y-10 cwhite w295 h45 gGuiMove +Center, `nAHK No Blank Lines
Gui, Font, s10
Gui, Add, Text, xp y+0 h30 Wp cWhite gGuiMove vDrop +Center, Drop files and folders
Gui, Show, y0 w355 h65 NoActivate, AHK No Blank Lines
Return
GuiMove:
PostMessage, 0xA1, 2,,, A
Return
GuiDropfiles:
Gui, Color, EE82EE
Gui, -E0x10
GuiControl, ,Dropped, Please wait . . .
Loop, Parse, A_GuiEvent, `n, `r
Loop, % (D:=InStr(FileExist(A_Loopfield), "D")) ? A_LoopField "\*.ahk" : A_LoopField ,% D ? 1 : 0, % D ? 1 : 0
{
IfNotEqual, A_LoopFileExt,ahk, Continue
FileRead, Contents, %A_LoopFileFullPath%
if not ErrorLevel
Filepathname=%A_LoopFileFullPath%
Loop,parse,Contents, `r, %A_Tab%
{
StringLen, linenumber, A_LoopField
If linenumber=0
Continue
If linenumber=1
Continue
stringreplace, Contents, A_LoopField, `r`n`r`n, `r`n, A
StringReplace, BlFilepathname, Filepathname, .ahk, (No Blank Lines).ahk
FileAppend, %Contents%, %BlFilepathname%
}
}
Gui, Color, Purple
GuiControl, ,Drop, Drop files and folders
Gui, +E0x10
Return
Gui, Color, Purple
GuiControl, ,Drop, Drop files and folders
Gui, +E0x10
Return
GuiEscape:
ExitApp 

------------------------------------------------------
The uptodate AHK and EXE files are maintained here only :
AHK BlanK Lines Remover.ahk on AutoHotkey.net
Right click and save as
My first baby :shock:
It deserve a gooooddd picture.
Renamed from AHK BlanK Lines Remover to :
------------- AHK No BlanK Lines ---------------
AHK_L 32 UTF8
-is surely AHK Classic Compatible. Has to be tested Yet !
-----------------------------------------------------

------------- Friday 23rd of September 2011 ----------

I Passed by and liked what Nazzal had done : AHKCaser
<!-- m -->http://www.autohotke... ... t=ahkcaser<!-- m -->
I used his idea to make this one.
That's my first Software !
-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed.
Destination files in the same folder as source files.
Source Files unchanged.
New files named :
SourceFileName(No Blank Lines).ahk

Back up your files just in case.
------------------------------------------------------
----------------------------- Credits -----------------------------
Special thanks to all knowledgeable Members which helped me, to say the least, or participated in any way or from which I borrowed code from their previous works, to get it done.
Truly, I only bring the idea and all the real work was done by :
Nazzal for his AHKCaser which gave me this idea, Tuncay, Morpheus, closed, emmanuel d
----- Feel free to contact me if I forgot to mention your name -----
----------------------------- AutoHotKey -----------------------------
Of course nothing of this would have been possible without AutoHotKey aka AHK :
<!-- m -->http://www.autohotkey.com<!-- m -->
Created by : Chris Mallet
And all new Versions/Branch
created by Lexicos



Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
To delete blank lines, a regex is enough:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

text =
(


Just drop file/folders and all blank lines in .ahk files will be removed.


Destination files in the same folder as source files.

Source Files unchanged.

New files named :



SourceFileName_UnBlanked.ahk.ahk 


)

MsgBox % "'" . remove_BlankLines(text) . "'"

remove_BlankLines(ByRef _source) {
    return RegExReplace(_source, "m)(?:(\r?\n|$))+", "$1")
}
Only the first blank line is not removed, if the source text begins with a blank line. This can be catched in the function and removed "by hand".

No signature.


Morpheus
  • Members
  • 475 posts
  • Last active: Oct 21 2014 11:08 AM
  • Joined: 31 Jul 2008
Out of curiosity, are there cases where this snippet from the help file won't work?
Loop
{
    StringReplace, MyString, MyString, `r`n`r`n, `r`n, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
}


awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009

Only the first blank line is not removed, if the source text begins with a blank line. This can be catched in the function and removed "by hand".


Thank you Tuncay

(it tooks me quite a while ( few days) to get a result for this . . . :oops: )

I searched but . . . :oops: :
A solution for the first blank line that is not removed ?

I don't know regex, just a couple of rules, 2 or 3 characters and what can be done with it, that's all. I know that it is something to know about.

A regex to remove comments ?
like :
ifwinexist, windows, my window ; blah, blahblahblahblah 
; ;;;;;;;;;;
; blah
;blah blah, blah
/*
--------- blah
blah blah
*/
You know, . . .usual stuff . . .

And if some like to throw a few lines to explain this code, from Nazzal, they're more than welcome :
Loop, % (D:=InStr(FileExist(A_Loopfield), "D")) ? A_LoopField "\*.ahk" : A_LoopField ,% D ? 1 : 0, % D ? 1 : 0

And I'ld eventually love to hear your regex explanation, too Tuncay ! :lol:
RegExReplace(_source, "m)(?:(\r?\n|$))+", "$1")

I definitely need to deepyfy my regex thingy !

Out of curiosity, are there cases where this snippet from the help file won't work?

Loop
{
    StringReplace, MyString, MyString, `r`n`r`n, `r`n, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
}

It didn't worked in my case, I don't know why.
I had to remove tabs first, then, do not copy lines with 0 or 1 character in it (which were remaining Tabs and Cr Lf)
If linenumber=0
Continue
If linenumber=1
Continue
Before that, only some blank lines were removed.

closed
  • Members
  • 509 posts
  • Last active: Jan 14 2012 06:14 PM
  • Joined: 07 Feb 2008
Nice idea

I would use it if left an empty line before a label and after a return.Just to maintain readability.

One minor thing is the code for the new filename ,it is within the loop so it is done over and over again :) (if i got it right...)

emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
actualy i wrote that line for him :D

And if some like to throw a few lines to explain this code, from Nazzal, they're more than welcome :

Loop, % (D:=InStr(FileExist(A_Loopfield), "D")) ? A_LoopField "\*.ahk" : A_LoopField ,% D ? 1 : 0, % D ? 1 : 0

this returns the fileattributes: FileExist(A_Loopfield)
this will check if there is a D in the fileattributes: InStr(FileExist(A_Loopfield), "D"))
if it is than its a directory and the result is true.
this wil store the result true ore false in the var d: (D:=InStr(FileExist(A_Loopfield), "D"))
i am checking if the fileattributes contain D than i loop folders and look for files(A_LoopField "\*.ahk") else i loop files A_LoopField
then if its a folder i do recursive and include subfolders

i am using ternary operators there
IfSomethingIsTrue ? Dothis : ElseDothis
to use this in a command you put % and a space to tell the script you are doing a expression.

if i'm not clear then say it.
good luck

ps i did some modifications:
/*

			
------------- AHK BlanK Lines Remover ---------------
AHK_L 32 UTF8
-is surely AHK Classic Compatible. Has to be tested Yet !
-----------------------------------------------------

------------- Friday 23rd of September 2011 ----------

I Passed by and liked what Nazzal had done : AHKCaser
http://www.autohotkey.com/forum/topic72146.html&highlight=ahkcaser
I used his idea to make this one.
         That's my first Software !
-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed.
Destination files in the same folder as source files.
Source Files unchanged.
New files named :
SourceFileName_UnBlanked.ahk.ahk

Back up your files just in case.
------------------------------------------------------
*/

#NoEnv
#Persistent
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
Gui, +AlwaysOnTop -Border
Gui, Font, s13
Gui, Color, Purple
Gui, Add, Pic, x10 y15 icon2, %A_AhkPath%
Gui, Add, Text, x+0 y-10 cwhite w295 h45 gGuiMove +Center, `nAHK BlanK Lines Remover
Gui, Font, s10
Gui, Add, Text, xp y+0 h30 Wp cWhite gGuiMove  vDrop +Center, Drop files or folders
Gui, Show, y0 w355 h65 NoActivate, AHK Blan KinG
Return
GuiMove:
   PostMessage, 0xA1, 2,,, A
   Return
GuiDropfiles:
	Gui, Color, EE82EE
	Gui, -E0x10
	GuiControl, ,Dropped, Please wait . . .
	Loop, Parse, A_GuiEvent, `n, `r  ; Drop files
	Loop, % (D:=InStr(FileExist(A_Loopfield), "D")) ? A_LoopField "\*.ahk" : A_LoopField ,% D ? 1 : 0, % D ? 1 : 0
		{
		IfNotEqual, A_LoopFileExt,ahk, Continue
		FileRead, Contents, %A_LoopFileFullPath% ;load file in var Contents
		if not ErrorLevel  ; Successfully loaded.
			Loop,parse,Contents, `r,`n ;%A_Tab% ; Delete Blank Lines do not delete tabs, you are messing wit indentation
				{
				IfEqual,A_LoopField,,continue
				if IsTabsOnly(A_LoopField)
					continue
				else FileAppend,%A_LoopField%`n,%A_LoopFileDir%\_%A_LoopFileName% ;
				
				}
		} 
		
   Gui, Color, Purple
   GuiControl, ,Drop, Drop files or folders
   Gui, +E0x10
   Return
GuiEscape:
   ExitApp
IsTabsOnly(text) {
	loop,parse,text ; parse all charracters
		If A_LoopField not in %A_Tab%,%A_Space%
			Return,0 ; if it is not a tab, return false
	return,1 ; if we are here it where all tabs, so return, true
		  
      
}

you don't need to replace new line, you just skip them

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
forgot to mention that variables from a file loop are stil available in a parse loop, so FileAppend,%A_LoopField%`n,%A_LoopFileDir%\_%A_LoopFileName% works fine

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
I found a better way to do it. The regex is modified version from my previous and now it leaves last newline (if present) intact. Last newline is not that important. Some programs treat last newline as an empty line and some others do not. I think this can be neglected.

remove_BlankLines(ByRef _source) {
    return RegExReplace(_source, "m`a)(*BSR_ANYCRLF)^\R+")
}
And here the details:

[color=orange]m`a)(*BSR_ANYCRLF)[/color][color=green]^\R+[/color]

The first orange part are the options to the regex parser. The marked green part is the actual regular expression.

m
This option turns on multiline mode. The regex is tested against each lines individually and operates on them.

`a and (*BSR_ANYCRLF)
`a changes the default newline character to recognize by the RegEx parser. Normally, this is `r`n only. But the `a option changes it to recognize any type of newline (see documentation). But this includes too many types. This can be restricted to `r`n, `r and `n only. that is done with an extension option (*BSR_ANYCRLF).

^
This matches the beginning of the line. Without it, all newlines from source would match. The regex is tested from begin of line, not just anywhere from it.

\R+
\R stands for any newline type, which is restricted with the special option (*BSR_ANYCRLF) (see above). The plus sign repeats the previous character (or character class) 1 or more times. So only newlines are matching, which is followed by another newline until something different is found. All these newlines are compacted to one that way.

No signature.


Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006

Out of curiosity, are there cases where this snippet from the help file won't work?

Loop
{
    StringReplace, MyString, MyString, `r`n`r`n, `r`n, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
}

To answer your question: A newline can have the form of `r`n or `n or `r only. Now lets change the StringReplace part to
StringReplace, text, text, `n`n, `n, UseErrorLevel
Now it works and nearly all empty lines are removed. Only the first and last newline are intact. If you care about them, you have to remove them manually too. Also this code needs a Loop, with more than one call of StringReplace. The problem here is, it does recognize one newline type only.

No signature.


emmanuel d
  • Members
  • 519 posts
  • Last active: Jul 15 2017 12:04 PM
  • Joined: 29 Jan 2009
thank you Tuncay
but at first glimps it doesn't removes lines with spaces only or tabs only
or tabs and spaces mixed.
like indented text but nothing added behind it.
could you add that in there too?

and many thanx for the support sir.

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum


Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
I am not very sure, but try this:
"m`a)(*BSR_ANYCRLF)^\R+|^\s+(?:\R+|$)"
Still does leave in my experimentation an empty line, where multiple tabs was before. But calling the function a second time eliminated that too. So using this regex two times works (I think..).

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

text =
(


Just drop file/folders and all blank lines in .ahk files will be removed.
`t
`t`t`t
Destination files in the same folder as source files.

   Source Files unchanged.
                
New files named :



`tSourceFileName_UnBlanked.ahk.ahk 


)

;~ text := "`n`n   hallo`n`t`t`n        "
MsgBox % clipboard  := "'" . remove_BlankLines(remove_BlankLines(text)) . "'"

remove_BlankLines(ByRef _source) {
    return RegExReplace(_source, "m`a)(*BSR_ANYCRLF)^\R+|^\s+(?:\R+|$)")
}

Edit, added this: In example like that
remove_BlankLines(ByRef _source) {
    return RegExReplace(RegExReplace(_source, regex := "Sm`a)(*BSR_ANYCRLF)^\R+|^\s+(?:\R+|$)"), regex)
}

No signature.


awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009
You're awsome !

I left, leaving a relatively pretty long post, minding if maybe I was asking too many questions . . .

Then, I came back a few hours later, and . . . I had this very good surprise to have a lot of detailled and up to the point informations to read.

So, it'll take me some time to read and understand them all, I will start that tomorrow.

But I wanted to post to say thank you all ! ! !

awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009
You are, indeed, very clear, it's more on my side : for now, all this fly way over my head, unfortunatly. From the help file I would never extrapolate such a line of code to proceed files and folders . . .
But the good news, for me anyway, is that your explanation help me (and surely others which will make searches) to progress.

actualy i wrote that line for him :D

And if some like to throw a few lines to explain this code, from Nazzal, they're more than welcome :

Loop, % (D:=InStr(FileExist(A_Loopfield), "D")) ? A_LoopField "\*.ahk" : A_LoopField ,% D ? 1 : 0, % D ? 1 : 0

if i'm not clear then say it.
good luck


ps i did some modifications:

Yes, and you did so well (even commented the changes, so that I could understand) that entire files I tested are unblanked, even the first line (or the last one).
I'll will modify the code, infact I will copy paste yours, as mine don't work in all scenarios.
Thanks to Yoda Master emmanuel d

Hi Tuncay,
Thanks for all detailled explanations, it'll help me to understand and progress (Well, I hope, anyway).
I think, I tested on some files, that emmanuel d code handle first and/or last line, so I'll make changes for his code.
I spent the week end on all the post trying to understand and make my own, but . . . I couldn't get something operational . . . :roll:

I found a better way to do it. The regex is modified version from my previous and now it leaves last newline (if present) intact.

What if one plan to remove comments in ahk files :
This week end I also read a bit on regex, but as usual, it needs practice to improve, and wanted to answer this:

A regex to remove comments ?
like :

ifwinexist, windows, my window ; blah, blahblahblahblah 
; ;;;;;;;;;;
; blah
;blah blah, blah
/*
--------- blah
blah blah
*/
You know, . . .usual stuff . . .

I made a far from perfect regex, but cannot get it better, for now anyway.
I had to write 2 regex, which are more less efficients, to erase comments, this first one work on this comments types :
/*
Blah
*/
#NoEnv 
SendMode Input 
SetWorkingDir %A_ScriptDir% 

text = 
( 

AutoHotkey is a free, open-source utility for Windows. With it, you can:
Automate almost anything by sending keystrokes and mouse clicks. You can write a mouse or keyboard macro by hand or use the macro recorder.

-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed. 
Destination files in tmy first Software
/*
Just drop file/folders and all blank lines in .ahk files will be removed. 
Destination files in t
 his idea to make this one.
-----------------------;-) ----------------------------
es.
http://www.autohotkey.com/forum/topic72146.html&highlight=ahkcaser

------------------------------------------------------
*/ 

I Passed by and liked what Nazzal had done : AHKCaser
http://www.autohotkey.com/forum/topic72146.html&highlight=ahkcaser
I used his idea to make this one.
			That's my first Software !
;ersgeheeeetrhtrhrthrth
-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed. 
Destination files in the same folder as source files. ; kjhoihttft
Source
/*
***************************************
*		hey, 			 	  *
*		don't assume !		  *
***************************************
*/
			gibberishy !
-----------------------;-) ---------------------------- ; ghgjhkjv
AutoHotkey unleashes the full potential of your keyboard, joystick, and mouse. For example, in addition to the typical Control, Alt, and Shift modifiers, you can use the Windows key and the Capslock key as modifiers. 
ifwinexist, windows, my window ; blah, blahblahblahblah 
; ;;;;;;;;;; 
; blah 
;blah blah, blah 
/* 
--------- blah 
blah blah 
*/
) 

MsgBox % "'" . remove_BlankLines(text) . "'" 

remove_BlankLines(ByRef _source) { 
	return RegExReplace(_source, "m)(\/\*)(.*?)(\*\/\r?\n?)", "") 
}

This one on this type of comments :
ifwinexist, windows, my window ; blah, blahblahblahblah 
; ;;;;;;;;;; 
; blah 
;blah blah, blah
#NoEnv 
SendMode Input 
SetWorkingDir %A_ScriptDir% 

text = 
( 
can write a mouse or keyboard macro by hand or use the macro recorder.

-----------------------;-) ----------------------------
Just drop file/folders and all blank lines in .ahk files will be removed. 
/*
Just  his this one.
-----------------------;-) ----------------------------
c72146.html&highlight=ahkcaser

------------------------------------------------------
*/ 

I Passed by

;ersgeheeeetrhtrhrthrth
-----------------------;-) ----------------------------
Just foldeurce files. ; kjhoihttft
Source
/*
***************************************
*		hey, 			 	  *
*		don't assume !		  *
***************************************
*/
			gibberishy !
-----------------------;-) ---------------------------- ; ghgjhkjv
AutoHotkey unleashes the full potential of your keyboard, joystick, and mouse the Windows key and the Capslock key as modifiers. 
ifwinews, my window ; blah, blahblahblahblah 
; ;;;;;;;;;; 
; blah 
;blah blah, blah 
/* 
--------- blah 
blah blah 
*/
) 

MsgBox % "'" . remove_BlankLines(text) . "'" 

remove_BlankLines(ByRef _source) { 
	return RegExReplace(_source, ")(;\s*.*?)(\r?\n)", "$2")  
}

It needs to be corrected.
Could they be "fusionned" in one regex to correct both comments type?

Nice idea

I would use it if left an empty line before a label and after a return.Just to maintain readability.

One minor thing is the code for the new filename ,it is within the loop so it is done over and over again :) (if i got it right...)

Hi Thanh00, when I got everything in order there I try to make a custom one.
But you noticed who the Jedis are here, right ?

awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009
Done ! :
:wink:
Here :
[Script] AHK Almost No BlanK Lines

Nice idea

I would use it if left an empty line before a label and after a return.Just to maintain readability.

One minor thing is the code for the new filename ,it is within the loop so it is done over and over again :) (if i got it right...)


Your scripts online or anything you'll need online for free with 0hna.tk  like: Zero $ Hosting No Ad Free Online Hosting
Remove comments and blank lines in AHK code