Solved

remove duplicate line breaks from a txt file using batch file

Posted on 2014-09-30
4
793 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:NeilT
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40351657
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 200 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 68

Accepted Solution

by:
Qlemo earned 300 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:NeilT
ID: 40356808
Many thanks All,
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

10 Experts available now in Live!

Get 1:1 Help Now