Link to home
Start Free TrialLog in
Avatar of Rajat Sehgal
Rajat SehgalFlag for India

asked on

Batch Scripts

Hello Expert's,
Need a batch scripts:-

We have an application which is generated a log file in txt format (d:/OF Top/logs.txt), once application generate new log that happen added to that txt file. It happens so often, txt file have info in the term's of user identity number.
e. g.
11345, TOP, BLACK, TOPIC11, 110067145, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, TOPIC81, 110067155, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, LOGIC00, 110067145, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, MAGIC99, 110067105, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, TOPIC89, 110067110, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, ARPIT67, 110067100, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, MANIS56, 110067110, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, WHITE00, 110067145, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, ACERP23, 110067155, DBEN, EXCEL, 11:33:54, LOCK
11345, TOP, BLACK, TOPIC89, 110067105, DBEN, EXCEL, 11:33:54, LOCK

Column number 5 have user Identification numbers.

There are many such lines store in this file.

Now i want a batch when I run the batch file then batch ask me to place user identity number to grep that particular user identity number and generate a txt file in a prefix location (c:/out. txt)

after that once new entry came to this file it will automatically grep that entry & store (add) in to out.txt file.
Avatar of ste5an
ste5an
Flag of Germany image

Use grep or PowerShell or you start looking at for /f "delims=" %%a in (file.csv) do set . E.g. in PowerShell

Import-Csv -Path 'C:\Temp\test.csv' -Header Num1, Position, Color, Topic, UserID, WhatEver, Application, Time, State |
    Where-Object { $_.UserID -eq 110067110 } |
    Export-Csv -Path 'C:\Temp\test2.csv' -NoTypeInformation

Open in new window

Avatar of Rajat Sehgal

ASKER

I am not familiar with power shell, that why need batch solution
Avatar of Bill Prew
Bill Prew

You might take a look at the FINDSTR command, as far as finding all records for an Identification number you could do:

findstr /r /c:"[, ]110067145[, ]" "d:/OF Top/logs.txt"

and to save to a file just do:

findstr /r /c:"[, ]110067145[, ]" "d:/OF Top/logs.txt" >> "C:\Temp\out.txt"

as far as monitoring the file for additions at the bottom and then finding those that is quite a bit beyond what would be easily done in a BAT script.  You would probably want to use Task Scheduler to run the script, but then it has to know what the file looked like the last time it ran to compare that to what it looks like now, identify all the added records at the end of the current file, and loop over them pulling all the new Identification numbers and then use FINDSTR as mentioned to add those records to the output file.  That would be quite a challenge in a BAT file...


»bp
From this script i can filter & store data in a file & through loop i can check & store repeatedly

@echo off
:loop
findstr "110067145 110067155 "  d:\OD Top\logs.txt > c:/out.txt
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
Pause

but this script check it out from the beginning then store new line in to that file, this method is wrong.
The problem with that approach (once the bugs are fixed in it) is it will constantly find the same records each time it loops, and add the same duplicate info to the output file.  I doubt that is what you want.


»bp
hmm, as batch..

PowerShell.exe Import-Csv -Path 'C:\Temp\test.csv' -Header Num1, Position, Color, Topic, UserID, WhatEver, Application, Time, State | Where-Object { $_.UserID -eq 110067110 } | Export-Csv -Path 'C:\Temp\test2.csv' -NoTypeInformation

Open in new window

Sir,
Need to copy data as per user request & store into a new file, my method is wrong which i am trying .
Here's a few adjustments to the BAT script you proposed that get it closer to what you described.  You needed to set the number of times to loop, and I added a 5 second delay between checks.  Also enclosed the file path being searched in double quotes since it has spaces in it.

@echo off
setlocal

set loopcount=10

:loop
findstr "110067145 110067155 " "d:\OD Top\logs.txt" >> "c:/out.txt"
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
timeout /t 5 >NUL
goto loop

:exitloop
pause

Open in new window

You could also do this a little simpler using a FOR /L loop rather than GOTO's.

@echo off
setlocal

for /l %%i in (10,-1,0) do (
    findstr "110067145 110067155 " "d:\OD Top\logs.txt" >> "c:/out.txt"
    timeout /t 5 >NUL
)

pause

Open in new window


»bp
Sir,
I'm able to store data through using FOR/L loop, but i dont want put user identity number inside the batch script, once i start that script it should ask to put user identity number in CMD console.
Okay, try this.

@echo off
setlocal EnableDelayedExpansion

rem Loop ten times
for /l %%i in (10,-1,0) do (

    rem Prompt for identity number
    set RefNum=
    set /P "IdNum=Enter identitye number (blank to exit):"

    rem If none entered, exit script
    if "%IdNum%" EQU "" exit /b

    rem Look for id number records and add to output file
    findstr "!IdNum!" "d:\OD Top\logs.txt" >> "c:/out.txt"

    rem delay 5 seconds during looping steps
    timeout /t 5 >NUL
)

pause

Open in new window


»bp
once i start with entering identification number script goes close.
Noticed one bug, try this adjustment.

@echo off
setlocal EnableDelayedExpansion

rem Loop ten times
for /l %%i in (100,-1,0) do (

    rem Prompt for identity number
    set RefNum=
    set /P "IdNum=Enter identitye number (blank to exit):"

    rem If none entered, exit script
    if "!IdNum!" EQU "" exit /b

    rem Look for id number records and add to output file
    findstr "!IdNum!" "d:\OD Top\logs.txt" >> "c:/out.txt"

    rem delay 5 seconds during looping steps
    timeout /t 5 >NUL
)

pause

Open in new window


»bp
Hello,

Sorry for the delay in replay

Filter is working, but why it goes to stop after filtering identification number actually anytime data (identification number) will entered into this file d:\OD Top\logs.txt from our main application, it should be alive till closed by user. Once new entry added to d:\OD Top\logs.txt  batch script will add that entry (if match with identification number) in to c:\out.txt

Can we put multiple identify number inside the batch which I want to filter instead of entered manually ?
Give this a try, adjust the SearchList variable to include the items you want to look for.

@echo off
setlocal EnableDelayedExpansion

rem Define all items to search for in FINDSTR format
set SearchList=/C:"item1" /C:"item2" /C:"item3"

:MainLoop

rem Look for matching records and add to output file
findstr /i %SearchList% "d:\OD Top\logs.txt" >> "c:/out.txt"

rem delay 5 seconds during looping steps
timeout /t 5 >NUL

goto :MainLoop

Open in new window


»bp
file created but getting duplicate entry, it should be check for that row already in the out.txt file that should be not repeated...
Give this a try.

@echo off
setlocal EnableDelayedExpansion

rem Define all items to search for in FINDSTR format
set SearchFile=d:\OD Top\logs.txt
set SearchList=/C:"110067105" /C:"110067145"
set OutputFile=c:/out.txt

:MainLoop

rem Look for matching records and add to output file (only if they don't already exist)
for /f "tokens=*" %%L in ('findstr /i %SearchList% "%SearchFile%"') do (
    findstr /C:"%%~L" "%OutputFile%" >NUL 2>&1 || (
        echo.%%~L>>"%OutputFile%"
    )
)

rem delay 5 seconds during looping steps
timeout /t 5 >NUL

goto :MainLoop

Open in new window


»bp
Hi,
Now it working properly like i wanted, thanks for the same.

I am trying to make some little bit changes like administrator or user name Should not be print on title bar & need to configure expiry date for the batch script . i tried some from google but it unable to work.


@echo off
mode 10, 10
title                             ~~~Consolidate~~~
(SET OUT=1F)

color %OUT%

for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"

set "LocalDateTime=%LocalDateTime:~0,8%"

::echo %LocalDateTime%

:: EXPIRATION DATE ::
set "EXP_DATE=20190216"
:::::::::::::::::::::


if %LocalDateTime% GTR %EXP_DATE% (
    echo this wont work anymore
    exit /b
)

setlocal EnableDelayedExpansion
rem Define all items to search for in FINDSTR format
set SearchFile=d:\OD Top\logs.txt
set SearchList="27380 27282 27310 27278"
set OutputFile=c:/out.txt

:MainLoop

rem Look for matching records and add to output file (only if they don't already exist)
for /f "tokens=*" %%L in ('findstr /i %SearchList% "%SearchFile%"') do (
    findstr /C:"%%~L" "%OutputFile%" >NUL 2>&1 || (
        echo.%%~L>>"%OutputFile%"
    )
)

rem delay 5 seconds during looping steps
timeout /t 5 >NUL

goto :MainLoop
Why don't you explain to me the details of what you want to change / add and I will propose an approach.


»bp
This is the last & final, sorry for the inconveniences.
That's fine, but it wasn't clear to me what you wanted.


»bp
I need only title which i store in batch script but I'm getting a computer user name with title, that should not be print.
User generated image
I want set a date under this batch script, batch should stop work after that date.
User generated image
This should handle the date check properly, stopping after the ExpDate value.

Given that you are running this in Administrator mode there is no way to remove the "Administrator": in the window title bar.

@echo off
setlocal EnableDelayedExpansion

title                             ~~~Consolidate~~~

rem Define all items to search for in FINDSTR format
set SearchFile=d:\OD Top\logs.txt
set SearchList=/C:"110067105" /C:"110067145"
set OutputFile=c:/out.txt
set ExpDate=20190216

:MainLoop

REM Get current date in YYYYMMDD format
set LocalDateTime=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined LocalDateTime (
        set LocalDateTime=%%A
    )
)
set LocalDateTime=%LocalDateTime:~0,8%

if "%LocalDateTime%"" GTR "%ExpDate%"" (
    echo this wont work anymore
    exit /b 
)

rem Look for matching records and add to output file (only if they don't already exist)
for /f "tokens=*" %%L in ('findstr /i %SearchList% "%SearchFile%"') do (
    findstr /C:"%%~L" "%OutputFile%" >NUL 2>&1 || (
        echo.%%~L>>"%OutputFile%"
    )
)

rem delay 5 seconds during looping steps
timeout /t 5 >NUL

goto :MainLoop

Open in new window


»bp
Now not working... when i try to run, it off
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You Mr. Bill