Link to home
Start Free TrialLog in
Avatar of tjgrindsted
tjgrindsted

asked on

How do i show the newest pdf file from folder as a link !?

Hi

I have a folder "upload" here i have some PDF files, i can upload a file to the folder and the files is getting the upload date as name, so i file uploaded today will have the name "20111207.pdf"
I need to make a link that only link to the newest PDF file.

So i dont need to changes the link when i have uploadet a PDF file, but so i only need to upload the pdf with the date and then a script will find the file with the biggest number and show that as a link.

How can i do that !?

ex.
i god these files in the folder.
Upload
- 20101015.pdf
- 20110125.pdf
- 20120101.pdf

then i need a script that can look after the highest number (in this case 20120101)
and then make a link to that file as <a href="upload/20120101.pdf">Hent Prisliste</a>
and if i upload a new pdf file with at bigger number then 20120101 then it vil show that link, to the file.

Hope u understand what im looking for.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Getting the newest file isn't difficult in a VBscript or batch file... and neither is creating a suitable LNK link file to it, which I can only assume is how this expert alert came to head this way ... but having opened the question I assume from your zones entered and web link request that this is a web based app that once the file is uploaded should read the directory structure as you say.

You may want to clarify how and where this script is to run, what language you are after and the like though it isn't my area now anyway.

Steve
You can use a LINQ query, to get the MAX date, and use that to generate a HyperLink control.
Try following

<a href='<%# GetLink() %>'>Download PDF</a>

Add this function to the code behind

Private Function GetLink() As String
Dim directory = New DirectoryInfo("C:\MyDirectory")
Dim myFile = (from f in directory.GetFiles() Order By f.LastWriteTime Descending).First()
return myfile
End Function



You may want to place with the myfile path to make sure you return correct relative URL.
Avatar of tjgrindsted
tjgrindsted

ASKER

Hi all sry, that im writing back now.

@CodeCruiser
if i use ur ex. and add Import System.IO on the codebehind
I get this error in live code.

BC30390: U cant get access to '_Default.Private Function GetLink() As String' when its 'Private'.
If i changes the Private Function to Public, then i get this error

BC30311: A value of the type 'System.IO.FileInfo' can nok be convertet to 'String'.
Change this

return myfile

to

return myfile.FullName
Hi again

If i add
Import System.OI
and changes the Privat to Public and add the code u just give me, and i then run the code
i then get this link, when i make a mouse over the "download PDF"
http://localhost:1145/ShowNewestFileFromFolder_OK/Default.aspx

Where it need to be
http://localhost:1145/ShowNewestFileFromFolder_OK/upload/WEB_X6DVBEUA-ENG-0401.pdf

right now its the only file in the Upload folder, so it must be the newest.
Code
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Public Function GetLink() As String
        Dim directory = New DirectoryInfo("C:\Users\MacNitro\Documents\Visual Studio 2010\WebSites\ShowNewestFileFromFolder_OK\Upload")
        Dim myFile = (From f In directory.GetFiles() Order By f.LastWriteTime Descending).First()
        Return myFile.FullName
    End Function

End Class

Open in new window

If you put a break point, what is myfile.fullname returning?
Breakline.... Do i add that before the return myfile or at the return myFile or is it on the maste codepage i add it !?
You can put the breakpoint on return statement and see the value of myfile.fullname
ok
When i add the BP and run the code, and i then look at the BreakPoint window, i see
Function
_Default.GetLink()

Process
WebDev.WebServer40.exe

Condition
no condition
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
then i get this, have tryed 2 times

?myfile.fullname
The expression cannot be evaluated while in run mode.

?myfile.fullname
The expression cannot be evaluated while in design mode.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i cant get this to work, im sry to say it, and yes maybe its me, but im not coding as a pro. so i need the knowhow and exp. to know alot of things, but what u are writing, im not getting it, sry...
dident get it to work.