Page 1 of 1
bat script to find file name in a file
Posted: 17 Aug 2010 23:01
by agrawal.meet
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...
Re: bat script to find file name in a file
Posted: 18 Aug 2010 08:26
by aGerman
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
Re: bat script to find file name in a file
Posted: 18 Aug 2010 08:28
by ghostmachine4
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.
Re: bat script to find file name in a file
Posted: 18 Aug 2010 10:51
by aGerman
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
Re: bat script to find file name in a file
Posted: 18 Aug 2010 22:41
by ghostmachine4
if its between anchor tags then the most probable separator to use is </a> (case insensitive)
Re: bat script to find file name in a file
Posted: 20 Aug 2010 01:10
by agrawal.meet
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?
Re: bat script to find file name in a file
Posted: 20 Aug 2010 01:43
by ghostmachine4
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.
Re: bat script to find file name in a file
Posted: 20 Aug 2010 05:39
by aGerman
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
Re: bat script to find file name in a file
Posted: 23 Aug 2010 02:01
by agrawal.meet
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.
Re: bat script to find file name in a file
Posted: 24 Aug 2010 10:59
by aGerman
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
Re: bat script to find file name in a file
Posted: 24 Aug 2010 11:25
by orange_batch
What's happening in China, aGerman?