Sign in to follow this  
Followers 0
jackr57

Remove line if ad field empty

4 posts in this topic

#1 ·  Posted (edited)

Hi

I have a working outlook signature script, that pulls data from active directory. I am just having a problem with fields that are optional.

 

Func regsig ()

FileInstall("sig.htm", $sigpath & "\tmp.htm",1)
$filename1 = $sigpath & "\tmp.htm"
_ReplaceStringInFile($filename1,"namenamename",$fullname)
_ReplaceStringInFile($filename1,"emailemailemail","<a href='mailto:"& $emailaddress &"'>" & $emailaddress & "</a>")
_ReplaceStringInFile($filename1,"titletitletitle",$title)
_ReplaceStringInFile($filename1,"offnumoffnumoffnum",$officenumber)
_ReplaceStringInFile($filename1,"faxfaxfax",$faxnumber)
_ReplaceStringInFile($filename1,"extensionAttribute1extensionAttribute1extensionAttribute1",$extensionAttribute1)
If $mobilenumber <>"" Then
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "Mobile: " & $mobilenumber)
Else
    _FileWriteToLine($filename1, 13, "", 1)
EndIf
If $mainnumber <>"" Then
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", "Mobile: " & $mainnumber)
Else
    _FileWriteToLine($filename1, 14, "", 1)
EndIf
If $webpage <>"" Then
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage","<a href='"& $webpage &"'>" & $webpage & "</a>")
Else
    _FileWriteToLine($filename1, 16, "", 1)
EndIf
FileCopy($filename1,$sigpath & "\" & $sightm,1)
EndFunc

As you can see here

_FileWriteToLine($filename1, 13, "", 1)

This works great but the problem is that the line numbers change when certain fields are removed preventing this line removal method from working.

If the fields in AD are blank and the else statement is not there, then it simply writes "mobilemobilemobile" to the line as that is what it is in the template file. Ideally I would need a way to use _replacestringinfile in such a way that removes the line when the active directory field that is mapped to that variable is empty.

Else
_ReplaceStringInFile($filename1,"mobile2mobile2mobile2","")

This results in an empty line in the signature which is better than mobile2mobile2mobile2. but ideally would like to remove the line.

Edited by jackr57

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm I missing something or can't you just use StringRegExpReplace and include the line return, new line in the pattern to be replaced?

Else
     $filename1 = StringRegExpReplace($filename1, '(?i)(?-s)mobilemobilemobile\s*', '')
EndIf

 

Edited by Jury

Share this post


Link to post
Share on other sites

I also could not remove empty lines but you can create a new one without them.

#include <File.au3>

$original_file = @ScriptDir & "\test.ini"
$new_file = @ScriptDir & "\test new.ini"
$count_line = _FileCountLines($original_file)
For $i = 1 To $count_line
    $text = FileReadLine($original_file, $i)
    If $text <> "" Then FileWriteLine($new_file, $text)
Next

 

Share this post


Link to post
Share on other sites

Thanks for the suggestions. I realized while using the second solution that I can just remove the <br /> in the html as its html signature and this will remove the lines.

_ReplaceStringInFile($filename1,"mobile2mobile2mobile2<br />", "")

For text signature i can use the code:

$original_file = $filename1
$new_file = $sigpath & "\" & $sightm
$count_line = _FileCountLines($original_file)
For $i = 1 To $count_line
    $text = FileReadLine($original_file, $i)
    If $text <> "" Then FileWriteLine($new_file, $text)
Next

Unfortunately for the rtf signature there is no solution but no one uses rtf so they will have to put up with blank lines on that one or purchase some signature software.

 

Thanks again for your help.

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