Crayven
(Fresh Scripter)
2016-12-28 12:14 PM
Delete empty line from a text file

I find this post; and it 's really what i need !!
I have a .txt where a vbs delete 2 random lines .
This is a file in which 2 known administrator account names are cleared so as to only keep those that are unauthorized and unknown.

Example:
-After filling in c:\Admins.txt :
Admaster
Administrateur
Administratec
Administrator
Administratsrv

-After wiping:
Admaster

Administratec

Administratsrv

I want find this more or less 3 lines and take this in one line with ";" to separe string.
in batch there is : findstr /v "^$" "c:\Admins.txt" > c:\toto.txt


Mart
(KiX Supporter)
2017-01-09 08:56 AM
Re: Delete empty line from a text file

Untested but this looks like it should do the job. The only thing that need to be changed is the path and names of the files with all admin names and the output file.

 Code:
Break on

;set path and name of file with all admin names including empty lines
$alladminnames = "C:\AllAdminNames.txt"
;set path and name of file with cleaned up admin names excluding empty lines
$cleanedadminnames = "C:\CleanedAdminNames.txt"

;open file with all admin names including empty lines for reading
$rc = Open(1, $alladminnames, 2)
;open file with cleaned up admin names excluding empty lines for writing
$rc = Open(2, $cleanedadminnames, 5)

;read first line from file with all admin names
$line = ReadLine(1)
;check if there was an error (end of file for example)
While Not @ERROR
	;check if line in not empty
	If Trim($line) <> ""
		;check if output variable is not empty to prevent ;name1;name2;....
		If $admins <> ""
			;add line to output variable
			$admins = $line
		Else
			;add ; and line to output variable
			$admin = $admins + ";" + $line
		EndIf
	EndIf
	;read next line from file with all admin names
	$line = ReadLine(1)
Loop

;write output variable to file with cleaned up admin names
$rc = WriteLine(2, $admins)

;close file with all admin names including empty lines
$rc = Close(1)
;close file with cleaned up admin names excluding empty lines
$rc = Close(2)