Solved

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

Posted on 2012-03-15
4
145 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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Steve, I will try that out too....nothing wrong with simplifying things....right?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Punctuation : Bulleted lists 2 35
Managing IT related issues 6 58
how to run a batch process 16 38
How do I delete 2 30
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
The Bounty Board allows you to request an article or video on any technical topic, or fulfill a bounty request to earn points. Watch this video to learn how to use the Bounty Board to get the content you want, earn points, and browse submitted bount…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now