Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remove duplicate line breaks from a txt file using batch file

Posted on 2014-09-30
4
Medium Priority
?
896 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
  • 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

886 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