We help IT Professionals succeed at work.

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

tjgrindsted
tjgrindsted asked
on
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.
Comment
Watch Question

Commented:
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
Most Valuable Expert 2012
Top Expert 2008

Commented:
You can use a LINQ query, to get the MAX date, and use that to generate a HyperLink control.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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'.

Author

Commented:
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'.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Change this

return myfile

to

return myfile.FullName

Author

Commented:
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.

Author

Commented:
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

Most Valuable Expert 2012
Top Expert 2014

Commented:
If you put a break point, what is myfile.fullname returning?

Author

Commented:
Breakline.... Do i add that before the return myfile or at the return myFile or is it on the maste codepage i add it !?
Most Valuable Expert 2012
Top Expert 2014

Commented:
You can put the breakpoint on return statement and see the value of myfile.fullname

Author

Commented:
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
Most Valuable Expert 2012
Top Expert 2014
Commented:
In the immediate window, write

?myfile.fullname

and press enter.

Author

Commented:
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.
Commented:
Hi!
PDF file you can show like this:
System.Diagnostics.Process.Start(path an filename here)

It seems that you have got code to find that file, earlier

Matti

Author

Commented:
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...

Author

Commented:
dident get it to work.