[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-06-20
19
Medium Priority
?
376 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
ID: 39262455
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
ID: 39262524
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
ID: 39262624
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:TvMpt
ID: 39262654
You want to the folder as parameter?
0
 

Author Comment

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

Expert Comment

by:TvMpt
ID: 39262839
@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 59

Expert Comment

by:Bill Prew
ID: 39263047
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
ID: 39265488
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
ID: 39265583
http://www.sttmedia.com/textencoder-batch-version

or you can use the notepad++.

 Notepad++ > Encoding > Convert to UTF-8 without BOM
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39265590
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
ID: 39265688
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 59

Expert Comment

by:Bill Prew
ID: 39265733
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
ID: 39265852
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 59

Expert Comment

by:Bill Prew
ID: 39265966
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
ID: 39266083
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 59

Expert Comment

by:Bill Prew
ID: 39266312
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
ID: 39266424
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 59

Expert Comment

by:Bill Prew
ID: 39266442
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
ID: 39266454
ok, thanks again.
have a nice weekend.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
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…

830 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