Solved

remove duplicate line breaks from a txt file using batch file

Posted on 2014-09-30
4
808 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

21 Experts available now in Live!

Get 1:1 Help Now