| $default_path = "" |
| [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null |
| while(1) |
| { |
| $Path ="D:\" |
| $Filter = '*.*' |
| $_Debug = 0 |
| $DEBUG_LEVEL = 0 |
| |
| |
| Write-Host "请选择要搜索的文件夹路径:例如 D:\test" |
| if ($_Debug) { |
| $Path = "D:\___sv_cpp_py\vcs\uvm-1.2\src" |
| } else { |
| |
| |
| $objForm = New-Object System.Windows.Forms.FolderBrowserDialog |
| $objForm.RootFolder = "MyComputer" |
| $objForm.Description = "选择需要搜索的文件夹: " |
| $objForm.ShowNewFolderButton = "true" |
| $objForm.SelectedPath = "d:\___sv_cpp_py\vcs" |
| |
| if($default_path -eq ""){ |
| }else { |
| $objForm.SelectedPath = $default_path |
| } |
| $Show = $objForm.ShowDialog() |
| |
| If ($Show -eq "OK") |
| { |
| |
| $Path = $objForm.SelectedPath |
| $default_path = $objForm.SelectedPath |
| } |
| Else |
| { |
| Write-Warning 'Cancelled' |
| } |
| |
| } |
| |
| |
| if(!(Test-Path $Path)) |
| { |
| $Path =".\" |
| Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path |
| } |
| |
| Write-Host "" |
| Write-Host "" |
| Write-Host "Path = " $Path |
| |
| if ($_Debug) { |
| $MySearchString_tmp = "domain " |
| } else { |
| Write-Host "请输入要搜索的字符串:" |
| $MySearchString_tmp = Read-Host |
| } |
| |
| while([String]::IsNullOrEmpty($MySearchString_tmp)) |
| { |
| Write-Host "The string is null or empty." |
| Write-Host "请重新输入要搜索的字符串:" |
| $MySearchString_tmp = Read-Host |
| } |
| |
| Write-Host "String = " $MySearchString_tmp |
| Write-Host "" |
| Write-Host "" |
| |
| $mySearchStr = $MySearchString_tmp.split(" "); |
| Foreach($split_ele in $mySearchStr) |
| { |
| |
| } |
| |
| |
| |
| $MySearchString_tmp = $mySearchStr[0] |
| $MySearchString_option_rwi = $mySearchStr[1] |
| Write-Host "MySearchString_tmp =" $MySearchString_tmp |
| Write-Host "MySearchString_option_rwi =" $MySearchString_option_rwi |
| |
| |
| $MySearchString = "*$MySearchString_tmp*" |
| $MySearchString_w = "\b$MySearchString_tmp\b" |
| $MySearchString_i = "$MySearchString_tmp" |
| $MySearchString_wi = "\b$MySearchString_tmp\b" |
| Write-Host "MySearchString =" $MySearchString |
| Write-Host "MySearchString_w =" $MySearchString_w |
| Write-Host "MySearchString_i =" $MySearchString_i |
| Write-Host "MySearchString_wi =" $MySearchString_wi |
| |
| function processing_grep( $file ){ |
| |
| |
| if($DEBUG_LEVEL -gt 0) { write-host "file_name=" $file } |
| $tmpContent = Get-Content $file |
| if($DEBUG_LEVEL -gt 0) {write-host "file_length=" $tmpContent.length } |
| if($tmpContent.length -gt 0){ |
| for ($i=0; $i -le $tmpContent.length; $i++) |
| { |
| if($MySearchString_option_rwi -match "wi") { |
| if($DEBUG_LEVEL -gt 0) { Write-Host "++++++++++MySearchString_wi like wi +++++++++++" } |
| |
| |
| if($tmpContent[$i] -cmatch $MySearchString_wi) |
| { |
| |
| |
| write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black |
| $found = 1; |
| } |
| } elseif ($MySearchString_option_rwi -match "i") { |
| if($tmpContent[$i] -cmatch $MySearchString_i) |
| { |
| |
| |
| write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black |
| $found = 1; |
| } |
| } elseif ($MySearchString_option_rwi -match "w") { |
| if($tmpContent[$i] -match $MySearchString_w) |
| { |
| |
| |
| write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black |
| $found = 1; |
| } |
| } else { |
| if($tmpContent[$i] -like "$MySearchString") |
| { |
| |
| |
| write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black |
| $found = 1; |
| } |
| } |
| } |
| } |
| } |
| |
| |
| $found = 0; |
| |
| |
| ,@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} ) |
| |
| if ($found -eq 0) |
| { |
| write-host "没有找到您查找的字符串,请重新输入吧,O(∩_∩)O~ " -ForegroundColor red -background black |
| } |
| |
| |
| cmd /c "pause" |
| |
| }COPY |