Solved

batch to iterate in delete first blank line of each htm file

Posted on 2013-06-20
19
360 Views
Last Modified: 2013-06-21
Hi Experts,

I need to iterate over all files (htm) in a folder and get rid of the first line which is blank.

Thanks,
Duke001
0
Comment
Question by:Duke001
  • 9
  • 6
  • 4
19 Comments
 
LVL 9

Expert Comment

by:TvMpt
Comment Utility
This should work in batch file, just add this code into .bat file)
@echo off
for %%x in (*.html) do (
    more +1 "%%x" >tmp
    move /y tmp "%%x"
)

Open in new window

0
 

Author Comment

by:Duke001
Comment Utility
Hi TvMpt

At the first glance it seems to work perfectly. Please let me do some further checks and I will let you know in the next hour or so and assign the points to you.

Many thanks,
Duke001
0
 

Author Comment

by:Duke001
Comment Utility
I am sorry to push my luck but would it be possible to modify the code in order to give it more flexibility and run it from anywhere (ex. set BaseDir= )

Many thanks
Duke001
0
 
LVL 9

Expert Comment

by:TvMpt
Comment Utility
You want to the folder as parameter?
0
 

Author Comment

by:Duke001
Comment Utility
Basically I want to be able to indicate where the the htm files are.
0
 
LVL 9

Expert Comment

by:TvMpt
Comment Utility
@echo off
for %%x in (%1*.html) do (
    more +1 "%%x" >tmp
    move /y tmp "%%x"
)

Open in new window


Run it like ->   script.bat c:\folder\
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
I need to iterate over all files (htm) in a folder and get rid of the first line which is blank.
Is it line #1 you always want to remove, or the first line that is blank (meaning some non-blank lines could come before it)?

~bp
0
 

Author Comment

by:Duke001
Comment Utility
I am attaching a sample.htm file which shows exactly what I am looking for.
The first line of the file which is blank followed by ".## blablabla" as seen in the example, must be wiped.

Thanks

PS: do you know a method to convert the Encoding from UTF in UTF-8 to UTF in UTF-8 without BOM

If yes please let me know and I will increase the points
Batch.zip
0
 
LVL 9

Expert Comment

by:TvMpt
Comment Utility
http://www.sttmedia.com/textencoder-batch-version

or you can use the notepad++.

 Notepad++ > Encoding > Convert to UTF-8 without BOM
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Give this VBS script a try.  Save as a VBS file, and adjust the path near the top to be the location of your files, then run from a command line as:

cscript EE28162695.vbs

Bear in mind it overwrites the files it changes, so backup before testing in case it doesn't do what you want.

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder and extension (lowercase!) to search
strBaseDir = "C:\EE\EE28162695\Files\"
strBaseExt = ".htm"

' Access the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Clear statistics counters
intFiles = 0
intMatched = 0
intChanged = 0
intSkipped = 0

' Process each file in the folder
For Each objFile in objFolder.Files

    intFiles = intFiles + 1

    ' See if it matches the pattern of filenames we want
    If LCase(Right(objFile.Name, Len(strBaseExt))) = strBaseExt Then

        intMatched = intMatched + 1

        ' Read the entire file
        strFile = objFile.Path
        Set objReader = objFSO.OpenTextFile(strFile, ForReading)
        strData = objReader.ReadAll
        objReader.Close

        ' Make desired replacements in the file
        If Left(strData, 2) = vbCrLf Then
           intChanged = intChanged + 1
           strData = Mid(strData, 6)

           ' Write the updated data back to the file
           Set objWriter = objFSO.OpenTextFile(strFile, ForWriting, True)
           objWriter.Write strData
           objWriter.Close
        Else
           intSkipped = intSkipped + 1
        End If
    End If
Next

Wscript.Echo "Files in folder: " & intFiles
Wscript.Echo "Files matching extension: " & intMatched
Wscript.Echo "Files updated: " & intChanged
Wscript.Echo "Files skipped: " & intSkipped

Open in new window

~bp
0
 

Author Comment

by:Duke001
Comment Utility
Hi,

Please see attached file which contains the error message when running (my OS is XP Pro SP3)

Is it possible to incorporate the batch command (software you recommended) into this vbs in order to do the encoding ?

Thanks,

PS: I did not forget to increase the points
VBS-ERROR.tif
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Can you determine which file it errored on?  Take a look at the files in the test folder and sort by last update and see if it worked on any?  I'd like to get a copy of the file it is having trouble with.

It should take care of the UTF BOM already.

~bp
0
 

Author Comment

by:Duke001
Comment Utility
Strangely I have tried to reproduce the same problem but everything seems to running smoothly now.
So, if you would kindly do the "Encoding" bit I would be grateful and of course (again) I will not forget to increase the points.

PS: I believe that I need to by the piece of software from "Stefan Trost Media", am I right?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
So, if you would kindly do the "Encoding" bit I would be grateful
It should already be doing this, let me know if it isn't.

~bp
0
 

Author Comment

by:Duke001
Comment Utility
You are completely right.
Because I didn't see the code where this encoding is takes action I've just assumed (wrongly!) that the encoding was not ready. Just to trying to understand, where is that encoding statement?

Many thanks for doing this.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
The MID() strips off the 4 byte BOM that you seemed to have (although it was after the first CR,LF pair, which seemed odd), and the OpenTextFile defaults to writing the file in ANSI mode.

~bp
0
 

Author Comment

by:Duke001
Comment Utility
Thanks for the explanation.
This means that wherever the htm files encoding are this "vbs" will do the businesses! Is this correct?

As promised I have increased the points up to 500
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Yes.  Just make sure you test fully in case you have files that are different than the one you showed me.

~bp
0
 

Author Comment

by:Duke001
Comment Utility
ok, thanks again.
have a nice weekend.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

7 Experts available now in Live!

Get 1:1 Help Now