locked
find a special string in a file RRS feed

  • Question

  • Hi,

    I'm creating a powershell script to find new label. I have a text file with the label already knowed.

    The label to find is "#FIDI#FIDI-+-ASSURANCE#" ; I think there is glitch with "-+-".

    Here is the powershell code :

    # label already knowed
    $File_LabelKnown = "X:\CCCCCC\BW_Qualifications.txt"
    # directory where the records are store
    $directory_Origine1 = "X:\XXXXX\Test"
    
    # date and time
    $Time = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
    # creation file for new label
    $FileQualifications = "x:\CCCCCC\" + "BW_Qualifications_" + $Time + ".txt"
    # adding a column name
    "Qualification complete" | Out-File $FileQualifications -Append -Force
    
    foreach ($file in (Get-ChildItem -Path $directory_Origine1 -Include *.wav -Recurse ))
    {
    $fichier = $file.Name
    $label1 = $fichier.split('#')[1]
    $label2 = $fichier.split('#')[2]
    $label = "#" + $label1 + "#" + $label2 + "#"
    if (!(Get-Content $File_LabelKnown | Select-String -Pattern $label))
    	{
    
    # add label in the new label file
    $label | Out-File $FileQualifications -Append -Force
    		
    	}
    }

    Monday, April 11, 2016 12:30 PM

Answers

  • The plus gets escaped:

    "#FIDI#FIDI-+-ASSURANCE#" -match "#FIDI#FIDI-\+-ASSURANCE#"


    \_(ツ)_/

    • Marked as answer by GenePatton Tuesday, April 19, 2016 9:26 AM
    Monday, April 11, 2016 12:51 PM
  • Since you already know the label (you have stored that in a text file.), you can compare those in this way.

    Get-Content $File_LabelKnown | ForEach-Object { 
    if($_ -match  "^#\w{4}(#\w{4})?-\+-.*#$")
    {
    	
    # add label in the new label file
    $label | Out-File $FileQualifications -Append -Force
    }
    
    }

    • Marked as answer by GenePatton Tuesday, April 19, 2016 9:26 AM
    Monday, April 11, 2016 3:40 PM

All replies

  • The plus gets escaped:

    "#FIDI#FIDI-+-ASSURANCE#" -match "#FIDI#FIDI-\+-ASSURANCE#"


    \_(ツ)_/

    • Marked as answer by GenePatton Tuesday, April 19, 2016 9:26 AM
    Monday, April 11, 2016 12:51 PM
  • Hi,

    OK, I see what you mean but how do I put this in my already knowed label file ? because i have a bunch of these :

    #FIDI#FIDI-+-ASSURANCE#
    #FIDI#FIDI-+-ASSURANCE-+-E-BILLING#
    #FIDI#FIDI-+-AUG#
    #FIDI#FIDI-+-AUG-+-ASM#
    #FIDI#FIDI-+-AUG-+-E-BILLING#
    #FIDI#FIDI-+-E-BILLING#
    #FIDI-+-E-BILLING##

    I can test everyone, one by one but it'isn't very efficient.

    best regards

    Monday, April 11, 2016 1:27 PM
  • You don't change the file.  Change the pattern.

    \_(ツ)_/

    Monday, April 11, 2016 2:01 PM
  • You can use a regular expression for that.

    "^#\w{4}#\w{4}-\+-.*#$"

    "#FIDI#FIDI-+-ASSURANCE#" -match "^#\w{4}#\w{4}-\+-.*#$"

    Monday, April 11, 2016 2:08 PM
  • Hi,

    Thank you for your answers bu how i put your prposition in my code ?

    I try :

    if (!(Get-Content $File_LabelKnown | Select-String -Pattern ("#FIDI#FIDI-+-ASSURANCE#"  -match "#FIDI#FIDI-\+-ASSURANCE#")))

    Monday, April 11, 2016 2:53 PM
  • Since you already know the label (you have stored that in a text file.), you can compare those in this way.

    Get-Content $File_LabelKnown | ForEach-Object { 
    if($_ -match  "^#\w{4}(#\w{4})?-\+-.*#$")
    {
    	
    # add label in the new label file
    $label | Out-File $FileQualifications -Append -Force
    }
    
    }

    • Marked as answer by GenePatton Tuesday, April 19, 2016 9:26 AM
    Monday, April 11, 2016 3:40 PM
  • Hi,

    I'm almost there. I just a problem with those labels :

    $LabelSpecial9 = "#CONTACT-\(ARGUMENTé\)#AUG#"
    $LabelSpecial10 = "#CONTACT-\(ARGUMENTé\)#CARTE#"
    $LabelSpecial11 = "#CONTACT-\(ARGUMENTé\)#PAS-INTERESSE#"
    $LabelSpecial12 = "#CONTACT-\(NON-ARGUMENTé\)#ERREUR-NUM-CLIENT#"
    $LabelSpecial13 = "#CONTACT-\(NON-ARGUMENTé\)#MESSAGE#"
    $LabelSpecial14 = "#CONTACT-\(NON-ARGUMENTé\)#NON-éLIGIBLE#"
    $LabelSpecial15 = "#MESSAGERIE-\(MESSAGE-LAISSé\)##"

    I'm guessing the "()" are the escape characters but I can't get a match

    here is my code so far :

    # declaration du repertoire ou on va chercher les fichiers
    $File_LabelKnown = "E:\XXXX\XX_Qualifs.txt"
    $directory_Origine1 = "D:\RECORDS_ALL\Test"
    
    # Declarartion de l'expediteur
    $ReportSender = "label@compagny.com"
    # Declaration des destinataires
    $ReportRecipient = "Jon.smith@compagny.com"
    
    $LabelSpecial1 = "#ASSURANCE#ASSURANCE-\+-E-BILLING#"
    $LabelSpecial2 = "#FIDI#FIDI-\+-ASSURANCE#"
    $LabelSpecial3 = "#FIDI#FIDI-\+-ASSURANCE-\+-E-BILLING#"
    $LabelSpecial4 = "#FIDI#FIDI-\+-AUG#"
    $LabelSpecial5 = "#FIDI#FIDI-\+-AUG-\+-ASM#"
    $LabelSpecial6 = "#FIDI#FIDI-\+-AUG-\+-E-BILLING#"
    $LabelSpecial7 = "#FIDI#FIDI-\+-E-BILLING#"
    $LabelSpecial8 = "#FIDI-\+-E-BILLING##"
    $LabelSpecial9 = "#CONTACT-\(ARGUMENTé\)#AUG#"
    $LabelSpecial10 = "#CONTACT-\(ARGUMENTé\)#CARTE#"
    $LabelSpecial11 = "#CONTACT-\(ARGUMENTé\)#PAS-INTERESSE#"
    $LabelSpecial12 = "#CONTACT-\(NON-ARGUMENTé\)#ERREUR-NUM-CLIENT#"
    $LabelSpecial13 = "#CONTACT-\(NON-ARGUMENTé\)#MESSAGE#"
    $LabelSpecial14 = "#CONTACT-\(NON-ARGUMENTé\)#NON-éLIGIBLE#"
    $LabelSpecial15 = "#MESSAGERIE-\(MESSAGE-LAISSé\)##"
    
    #SendEmailFunction
    Function sendEmailhtmlSupp
    { 
    # passage du fichier en parametre
    param($fileNewLabel)
    # declaration du server mail
    $smtphost="mailhost"
    # Declaration du sujet
    $MailSubjectSupp = ("Rapport de Qualification" + " - " + ( get-date ).ToString('yyyy/MM/dd'))
    $FileNameNewLabel = $fileNewLabel.split('\')[2]
    # declaration du message
    $bodySupp="La liste des nouvelles qualifications."
    # commande d'envoi du mail
    Send-MailMessage -from $reportsender -to $reportrecipient -subject $mailsubjectSupp -Body $bodySupp -smtpserver $smtphost -BodyAsHtml
    }
    
    # recuperation de la date
    $Time = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
    # declaration du fichier
    $FileQualifications = "C:\VocEnreManipLog\" + "XX_Qualifs_" + $Time + ".txt"
    "Qualification complete" | Out-File $FileQualifications -Append -Force
    
    # recuperation de tous les fichiers wav contenu dans le repertoire et les sous-repertoire de recherche
    foreach ($file in (Get-ChildItem -Path $directory_Origine1 -Include *.wav -Recurse ))
    {
    	$Weapons = 0
    	$FNC = 1
    	$FN2000 = 2
    	$MINIMI = 3
    	$SCARL = 4
    	$P90 = 5
    	$FiveSeven = 6
    	# recuperation du nom du fichier
        $fichier = $file.Name
        # recuperation du chemin du fichier
        $path_fichier = $file.fullname
    	# recuperation de ce qui se trouve entre le 3ier et le 4ieme caracter special
    	$campagne = $path_fichier.split('\')[3]
    	# recuperation de ce qui se trouve entre le 4ier et le 5ieme caracter special
    	$FolderLevel2 = $path_fichier.split('\')[4]
        # recuperation de ce qui se trouve entre le 1ier et le 2ieme caracter special     
    	$label1 = $fichier.split('#')[1]
    	# recuperation de ce qui se trouve entre le 2ieme et le 3ieme caracter special
    	$label2 = $fichier.split('#')[2]
    	# concatenation et insertion de caractere special pour avoir la qualification de l appel
    	$label = "#" + $label1 + "#" + $label2 + "#"
    	$label
    	#
    	if (!(Get-Content $File_LabelKnown | Select-String -Pattern $label))
    	{
    		Write-Host "1"
    		$Weapons = $Weapons + $FNC
    		
    		if (!(Get-Content $FileQualifications | Select-String -Pattern $label))
    		{		
    			Write-Host "2"
    			$Weapons = $Weapons + $FN2000
    			
    			for ($j=1; $j -le 15; $j++)
    			{
    				Write-Host "3"
    				$Weapons = $Weapons + $MINIMI
    				
    				$SpecialFilter_var = Get-Variable -Name "LabelSpecial$j" -ValueOnly
    				$SpecialFilter_var
    				
    				if ($label -Match $SpecialFilter_var)
    				{
    					$Weapons =  $Weapons + $SCARL
    					Write-Host "4"
    					
    					if (!(Get-Content $File_LabelKnown | Select-String -Pattern $SpecialFilter_var))
    					{
    						$Weapons =  $Weapons + $P90
    						Write-Host "5"
    						
    						if (!(Get-Content $FileQualifications | Select-String -Pattern $SpecialFilter_var))
    						{
    							$Weapons =  $Weapons + $FiveSeven
    							Write-Host "6"
    							
    							# ajout dans un fichier le label entier
    							#$label | Out-File $FileQualifications -Append -Force
    						}
    					}
    				}
    			}
    		}
    	}
    	$Weapons
    	if ($Weapons -eq 48)
    	{
    		# ajout dans un fichier le label entier
    		$label | Out-File $FileQualifications -Append -Force
    	}
    }
    
    
    # envoi mail
    sendEmailhtmlSupp $FileQualifications

     

    Monday, April 18, 2016 12:00 PM
  • parens have to be escaped or they will be seen as part of the template.

    Start here: https://en.wikipedia.org/wiki/Regular_expression


    \_(ツ)_/

    Monday, April 18, 2016 7:00 PM
  • Thanks
    Tuesday, April 19, 2016 9:26 AM