Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract filenames and open file 2

Status
Not open for further replies.

optjco

IS-IT--Management
Apr 13, 2004
185
GB
I wonder if someone can help me I have the code shown below which extracts the file names from a certain folder all the file names refer to PDF's. What I am trying to do is to list the file names and then then click on any one of them to open it.

Any help would be appreciated

Code:
<%
  'Create FSO Object
  Dim objFSO
  Set objFSO = CreateObject("Scripting.FileSystemObject")


  Dim objFolder
  Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

  Dim Files
  For Each File in objFolder.Files
    Response.Write(File.Name & "<br />")
  Next
%> 
[/code

Regards

Olly
 
<%
'Create FSO Object
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")


Dim objFolder
Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

Dim Files
For Each File in objFolder.Files
Response.Write("<a href=""../../pathtofile/kiwi-cag/production/CAF/<%=File.Name%>"">" & File.Name & "</a>" & "<br />")
Next
%>
Inserting the relevant path to the folder in the href tag.

Hope this helps

Nick (Webmaster)

info@npfx.com
 
Nick,
I am getting the following error message

Error Type:
Microsoft VBScript compilation (0x800A0409)
Unterminated string constant
/folderlist.asp, line 12, column 79



now I have changed the code to this

Code:
<%
  'Create our FSO Object
  Dim objFSO
  Set objFSO = CreateObject("Scripting.FileSystemObject")

  'Stuff our folder into an object we can manipulate
  Dim objFolder
  Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

  Dim Files
  For Each File in objFolder.Files
     Response.Write("<a href=""../../uktmwsa001/kiwi-cag/production/CAG/<%=File.Name%>"">" & File.Name & "</a>" & "<br />")

  Next
%>

Regards

Olly
 
Just had another thought:

Unless the folder containing the pdfs is within your website folder, just linking to the files probably won't work. I had this problem when trying to write a page which downloaded files from another server on the network.

It is possible to do using the ADODB.Stream object as this allows you to open the file using the server.mappath function and send its binary contents straight to the browser.

Let me know whether the first approach works if not you may consider trying the other.



Nick (Webmaster)

info@npfx.com
 
if you just add a virtual folder to your IIS pointing to the pdf folder you can map across network ties or outside your web scope. just replace the pathing in the link to reflect that of the virtual folder. and you can apply seperate security rules to the virtual folder if necessary

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Nick,
Thanks for your help. Still not working at the moment but I am going to keep trying

[2thumbsup]

Regards

Olly
 
is : D:\kiwi-cag\production\CAG part of your web scope?
if so, what's the virtual path to the folder from your root, if not, could you add it as a virtual folder then post the virtual path to it.

with the proper virtual location of the files, we could assist better in getting you a code snippet that would be functional in your situation.

thanks


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Guys,
OK got it to work now using the code below

Code:
<%
  'Create our FSO Object
  Dim objFSO
  Set objFSO = CreateObject("Scripting.FileSystemObject")

  'Stuff our folder into an object we can manipulate
  Dim objFolder
  Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

  Dim Files
  For Each File in objFolder.Files
     Response.Write("<a href=""\\uktmwsa001\kiwi-cag\production\CAG\" &  File.Name & """>" & File.Name & "</a>" & "<br />")



  Next
%>

Thanks for your help

"Robert, could you explain what you mean when you talk about virual folder ??"

Regards

Olly
 
Now I have this working my next step is to filter the data somehow as the folder can contain upto 2,000 records, all of the pdf files are named using the following convention

ABC_A12345.pdf
XYZ_Z98765.pdf

etc

Is it possible for me to create a search page where i can search with say ABC_* or something like that





Regards

Olly
 
a virtual folder is something outside your web scope or a shortcut to something in it... examples :
root - C:\inetpub\PDF location - c:\Clients\PDF\

pdf location can be mapped via IIS as an aliased folder name like : /PDF

so it can be accessed as
or if the folder is nested deep somewhere like :
/products/dataheets/manuals/guides/programming/pdf

can be remapped to /ProgGuides so that you dont need this long url to access it.

my concern that you might have been havin a web scope issue as in :
root - c:\inetpub\pdf location - g:\clientdata\datasheets\2004\AUG\pdf

and not being able to make a link straight to the files without a virtual folder.

virutal folders can be added by right-clicking in IIS on the folder location ( virtual or not ) that you wish it to be a sub location/folder, selecting New--> virtual location, a config wizard will appear afterwards.


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
simplest form of filter via search using code already posted:

<%
SearchValue = Request("SearchValue")
'Create our FSO Object
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Stuff our folder into an object we can manipulate
Dim objFolder
Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

Dim Files
For Each File in objFolder.Files
If Instr(File.Name,SearchValue,vbtextcompare) >= 1 Then
Response.Write("<a href=""\\uktmwsa001\kiwi-cag\production\CAG\" & File.Name & """>" & File.Name & "</a>" & "<br />")
End If
Next
%>

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Robert,
I think i am being a bit thick here but !!

I have copied the code from your last post but cannot work out how i use it. Do I need a search page that sends the "search Value" to my results page. If so do i need a form with a textbox in it and then submit to the results page. also as I said in an earlier thread all of our PDF file names are in the convention of

AAA_A12345.pdf

we may have say 50 pdf's that start PUR_
also perhaps 35 pdf's that start BIR_

what I am trying to achieve is that if the user enters say PUR into the search value then the results page will list all 50 PDF's

I hope I have not confused you too much and i would like to thank both yourself and HowardMarks for the assistance you have given me so far

Regards

Olly
 
yup, you have the right idea, actually exactly the right idea.. you'd need a simple form just a text box and a submit button really, you could also add a hidden field of "where" to search like the virtual path for /pdf or whatever.

you'd then request the value of the textbox element and use it in the aforementioned code to display, if there's no search element should return no results, to display a "no matches" in that event, modify the code as such :

Code:
<%
SearchValue  = Request("SearchValue")
MatchesFound = False ' pessimistic boolean for match finds
  'Create our FSO Object
  Dim objFSO
  Set objFSO = CreateObject("Scripting.FileSystemObject")

  'Stuff our folder into an object we can manipulate
  Dim objFolder
  Set objFolder = objFSO.GetFolder("D:\kiwi-cag\production\CAG")

  Dim Files
  For Each File in objFolder.Files
     If Instr(File.Name,SearchValue,vbtextcompare) >= 1 Then
       Response.Write("<a href=""\\uktmwsa001\kiwi-cag\production\CAG\" &  File.Name & """>" & File.Name & "</a>" & "<br />")
       MatchesFound = True ' something was matched.
     End If
  Next
If Not MatchesFound Then
Response.Write "No Matches Found"
End If
%>

i also recommend making the search form an include, that way you can include it in the "no matches" part and also elsewhere without having to repeat code


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
This might also help with your problem:

You could filter the records based on their filenames using the right() and left() functions.

For example

If Left(File.Name,3) = "ABC" Then
Response.Write("<a href=""\\uktmwsa001\kiwi-cag\production\CAG\" & File.Name & """>" & File.Name & "</a>" & "<br />")
End If

Equally, If Right(File.Name,3) = "pdf" could be used to determine which files are actually PDFs and which are other file types.

Hope this is of some use



Nick (Webmaster)

info@npfx.com
 
Robert,
When I use the code I get a an error as shown below

Code:
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "A1P_A48974.PDF"]'
/transferlog/FolderListCAG1.asp, line 15

The search string I tried was "PUR" the string string: "A1P_A48974.PDF"]

is actually the first filename in the list in the folder
Any Ideas ???






Regards

Olly
 
oops.. was thinking sideways, just change the InStr to StrComp, arguments are the same , and the equation should be >=0

modified line :

If StrComp(File.Name,SearchValue,vbtextcompare) >= 0 Then

PS if it throws an error, the const for vbtextcompare might not be working properly ( seen it happen ) change it out for :

If StrComp(File.Name,SearchValue,1) >= 0 Then


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Robert,
firstly I really appreciate all your help

It works sort of if i put in my search the example i used earlier "PUR" the it goes away and the list starts at the "PUR" files but continues right through to the end of the files in the folder.

i.e first file listed is PUR_P2340.pdf
last file listed is WOR_W71986.pdf


How can i restrict just to the search criteria ??

Regards

Olly
 
that's odd .. shouldn't be validating for file names not containing the vlaue of searchstr. per chance is the page cached?

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
you can also try flipping it back to instr :

If Instr(1,File.Name,SearchValue,vbtextcompare) >= 1 Then

Or

If Instr(1,File.Name,SearchValue,1) >= 1 Then


it's the same thing, just without the const variables


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top