Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

remove duplicate line breaks from a txt file using batch file

Posted on 2014-09-30
4
Medium Priority
?
886 Views
Last Modified: 2014-10-02
Hi

My PHP error log files are renamed on a daily basis but they are also full of duplicate carriage returns which I'm currently removing using regex in textpad replacing \n\n with \n

This is my batch file that renames them each day at midnight, how can I incorporate something in it to remove the extra line breaks before saving please?

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "php-errors.log" %%d-%%e-%%f.txt

Open in new window

0
Comment
Question by:Neil Thompson
[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 15

Expert Comment

by:Haris Djulic
ID: 40351657
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 800 total points
ID: 40351671
Based on the solution on the link http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_23027665.html

here is the code which does requested.

@echo off
set source=C:\30092014
set dest=C:\30092014
for /f "tokens=*" %%a in ('dir /b /a-d "%source%\php-errors.log"') do call :process %%a
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "tmp_php-errors.log" %%d-%%e-%%f.txt

goto :eof
 
:process
set filename=%*
echo Working on %filename%
type "%source%\%filename%" > "%source%\tmp_%filename%"
findstr /B /I /R "[!-z]" "%source%\tmp_%filename%" > "%dest%\%filename%"

Open in new window

0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1200 total points
ID: 40351912
Using findstr is a neat trick found by Steve (dragon-it), but we can simplify the above code as we are certain there is only one file - no loop needed here.
@echo off
type php-errors.log | findstr /B /I /R "[!-z]" > %date:~0,2%-%date:~3,2%-%date:~6,4%.txt

Open in new window

or, if you prefer the original date string construction
@echo off
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do set newname=%%d-%%e-%%f.txt
type php-errors.log | findstr /B /I /R "[!-z]" > %newname%.txt

Open in new window

0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 40356808
Many thanks All,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

688 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