A.Watkins
A.Watkins

Reputation: 37

Powershell - txt file search

Im currently trying to get a script to do the following:

search a file location and obtain any .txt,.ini,.config file. filter this file on an input from the user. move all the files to a location.

Im having a problem trying to feed in the variable and its probably really simple but im currently struggling to figure out what I need to do without splitting the string up manually. any ideas?

$QQ = Read-Host -Prompt "String your searching for:"
$QL = Read-Host -Prompt "Enter the file location you wish to search:"
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | %  {$_.fullname}
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | %  {$_.fullname}
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | %  {$_.fullname}
$FTS = Get-Content -Path "$FT" -Filter "$QQ" 
$FIS = Get-Content -Path "$FI" -Filter "$QQ"
$FCS = Get-Content -Path "$FC" -Filter "$QQ"
$FD = "C:\Search-$QQ"
$FD1 = Get-ChildItem $FD

function folder {
    if ($FD -eq $Null) {New-Item "$FD" -ItemType directory}}

function search{
    if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd"  | Write-Host "$FT" | Format-List}
    if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List}
    if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List}
}
folder
search;

An example of the error being received is: (obviously an issue with it treating the multiple files in the string as one)

Get-Content : Cannot find path 'C:\test\Test\1c.config C:\test\Test\2c.config C:\test\Test\3c.config' because it does not exist.

Upvotes: 0

Views: 193

Answers (3)

Esperento57
Esperento57

Reputation: 17492

try this

$QL = Read-Host -Prompt "Enter the file location you wish to search:"
if (-not (Test-Path $QL))
{
  write-host "Specified path ($QL) doesnt exists "  
  return;
}

$QQ = Read-Host -Prompt "String your searching for:"
$FD = "C:\Search-$QQ"
New-Item "C:\Search-$QQ" -ItemType directory -Force | out-null

Get-ChildItem -Path "$QL" -recurse -include "*.txt", "*.ini", "*.config" | 
    select-string -Pattern "$QQ" -SimpleMatch | 
        %{ Copy-Item -Path $_.Path -Destination "$fd" -Force ; $_.Path }

Upvotes: 0

A.Watkins
A.Watkins

Reputation: 37

(This Script is to mass search application files to find values like server id's or passwords hardcoded etc)

Fixed code:

$QQ = Read-Host -Prompt "String your searching for:"
$QL = Read-Host -Prompt "Enter the file location you wish to search:"
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | %  {$_.fullname}
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | %  {$_.fullname}
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | %  {$_.fullname}
$FTS = Get-Content -Path $FT -Filter "$QQ" 
$FIS = Get-Content -Path $FI -Filter "$QQ"
$FCS = Get-Content -Path $FC -Filter "$QQ"
$FD = "C:\Support\Search-$QQ"
$FD1 = Test-Path $FD

function folder {
if ($FD1 -eq $false) {New-Item "$FD" -ItemType directory}
}
function search{
    if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd"  | Write-Host "$FT" | Format-List}
    if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List}
    if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List}
}

folder
search;

Upvotes: 0

Mike Garuccio
Mike Garuccio

Reputation: 2718

right off the bat I see the folder function will not work property as $FD will never be null since you assign a string to it, if you need to check if the folder exists use Test-Path. but in relation to your actual script the problem is your use of get-content which is trying to read all of your files contents into an array of strings. from the looks of it you should just be working with the arrays returned by Get-ChildItem and use Test-Path to check your paths rather than that weird Get-Content method you are using(since the content of the files doesn't matter, just whether they exist or not). You also need to incorporate a loop to act on each array element individually rather than trying to work on them in groups like you are.

Upvotes: 1

Related Questions