Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separation of duties 5 52
deducing meaning of a word from its prefix-question 7 69
Need a cheap RFID setup 10 66
telephony Question - Can anyone recommend a solution? 1 66
Originally published Entrepreneur.com Booming numbers of freelancing professionals are changing the face of work. In the United States alone last year, the number of workers freelancing grew from 700,000 to 54 million, according to a Freelancers’…
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…
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…
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.

929 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

18 Experts available now in Live!

Get 1:1 Help Now