find consecutives empty lines in all files

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
ccmancio
Posts: 11
Joined: Tue Jun 05, 2007 6:54 pm

find consecutives empty lines in all files

Post by ccmancio »

I need to know that files have more then one consecutives empty lines in all files of one folder and subfolders.

I use \n\n and appears : Cannot search files for a regular expression including '\n'

Best regards
BrianB
Posts: 8
Joined: Thu Oct 15, 2009 3:47 pm
Location: United Kingdom

Post by BrianB »

Here is a VBscript to do that.It looks for 3 consecutive Carriage Return/LineFeed characters.

Code: Select all

'=======================================================================
'- WINDOWS VBSCRIPT
'-  FIND TEXT FILE WITH 2 CONSECUTIVE EMPTY LINES - option to open it
'- Copy/past this text to a text file. Save the file with extension .vbs. Double click it to run.
'- Brian Baulsom April 2010
'=======================================================================
Dim FSO				' FileSystemObject
Dim WSH			' Shell
Dim RootFolder
Dim UserCancelled
Dim rsp
'----------------------------------------------------------------------------------------------------------------------------------------------------------------
main
'=======================================================================
'- 	MAIN ROUTINE
'=======================================================================
Sub Main()
	'------------------------------------------------------------------------------------------------
	'- ROOT FOLDER TO START SEARCHING FROM ********************************
	Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
	Set RootFolder = FSO.GetFolder("F:\Test\")          		 ' ** CHANGE AS NECESSARY
	'------------------------------------------------------------------------------------------------
	Set WSH =WScript.CreateObject("WScript.Shell")
	UserCancelled=False
	'------------------------------------------------------------------------------------------------
	GetFiles RootFolder				' check files in the root Folder first
	If UserCancelled=true then exit Sub
	GetFolders RootFolder
End Sub
'=======================================================================
'- 	SUBROUTINE : GET FOLDERS
'=======================================================================
Sub GetFolders(basefolder)
	Dim MainFolder
	Dim SubFolders
	Dim Folder
	Dim FolderName
	'------------------------------------------------------------------------------------------------
	set MainFolder=basefolder
	Set SubFolders = MainFolder.SubFolders
	'------------------------------------------------------------------------------------------------
	'- LOOP FOLDERS
	For Each Folder In SubFolders
		FolderName = Folder.Name
		'----------------------------------------------------------------------------------------
		GetFiles Folder		' get files in the folder
		GetFolders Folder		' get folders in the folder
		'---------------------------------------------------------------------------------------
		'- check If folder message cancelled (=abort)
		If UserCancelled=true then exit Sub
		'---------------------------------------------------------------------------------------
	Next
 End Sub 
 '=========== end of Sub ==================================================
    
    
'====================================================================
'- 	SUBROUTINE : GET FILES IN Folder
'====================================================================
Sub GetFiles(myfolder)
	Dim MyFolderName
	Dim MyFiles
	Dim MyFile
	Dim MyFileName
	Dim MyFileContents
	Dim MyFileText
	Dim CheckString
	Const ForReading=1, TristateDefault=-2
	Dim fn					' full path & file name
	'-------------------------------------------------------------------------------------------
	MyFolderName=myfolder.path
	Set MyFiles = MyFolder.Files
	CheckString=vbCrLf &vbCrLf  &vbCrLf	' test for 3 consecutive end of lines
	'------------------------------------------------------------------------------------------
	'- LOOP THROUGH THE FILES IN THE Folder
	For Each MyFile In MyFiles
		MyFileName=MyFile.name
		'----------------------------------------------------------------------------------
		If right(MyFileName,3)="txt" then
			'--------------------------------------------------------------------------
			'- Read the text file
			Set MyFileContents = MyFile.OpenAsTextStream(ForReading,TrisateDefault)
			MyFileText=MyFileContents.ReadAll
			'-------------------------------------------------------------------------
			'- FILE FOUND :  Message Box
			If Instr(1,MyFileText,checkstring,1)<>0 then
				rsp= msgbox("This file contains 2 consecutive empty lines." &vbcr _
							&"Do you wish to open it ?" &vbCr  _
							&"Folder : "  &MyFolderName &vbcr _
							&"File  : " &MyFileName,vbYesNoCancel," FOUND FILE")
				'---------------------------------------------------------------
				If rsp=vbCancel then 
					UserCancelled=true
					Exit Sub
				'----------------------------------------------------------------
				ElseIf rsp=vbYes then
					fn= MyFolderName &"\" &MyFileName
					WSH.Run "%windir%\notepad " & fn
				End If
				'----------------------------------------------------------------
			End If
			'---------------------------------------------------------------------------
			MyFileContents.Close		' close the text file
		End If
		'-----------------------------------------------------------------------------------
	next
	'-------------------------------------------------------------------------------------------
End Sub
 '=========== end of Sub ==================================================
ccmancio
Posts: 11
Joined: Tue Jun 05, 2007 6:54 pm

Post by ccmancio »

Its Ok too, but I think to make this in RegularExpression with find files

Thanks :lol:
Post Reply