2013-12-18 2 views
1

사용자가 입력 한 문자열로 사용자가 입력 한 디렉토리의 모든 파일을 찾기 위해 powershell을 사용하고 있습니다. 지금 당신이 내 스크립트를 실행하고 하위 폴더가없는 파일 경로에 입력하면 스크립트는 정상적으로 작동하지만 하위 폴더가있는 폴더를 추가하면 오류가 발생합니다. 코드를 작성하는 몇 가지 방법을 시도했지만 성공하지 못했습니다. 상위 폴더뿐만 아니라 하위 폴더도 검색 할 수 있어야합니다. 이것은 내가 지금 가지고있는 것입니다.PowerShell에서 특정 문자열이있는 파일을 찾으려고합니다.

#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") 

답변

5

Select-String에 폴더를 지정하지 마십시오.

Get-ChildItem -r -file | ss $File_Name 
+0

감사합니다 키이스,이 챔피언처럼 일 :

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

또는에 당신이 간단하게 할 수 이상에서

ss $File_Name * | Format-List FileName 

:이 줄을 교체합니다. 나는 'Where'가 없으면 비슷한 코드를 가지고 있는데, 그것은 나에게 효과가 없다. –

관련 문제