locked
Check string on a dynamic file RRS feed

  • Question

  • Hi,

    I have a file which gets created every hour. I need to check the file contents and if it does not match a string, I need to execute an action.

    Copy-Item -Path D:\Data_rate.txt -destination $('D:\Data_rate_{0:yyyyMMdd_HH}.txt' -f (Get-Date))
    
    $Location = 'D:\Data_rate_{0:yyyyMMdd_HH}.txt'
    $SearchStr = "no rows selected"
    $Sel = Select-String  -pattern $SearchStr -path $Location 
    If ($Sel -NotMatch "no rows selected")
    {
        runcommand "Mismatch found $Location"
    }
     

    Files which are created every hour.

    Date_rate_20150624_09.txt

    Date_rate_20150624_08.txt

    I have verified and if I specify a static filename in $Location, the script works fine. I am running into issues when searching for the dynamic file.

    Any help will be greatly appreciated.


    Wednesday, June 24, 2015 1:51 PM

Answers

  • Read this first:


    PS C:\> help Select-String -Full
    

    Pay particular attention to the -List parameter, which is documented as follows:

    Returns only the first match in each input file. By default, Select-String returns a MatchInfo object for each match it finds.

    This means that if you don't use the -List parameter (which you are not from your sample, above), the Select-String cmdlet returns MatchInfo objects, not strings. If you just want to find whether a string appears in a file, then you can use the -List parameter.

    Implication: If Select-String -List returns a non-null string, then the string appears in the file. If Select-String -List returns a null string, then the string does NOT appear in the file.

    This is an exercise in reading and understanding the documentation that is already available to you.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 24, 2015 1:57 PM
  • Your $Location variable is not dynamic. You need -f (Get-Date) to make it dynamic. Try this:

    $Location = 'D:\Data_rate_{0:yyyyMMdd_HH}.txt' -f (Get-Date)
    Copy-Item -Path D:\Data_rate.txt -destination $Location
    
    $SearchStr = "no rows selected"
    $Sel = Select-String  -pattern $SearchStr -path $Location 
    If ($Sel -NotMatch "no rows selected")
    {
        runcommand "Mismatch found $Location"
    }



    Wednesday, June 24, 2015 2:05 PM

All replies

  • Read this first:


    PS C:\> help Select-String -Full
    

    Pay particular attention to the -List parameter, which is documented as follows:

    Returns only the first match in each input file. By default, Select-String returns a MatchInfo object for each match it finds.

    This means that if you don't use the -List parameter (which you are not from your sample, above), the Select-String cmdlet returns MatchInfo objects, not strings. If you just want to find whether a string appears in a file, then you can use the -List parameter.

    Implication: If Select-String -List returns a non-null string, then the string appears in the file. If Select-String -List returns a null string, then the string does NOT appear in the file.

    This is an exercise in reading and understanding the documentation that is already available to you.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 24, 2015 1:57 PM
  • Your $Location variable is not dynamic. You need -f (Get-Date) to make it dynamic. Try this:

    $Location = 'D:\Data_rate_{0:yyyyMMdd_HH}.txt' -f (Get-Date)
    Copy-Item -Path D:\Data_rate.txt -destination $Location
    
    $SearchStr = "no rows selected"
    $Sel = Select-String  -pattern $SearchStr -path $Location 
    If ($Sel -NotMatch "no rows selected")
    {
        runcommand "Mismatch found $Location"
    }



    Wednesday, June 24, 2015 2:05 PM
  • Awesome, that worked. Thank You.

    Wednesday, June 24, 2015 2:24 PM
  • I was using null to match the string and that was working. My issue was with the dynamic file. Thank you.
    Wednesday, June 24, 2015 2:25 PM