Solved

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

Posted on 2012-03-15
4
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

One of the biggest challenges facing freelancers is balancing multiple projects and deadlines. Organizational skills and time management are key to keeping up with projects and staying on track. Luckily, we’ve curated seven tools to help you focus o…
Whether you believe the “gig economy,” as it has been dubbed, is the next big economic paradigm shift (https://www.theguardian.com/commentisfree/2015/jul/26/will-we-get-by-gig-economy) or an overstated trend (http://www.wsj.com/articles/proof-of-a-g…
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…
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…

691 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