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

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
Duke001Asked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
 
TvMptCommented:
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
 
Duke001Author Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Duke001Author Commented:
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
 
TvMptCommented:
You want to the folder as parameter?
0
 
Duke001Author Commented:
Basically I want to be able to indicate where the the htm files are.
0
 
TvMptCommented:
@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
 
Bill PrewCommented:
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
 
Duke001Author Commented:
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
 
TvMptCommented:
http://www.sttmedia.com/textencoder-batch-version

or you can use the notepad++.

 Notepad++ > Encoding > Convert to UTF-8 without BOM
0
 
Duke001Author Commented:
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
 
Bill PrewCommented:
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
 
Duke001Author Commented:
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
 
Bill PrewCommented:
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
 
Duke001Author Commented:
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
 
Bill PrewCommented:
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
 
Duke001Author Commented:
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
 
Bill PrewCommented:
Yes.  Just make sure you test fully in case you have files that are different than the one you showed me.

~bp
0
 
Duke001Author Commented:
ok, thanks again.
have a nice weekend.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.