-2

My operating system is Windows 10.

I have a folder containing one to many .txt files. Within each file are lines formatted as the following:

2016-12-07 14:08:43 7.7 b=123 r=890

I am looking to create a reproducible process that I can execute that will look through all the files in the folder and provide me with the filenames in a .txt file of all the files that contain 6.7 and a date greater than seven days ago.

Example scenario:

File1 contains: 2016-12-07 14:08:43 7.7 b=123 r=890

File2 contains: 2017-01-24 14:08:43 7.7 b=123 r=890

File3 contains: 2017-01-23 14:08:43 6.7 b=123 r=890

I execute the process and I receive a .txt file containg: File3

I don't know if this can be done using command line, PowerShell, or if full development efforts are needed to accomplish this task.

  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Jan 28 '17 at 15:15
0

Following PowerShell script

Get-ChildItem *.txt -Recurse |
    ForEach-Object { 
        $aux=$_
        Get-Content "$aux" | 
            ForEach-Object {
                if ($_ -match '6\.7') {
                    if ( [datetime] $_.Substring(0,19) -gt (Get-Date).AddDays(-7)) {
                        $aux.FullName
                    }
                }
            }
    }

or equivalent oneliner

Get-ChildItem *.txt -Recurse | ForEach-Object { $aux=$_; Get-Content "$aux" | ForEach-Object {if ($_ -match '6\.7') {if ( [datetime] $_.Substring(0,19) -gt (Get-Date).AddDays(-7)) {$aux.FullName}}}}

Basic explanation:

  • Get a list of txt files Get-ChildItem and | pipe result to next cmdlet
  • (piped) for every particular file ForEach-Object
  • save it to an auxiliary variable $aux=$_ (note trailing ; semicolon in the oneliner)
  • and get it's content line by line Get-Content and | pipe result to next cmdlet
  • (piped) for every particular line ForEach-Object
  • test whether a line contains 6.7 string if ($_ -match '6\.7')
  • and if so then test whether date (first 19 characters) matches given criteria if ( [datetime] …
  • and if so then make public file name $aux.FullName
  • and close all opening {s using series of closing }s.
  • This script writes the name of the file to the PowerShell window for each occurrence of the defined search text. It does not create a new file containing this information. I'll have to look into how to write the PowerShell displayed results to file to get where I want to be. I appreciate your answer. – HappyCoding Jan 30 '17 at 14:52
  • @HappyCoding you can pipe output e.g. to Out-File cmdlet. Read again David's comment. – JosefZ Jan 30 '17 at 15:18
0

Expanding on JosefZ's original answer, I find the following answer more complete.

Include the following within a PowerShell file:

$filespath = "\\sampledirectory\log\"
$outputfilename = "\\sampledirectory\log\results.txt"

########## file creation step
Get-ChildItem -Path $filespath *.txt -Recurse |
    ForEach-Object { 
        $aux=$_
        Get-Content "$aux" | 
            ForEach-Object {
                if ($_ -match '6\.7')
                {
                    if ( [datetime] $_.Substring(0,19) -gt (Get-Date).AddDays(-7))
                    {
                        $aux.FullName | Out-File $outputfilename -append
                    }
                }
            }
    }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.