Solved

Batch file to delete sequential lines of text in a text file

Posted on 2012-03-15
4
147 Views
Last Modified: 2012-03-15
Thanks for any help that you can give me. I need a batch file that can delete lines from a text file, each line begins with a time, and I need to delete everything between 5:00am and 11:00am, so that I can automate daily editing of the file. Please be gentle with me, as I am new to batch files;)

Text File is attached for example of what I need to edit



As you can see, the lines begin with a time, so I don't think it would be too hard, but I need it to take everything between 5am and 11am out and then save the text file without those hours.

Thanks again!
120308.txt
0
Comment
Question by:rodthetripod
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 37726255
Try this; you can pass the file to filter either directly in the command line, drag and drop a file in Explorer on the script, or you'll be queried.
The script will generate a file with the same name as the input file, but "-filtered" attached.
You've noticed that there are "times" like "00:77:07"?
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
  echo Please pass the name of the file to filter as command line argument,
  echo or "drag and drop" the file onto the script or this window, or enter the file name:
  set /p InFile=^>
) else (
  set InFile=%~1
)
if not exist "%InFile%" (
  echo File not found: %InFile%
  pause
  goto :eof
)
set OutFile=%~dpn0-filtered.txt
if exist "%OutFile%" del "%OutFile%"
for /f "delims=" %%a in ('type "%InFile%"') do (
  set Print=0
  set Time=%%a
  set /a Hour=1!Time:~1,2!
  if "!Hour!" LSS "105" set Print=1
  if "!Hour!" GTR "111" set Print=1
  if !Print!==1 (
    echo %%a
    >>"%OutFile%" echo %%a
  )
)
echo Done.
pause
GOTO :EOF

Open in new window

0
 

Author Closing Comment

by:rodthetripod
ID: 37726422
You are a batch file GOD! Works absolutely perfectly! THANK YOU!
I am aware of the hours that are more than 60 minutes ex: "4:75", but those are handled by the automation software, so no worries there.

Once again thanks, I really appreciate it.

Greg
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37726438
You have your answer I can see, but a possibly simpler way programming wise is just to use the FINDSTR command .... I had typed this earlier but called away:

This takes the file 120308.txt and excludes all lines that start with

. = any char to match the "
0 = to match the 0
[5-9] = to match 5,6,7,8,9
: to match the colon.

and then runs an exclusion for lines that start "10:" and sends the output into a file called output.txt

findstr /V /B /R /C:".0[5-9]:" 120308.txt | findstr /V /B /R /C:".10:" > output.txt

I was trying to think of a better way to exclude the 10 using the same FINDSTR but failed!

Steve
0
 

Author Comment

by:rodthetripod
ID: 37726557
Thanks Steve, I will try that out too....nothing wrong with simplifying things....right?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complete list of Microsoft tech support phone numbers 5 104
URL to download QuickBooks Pro 2013 2 48
Work with App store 7 51
English question 2 14
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question