bat script to find file name in a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

bat script to find file name in a file

#1 Post by agrawal.meet » 17 Aug 2010 23:01

Hello All,

It's been a while since my last question. I have 1 now so here it goes:

I want to write a bat file that will loop through a folder (and also sub-folders in it) and read each file. File can be of any type..mostly ASP and html. It should read all lines and find anything which ends with .asp or .html.

e.g.: I have 1 html file and within it i am calling 2 html files in anchor tag (<a>2.html</a><a>3.html</a>), so the bat file should list down 2.html and 3.html.

Also,
Should I use bat script to code this at all??

Let me know if anything is not clear...

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: bat script to find file name in a file

#2 Post by aGerman » 18 Aug 2010 08:26

IMO never a good idea to parse html source text by batch.
When I'm at home I will try to write it in VBScript.
Is it a rule that the file names are separated by > and < ?

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: bat script to find file name in a file

#3 Post by ghostmachine4 » 18 Aug 2010 08:28

aGerman wrote:IMO never a good idea to parse html source text by batch.
When I'm at home I will try to write it in VBScript.
Is it a rule that the file names are separated by > and < ?

Regards
aGerman


even vbscript is not the correct tool to parse html. HTML parsers is the way to go.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: bat script to find file name in a file

#4 Post by aGerman » 18 Aug 2010 10:51

Of course, but I thought about splitting the entire file content to get an array where we could find the file names by looping over the elements.
But first I will wait for response, what exactly I could use as separator.

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: bat script to find file name in a file

#5 Post by ghostmachine4 » 18 Aug 2010 22:41

if its between anchor tags then the most probable separator to use is </a> (case insensitive)

agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

Re: bat script to find file name in a file

#6 Post by agrawal.meet » 20 Aug 2010 01:10

ok...we generally include file names in anchor tags...but there are other ways of doing it also apart from anchor tags..like <# include...>

Cant we search for .asp in a line and then take out the filename?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: bat script to find file name in a file

#7 Post by ghostmachine4 » 20 Aug 2010 01:43

so why don't you show a sample of your file? I can't answer you based on what you ask since we didn't see your input file. Next time when you ask questions, provide enough input and clearly describe the output you want so that we don't waste each other's time posting back and forth.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: bat script to find file name in a file

#8 Post by aGerman » 20 Aug 2010 05:39

agrawal.meet wrote:Cant we search for .asp in a line and then take out the filename?


No, because I don't know how to separate it from the source text.
You could return the entire line, but as you should know it's possible that there are more than one file name in a line. Further more it is possible to write the whole source text of a HTML file to a single line. Probably too large to process it by batch.

Regards
aGerman

agrawal.meet
Posts: 15
Joined: 14 Jun 2010 06:50

Re: bat script to find file name in a file

#9 Post by agrawal.meet » 23 Aug 2010 02:01

my bad...here is the code from sample file called a.asp

Code: Select all

<html>
<!-- #include virtual="/CAT/inc\check_site_avail.asp" -->
<!-- #include virtual="/CAT/inc\CTranslate.asp" -->
<!-- #include virtual="/CAT/PerfTst/PerfLogCookies.asp"-->
</html>


How should I go ahead. I want output as...check_site_avail.asp, CTranslate.asp, PerfLogCookies.asp

Also,
If a single line has reference to 2 or more asp files..they should be treated as same.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: bat script to find file name in a file

#10 Post by aGerman » 24 Aug 2010 10:59

Sorry guys, I'm currently in China and have less time.

@agrawal.meet
This small VBScript would probably work for files like your example

Code: Select all

' change it:
sRootFolder = "C:\whereever"

Sub ProcFile(sFileName)
  Set oFile = oFS.OpenTextFile(sFileName)
  sArrContent = Split(oFile.ReadAll, """")
  oFile.Close
  For Each sElement In sArrContent
    If LCase(Right(sElement, 4)) = ".asp" Then
      Redim Preserve sArrFoundNames(i)
      sPart = Right(sElement, Len(sElement) - InStrRev(sElement, "\"))
      sArrFoundNames(i) = Right(sPart, Len(sPart) - InStrRev(sPart, "/"))
      i = i + 1
    End If
  Next
End Sub

Sub procFolder(sFolderName)
  Set oFolder = oFS.GetFolder(sFolderName)
  Set colFiles = oFolder.Files
  For Each oFileItem In colFiles
    If LCase(oFS.GetExtensionName(oFileItem)) = "html" Then
      ProcFile oFS.GetParentFolderName(oFileItem) & "\" & oFileItem.Name
    End If
  Next
  Set colFolders = oFolder.SubFolders
  For Each oFolderItem In colFolders
    procFolder oFS.GetAbsolutePathName(oFolderItem)
  Next
End Sub

Dim sArrFoundNames(), i
Set oFS = CreateObject("Scripting.FileSystemObject")
procFolder sRootFolder
Set oOutFile = oFS.CreateTextFile("output.txt", True)
For Each sItem In sArrFoundNames
  oOutFile.WriteLine(sItem)
Next
oOutFile.Close
WScript.Echo "ready"


Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: bat script to find file name in a file

#11 Post by orange_batch » 24 Aug 2010 11:25

What's happening in China, aGerman? 8)

Post Reply