复制代码
- $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 {
- #$Path = Read-Host
- $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")
- {
- #Return $objForm.SelectedPath
- $Path = $objForm.SelectedPath
- $default_path = $objForm.SelectedPath
- }
- Else
- {
- Write-Warning 'Cancelled'
- }
- }
- #if([String]::IsNullOrEmpty($Path))
- 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(" "); # get -rwi option , just like grep aaa -rwi *;
- Foreach($split_ele in $mySearchStr)
- {
- # write-host "split_ele=" $split_ele
- }
- $MySearchString_tmp = $mySearchStr[0]
- $MySearchString_option_rwi = $mySearchStr[1]
- Write-Host "MySearchString_tmp =" $MySearchString_tmp
- Write-Host "MySearchString_option_rwi =" $MySearchString_option_rwi
- # 电锯惊魂
- # $MySearchString = "*Mad.Max.1979.*"
- $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 ){
- ### Write-Host "Processing : $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 +++++++++++" }
- #exit
- if($tmpContent[$i] -cmatch $MySearchString_wi)
- {
- #write-host ""
- #write-host $file
- 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 ""
- #write-host $file
- 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 ""
- #write-host $file
- write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
- $found = 1;
- }
- } else {
- if($tmpContent[$i] -like "$MySearchString")
- {
- #write-host ""
- #write-host $file
- write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
- $found = 1;
- }
- }#if -wi
- }#for
- }#if tmpContent.length
- }
- $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"
- # Start-Sleep -s 10
- }
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |