Windows Batch脚本将文件名作为输入并在文件夹位置搜索文件

时间:2015-04-01 14:31:33

标签: batch-file

我想创建一个Windows批处理文件,主要包括3个任务:

1.它会询问用户输入,如:

Please Enter file name to search:

您必须提供文件名,并根据文件名在一些指定的位置进行搜索。

2.如果找到该文件,它将再次在该文件中搜索某种模式(例如“错误”)。

3.从找到模式的行复制10行,并将内容粘贴到另一个文件中。

示例:

您输入的文件名为: DemoFile 1.它将在指定位置搜索 DemoFile.log

2.如果找到上面的文件,它将搜索某种模式(例如“错误”)。

3.在第20行找到了3.Suppoese模式错误,然后它将内容从第20行复制到第30行到另一个文件( MyDemoFile.txt )。

看起来相当复杂。无论如何都需要帮助。

提前致谢:)

下面是我的代码片段,其中提示用户输入文件名。

@ECHO OFF


*** Prompting user to enter the id*****
SET /P uname=Please enter the conversation id: 
IF "%uname%"=="" GOTO Error
ECHO Entered Conversation Id : %uname%, Below Are the Log files related to this id:

FOR /F "tokens=* delims=" %%x in (a.txt) DO echo %%x

*** finding files based on that conversation id****
set LISTFOLDER=C:\Users\demo\Desktop\stats\test\a.txt -- passing parameter as conversation id
set FILESPATH=\\test\*.log
set DESTPATH=C:\Users\demo\Desktop\stats\test\check

for /f "tokens=*" %%i in ('dir /b ^"%LISTFOLDER%\*.txt^"') do (call :COPY_FILES "%LISTFOLDER%\%%i")

pause
exit

1 个答案:

答案 0 :(得分:0)

基本

抬起头来。我不打算用纯批处理和内置Windows工具来解决这个问题。 (实际上有争议。请继续阅读!)我将假设环境没有被100%锁定,我们可以使用稍微好一点的工具来实现第3部分。

根据问题,示例代码和一些注释,我们要提示输入文件名的子模式,并搜索一个目录中与该模式匹配的所有文件。

批量

这是一种提示方式,检查给定目录中是否存在任何文件,如果是,则查找匹配的行。 (第1和第2部分)

@ECHO OFF
SETLOCAL

SET /P file=Please enter the thing you want to search in: 

SET logfiles=some\path\to\%file%*.log

IF EXIST "%logfiles%" (
    FINDSTR /I /P Error "%logfiles%" > "some\output\path\%file%_results.log"
) ELSE (
    ECHO No matches for "%logfiles%" found.
    EXIT /B 1
)

进入子目录是一项练习。

第3部分,在比赛后得到10条线是比较棘手的。这可能是我所谓的愚蠢的批量技巧"。我现在无法想出来。建议和编辑欢迎。

但是,我个人会用不同的工具解决这个问题,假设我可以。我会假设我们可以。

的PowerShell

默认情况下,从Windows 7 / Windows Server 2008R2开始,在许多Windows配置上安装了

PowerShell。它的Select-String cmdlet有一个-Context参数,可以控制包含多少行上下文。像下面这样的cmdlet是我开始使用的骨架。我故意写入cmdlet中的输出文件,以便以后能够利用PowerShell的管道。这是一个你可以轻易改变的决定。

function Get-ErrorFromLog {
  <#
  .SYNOPSIS
  Search through logs that match and extract the error lines and some context
  .EXAMPLE
  Get-ErrorFromLog -Log server
  .PARAMETER Log
  The base name of the log to search through. .log is automatically appended and we automatically search in SearchBase
  .PARAMETER SearchBase
  The directory to look in for logs
  #>
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$True,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$True,
    HelpMessage='What log would you like to search in?')]
    [string]$Log,

    [Parameter(Mandatory=$False)]
    [string]$SearchBase='some\path'
  )

  $logfiles = Get-ChildItem -Path $SearchBase -Filter ($Log + '*.log') -File -ErrorAction SilentlyContinue

  if ($logfiles) {
      $logfiles | Select-String -Pattern 'error' -Context 0,10
  } else {
      Write-Error "No items matchings `"$Log*.log`" exist in $SearchBase"
  }
}

一些示例输出

PS D:\> Get-ErrorFromLog -Log does-not-exist
Get-ErrorFromLog : No items matchings "does-not-exist*.log" exist in some\path
At line:1 char:1
+ Get-ErrorFromLog -Log does-not-exist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-ErrorFromLog


PS D:\> Get-ErrorFromLog -Log does-not-exist -SearchBase D:\temp\search
Get-ErrorFromLog : No items matchings "does-not-exist*.log" exist in D:\temp\search
At line:1 char:1
+ Get-ErrorFromLog -Log does-not-exist -SearchBase D:\temp\search
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-ErrorFromLog


PS D:\> # d:\temp\search\a.log exists, but has no errors

PS D:\> Get-ErrorFromLog -Log a -SearchBase D:\temp\search

PS D:\> Get-ErrorFromLog -Log b -SearchBase D:\temp\search

> temp\search\b.log:8:8 Error
  temp\search\b.log:9:9 Attempting recovery
  temp\search\b.log:10:10 Recovery in progress
  temp\search\b.log:11:11 Recovery finished
  temp\search\b.log:12:12 Nothing wrong at this time
  temp\search\b.log:13:13 Nothing wrong at this time
  temp\search\b.log:14:14 Nothing wrong at this time
  temp\search\b.log:15:15 Nothing wrong at this time
  temp\search\b.log:16:16 Nothing wrong at this time
  temp\search\b.log:17:17 Nothing wrong at this time
  temp\search\b.log:18:18 Nothing wrong at this time
> temp\search\b2.log:8:8 Error
  temp\search\b2.log:9:9 Attempting recovery
  temp\search\b2.log:10:10 Recovery in progress
  temp\search\b2.log:11:11 Recovery finished
  temp\search\b2.log:12:12 Nothing wrong at this time
  temp\search\b2.log:13:13 Nothing wrong at this time
  temp\search\b2.log:14:14 Nothing wrong at this time
  temp\search\b2.log:15:15 Nothing wrong at this time
  temp\search\b2.log:16:16 Nothing wrong at this time
  temp\search\b2.log:17:17 Nothing wrong at this time
  temp\search\b2.log:18:18 Nothing wrong at this time

进入子目录是一项练习。它在PowerShell中非常简单。查看Get-ChildItem的文档。

的grep

某些Windows环境安装了某个版本的grep工具,并且大多数现代版本的grep都有一种控制打印匹配后的上下文量的方法。在我们的示例中,我们使用类似以下内容而不是FINDSTR命令。

grep --with-filename --after-context=10 --ignore-case error "%logfiles%" > "%file%_results.log"
相关问题