CM_Heroman
CM_Heroman

Reputation: 378

Trying to find files with a certain string in PowerShell

I am working on using powershell to find all files in a directory input by the user with a string input by the user. Right now if you run my script, and enter in a file path with no sub-folders, then the script works fine, but if I add a folder with sub-folders then it gives me an access denied error. I have tried a few different ways to code it but have had no success. I need it to be able to search the parent directory as well as the child folders. This is what I have right now.

#Sets the ability to execute the script
Set-ExecutionPolicy RemoteSigned

#Requests the loctation of the files to search
$Location = Read-Host 'What is the folder location of the files you want to search?'

#Sets the location based off of the above variable
Set-Location $Location 

#Sets the alias
Set-Alias ss Select-String 

#Requests the text to search for in the files
$File_Name = Read-Host 'Object Name?'

#Searches the files and returns the filenames
ss $File_Name * | Format-List FileName

#Sets location back to root
Set-Location C:

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Upvotes: 1

Views: 285

Answers (2)

LuisCunhaPT
LuisCunhaPT

Reputation: 1

My Script is dynamic and works to find specific file in 3 000 000, and commented part to delete a writhe in a log file.

Param ( #Define o parametro do ano a eliminar "2020" $DateDel = '2018', $DateDe2 = '2019', $DateDe3 = '2020',

 #Define o parametro do registro do ficheiro "_800" ou "_800sm" ou "_200"
 $ResFile1 = '_200',
 $ResFile2 = '_800',
 $ResFile3 = '_800sm',

 #Define o parametro da terminacao do ficheiro "_800.jpg" ou "_800sm.jpg" ou "_200.jpg"
 $TypeFile = '.jpg',
 
 #Define o parametro de onde se localizado ficheiro "C:\users\Luis.Cunha\Desktop\LuisCunha\TarefaScript\TesteFinal\TesteScript1"
 $HomePath = 'C:\Users\Luis.Cunha\Desktop\LuisCunha\TarefaScript\TesteFinal1'   

)

 #Inicia transcriçao de toda informação para o ficheiro .log indicado
 Start-Transcript -Path $HomePath\CountDelItems.log -NoClobber -Append

 Get-ChildItem $HomePath -Recurse -File | Measure-Object | %{$_.Count}

 #o Get vai buscar o ficheiro com a data e a terminacao definidas no $homepath e $tipofich atraves do caminho indicado no $path
 #depois confirma os valores que foram removidos com o verbose
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}
    
 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 
 
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force
 
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count} 

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force

 

 
 Get-ChildItem $HomePath -Recurse -File | Measure-Object | %{$_.Count}


 #Termina transcrição
 Stop-Transcript 

Upvotes: -1

Keith Hill
Keith Hill

Reputation: 201592

Do not specify folders to Select-String. Replace this line:

ss $File_Name * | Format-List FileName

with

Get-ChildItem -r | Where {!$_.PSIsContainer} | ss $File_Name

On V3 or higher you can simplify to:

Get-ChildItem -r -file | ss $File_Name

Upvotes: 5

Related Questions