Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-15
4
Medium Priority
?
152 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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
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…
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.
Suggested Courses

610 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