Jump to content

Add line to file when not exists


ktc
 Share

Recommended Posts

I face the following challenge.

I have a directory with all kind of files. I want to search for .pst and then add a commandline (based on found file) to a text file.

However if the commandline already exists in the text file it should skip en go to the next file.

The problem I face is that the line is added multiple times. It does it for every line it finds.

#include <file.au3>
#include <guiconstantsex.au3>
#include <windowsconstants.au3>
Global $aRecords, $Share, $strUser, $strDestination
$Share = "\\Server01\Share01"
$strUser = "my.user"
$strDestination = $Share & $strUser
_Filelist($strDestination)

Func _Filelist($searchdir)
$search = FileFindFirstFile($searchdir & "\*.pst")
If $search = -1 Then return -1
$count = 1
While 1
  $file = FileFindNextFile($search)
  If @error Then
   FileClose($search)
   Return
  Elseif  $file = "."  or $file = ".." Then
   ContinueLoop
  ElseIf stringinstr(FileGetAttrib($searchdir & "\" & $file),"D") then
   _filelist($searchdir & "\" & $file)
  EndIf
  ;$Line = $searchdir & "\" & $file & @crlf
  $Cmd1Line = $searchdir & "\" & $file
  $strArchive = StringTrimRight($file, 4)
  $Command = "New-MailboxImportRequest –Mailbox " & $strUser & " -IsArchive –FilePath " & '"' & $Cmd1Line & '"' & " -TargetRootFolder " & '"' & $strArchive & '"'
  If Not _FileReadToArray($Share & "\Command.txt", $aRecords) Then
   MsgBox(4096, "Error", " Error reading log to Array    error:" & @error)
   Exit
  EndIf
  For $x = 1 To $aRecords[0]
   If $aRecords[$x] = $Command Then
    ;MsgBox(0, 'Record:' & $x, $aRecords[$x])
   Else
    Local $file = FileOpen($Share & "\Command.txt", 1)
    FileWriteLine($Share & "\Command.txt", $Command)
    FileClose($Share & "\Command.txt")
   EndIf
  Next
$count = $count + 1
WEnd
EndFunc  ;==>_Filelist

Thank you.

Link to comment
Share on other sites

Hello ktc,

look for _RecFileListToArray from Melba23 in the forums. It will recursive list all files to an array.

Then you can be sure that you have every .pst file only once in the array listed.

Loop the array and create an entry in your destination file for every .pst file.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi Hannes08,

Tnx for your response,

I looked at the posts you mentioned but the problem is that I'm not so familiar with array’s ;) . The above script is create out of various help files and I hoped someone could help me with the difficult part. :)

Link to comment
Share on other sites

If you have all the files in a array say:

$a_pstfiles[0] = 3
$a_pstfiles[1] = "C:temppst1.pst"
$a_pstfiles[2] = "C:temp1pst2.pst"
$a_pstfiles[3] = "C:temp2pst3.pst"

You can loop the array and write your output file:

$fh = FileOpen("Commant.cmd", 2)
For $i = 1 To $a_pstfiles[0]
    FileWriteLine($fh, "New-MailboxImportRequest –Mailbox " & $strUser & " -IsArchive –FilePath " & '"' & $a_pstfiles[$i] & '" -TargetRootFolder ' & $strArchive & '"'
Next
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...