ASP Forum
Find if a file exists in a folder
new_learner | Posted 4:41am 11. December 2006 Server Time |

Hii All,
   Need some help!! Would it be possible to check if the file is already present or not in a folder and accordingly create a link if it exists. This is what I am planning...
I upload the file from the asp page, the files gets stored in a folder. Then I want another page to check if a new file is present in the folder...if yes, then I want to create a hyperlink to the file, so that the user can open it immediately on uploading...i hope I am clear.

I was able to do it if the file is already in the folder..
this is the code...

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists("C:\abc\xyz.pdf"))=true Then
Response.Write("<a href='C:\abc\xyz.pdf')>xyz file</a>")
else
      Response.Write("File xyz does not exist.")
End If
set fs=nothing
%>
Any help would be appreciated..thank you.
katy8439 | Posted 5:52am 11. December 2006 Server Time |

You can't link to a file if it's outside of the webroot, and browsers won't recognise the URL C:\yourfolder\yourfile for security reasons.

The following example works, assuming files are loaded into a folder called "uploads"

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

MyFile="/uploads/xyz.pdf"

Response.Write MyFile
If fs.FileExists(Server.MapPath(MyFile)) Then
Response.Write("<a href='"&MyFile&"')>file xyz</a>")
else
      Response.Write("File xyz does not exist.")
End If
set fs=nothing
%>

Hope that helps

Katy
new_learner | Posted 6:31am 11. December 2006 Server Time |

Thanks Katy for the quick reply...my concern is how would the backend file know which file is gonna be updated..I mean is possible to get hold of the filename each time a new file is uploaded.
Myfile="/uploads/xyz.pdf" is fine if i know the filename which is gonna be installed. But each time a new file would be uploaded..so how does that change...??? i hope i am making sense... :-)
katy8439 | Posted 9:10am 11. December 2006 Server Time |

Are you saving the filenames to a database (which I think is the best solution), depending on your upload component you could set something like

objRecordSet("Filename")=Uploader.File.Name

of course, the above code will change depending on your recordset and upload component.

Alternatively you can list all files in the directory - but remember that every user will be able to see every file in the folder!

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

'** Folder location relative to the root of the website
MyFolder="/uploads/"

'** Check the folder exists
If fs.FolderExists(Server.MapPath(MyFolder)) Then
'** We have the folder so we set the base folder
set TheFolder = fs.GetFolder(Server.MapPath(MyFolder))
'** Now we loop through the contents
For Each objItem In TheFolder.Files
'** Write out the filename and a link
Response.Write "<a href="""&MyFolder & objItem.Name&""">"&objItem.Name&"</a><br>"
'** Move to the next File in folder
NEXT
'** Clear the folder
set TheFolder=nothing
'** End does folder exist check
END IF

'** Clear the filesystem object
set fs=nothing
%>
fzas | Posted 7:07am 29. January 2008 Server Time |

Hi Katy,

I am trying to save the filename to a field in a SQL server database called filename; as well as create folders and subfolders if not already created to a specific directory.  This action would need to happen upon saving record information.  The an e of the fodlers is partially dependent on data entered in each record.  Any ideas?

Tahnks


Reply to Post Find if a file exists in a folder



Back to Forum Page