Windows Batch script to get filename as input and search the file in a folder location -


i want create windows batch file includes 3 tasks mainly:

1.it ask user input like:

please enter file name search: 

where have give file name , depending on file name search in specified location.

2.if file found ,again search pattern(e.g-"error") in file.

3.copy 10 lines line pattern found , paste contents file.

example:

you have entered file name as: demofile 1.it search demofile.log in specified location.

2.if above file found search pattern(e.g-"error").

3.suppoese pattern error found in line #20 ,then copy contents line #20 line #30 file(mydemofile.txt).

it seems quite complex. anyhow need badly.

thanks in advance :)

below code snippet in prompts user file name.

@echo off   *** prompting user enter id***** set /p uname=please enter conversation id:  if "%uname%"=="" goto error echo entered conversation id : %uname%, below log files related id:  /f "tokens=* delims=" %%x in (a.txt) echo %%x  *** finding files based on conversation id**** set listfolder=c:\users\demo\desktop\stats\test\a.txt -- passing parameter conversation id set filespath=\\test\*.log set destpath=c:\users\demo\desktop\stats\test\check  /f "tokens=*" %%i in ('dir /b ^"%listfolder%\*.txt^"') (call :copy_files "%listfolder%\%%i")  pause exit 

basics

heads up. i'm not going solve pure batch , built-in windows tools. (debatable, actually. read on!) i'm going assume environment isn't 100% locked down , can use better tool part 3.

based on question, sample code, , comments, want prompt subpattern of file name , search through files in 1 directory match pattern.

batch

here's way prompt for, check whether files exist in given directory, and—if so—find lines match. (parts 1 , 2)

@echo off setlocal  set /p file=please enter thing want 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 "%logfiles%" found.     exit /b 1 ) 

going subdirectories left exercise.

part 3, getting 10 lines after match trickier. may possible i'd call "silly batch tricks". can't come them now. suggestions , edits welcome.

however, personally, solve problem different tool, assuming could. i'm going assume can.

powershell

powershell installed default on many configurations of windows starting windows 7/windows server 2008r2. it's select-string cmdlet has -context parameter can control how many lines of context included. cmdlet following skeleton i'd start with. i'm intentionally not writing output file in cmdlet able take advantage of powershell's pipeline later. decision can change.

function get-errorfromlog {   <#   .synopsis   search through logs match , extract error lines , context   .example   get-errorfromlog -log server   .parameter log   base name of log search through. .log automatically appended , automatically search in searchbase   .parameter searchbase   directory in logs   #>   [cmdletbinding()]   param   (     [parameter(mandatory=$true,     valuefrompipeline=$true,     valuefrompipelinebypropertyname=$true,     helpmessage='what log 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"   } } 

some example output

ps d:\> get-errorfromlog -log does-not-exist get-errorfromlog : no items matchings "does-not-exist*.log" exist in some\path @ 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 @ 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, has no errors  ps d:\> get-errorfromlog -log -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 @ time   temp\search\b.log:13:13 nothing wrong @ time   temp\search\b.log:14:14 nothing wrong @ time   temp\search\b.log:15:15 nothing wrong @ time   temp\search\b.log:16:16 nothing wrong @ time   temp\search\b.log:17:17 nothing wrong @ time   temp\search\b.log:18:18 nothing wrong @ 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 @ time   temp\search\b2.log:13:13 nothing wrong @ time   temp\search\b2.log:14:14 nothing wrong @ time   temp\search\b2.log:15:15 nothing wrong @ time   temp\search\b2.log:16:16 nothing wrong @ time   temp\search\b2.log:17:17 nothing wrong @ time   temp\search\b2.log:18:18 nothing wrong @ time 

going subdirectories left exercise. it's easy in powershell. check documentation get-childitem.

grep

some windows environments have version of grep tool installed, , modern version of grep have way of controlling amount of context after match print. in our example, we'd use following instead of findstr command.

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

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -