Getting the lastest saved file name from a directo

diegohack_1

New Member
Can anyone tell me what aspx code can be used to find the name of the last saved file in a directory? <BR><BR>I have a directory which people can save files to but I don't want the same name to be used which would overwrite the existing file. What I would like to do is to number my files e.g 1.txt,2.txt,3.txt,4.txt,... .Then have the last filename e.g 4.txt read and increment it by 1 so it saves the new uploaded file as filename 5.txt.<BR><BR>Any ideas? I have code just now which saves the file to a directory but doesn't check any file names there in order to prevent overwriting. The code is shown below:<BR><BR>Thanks, for any help given!<BR>Mazza<BR><BR><html><BR><BR><head><BR><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><BR><title>Picture_Upload</title><BR><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><BR><meta name="ProgId" content="FrontPage.Editor.Document"><BR><meta name="Microsoft Theme" content="copy-of-blends 111, default"><BR><meta name="Microsoft Border" content="t, default"><BR><BR><Script Language="VB" RunAt="Server"><BR><BR>Sub Page_Load(Sender as Object, e as EventArgs)<BR><BR> Dim MyPath, MyName as string<BR> ' Display the names in D: that represent directories.<BR><BR> MyPath = "D:Inetpubwwwroot ext" ' Set the path.<BR> MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.<BR><BR> if MyName="" ' The folder is not there & to be created<BR> MkDir("D:Inetpubwwwroot ext\") 'Folder created<BR> span2.InnerHtml="A Folder (D:Inetpubwwwroot ext) is created at the Page_Load"<BR> end if<BR><BR>End Sub<BR> <BR> Sub Upload_Click(Sender as Object, e as EventArgs)<BR><BR> ' Display properties of the uploaded file<BR><BR> FileName.InnerHtml = MyFile.PostedFile.FileName<BR> FileContent.InnerHtml = MyFile.PostedFile.ContentType <BR> FileSize.InnerHtml = MyFile.PostedFile.ContentLength<BR> UploadDetails.visible = True<BR><BR>' Let us recover only the file name from its fully qualified path at client <BR>Dim strFileName as string<BR> strFileName = MyFile.PostedFile.FileName<BR> Dim c as string = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path<BR><BR> ' Let us Save uploaded file to server at D:Inetpubwwwroot ext<BR><BR><BR> Try <BR><BR> MyFile.PostedFile.SaveAs("D:Inetpubwwwroot ext\" + c)<BR> Span1.InnerHtml = "Your File Uploaded Sucessfully at server as : D:Inetpubwwwroot ext\" & c<BR> <BR> catch Exp as exception<BR> span1.InnerHtml = "An Error occured. Please check the attached file"<BR> UploadDetails.visible = false<BR> span2.visible=false<BR> End Try<BR><BR>Response.Cookies("Picture")("FileName") = "D:Inetpubwwwroot ext\" + c<BR>End Sub<BR><BR> <BR><BR> </Script><BR></head><BR><Body><BR> <BR> <Form Method="POST" EncType="Multipart/Form-Data" RunAt="Server"><BR> <p><b><font size="6">Load Picture</font></b></p><BR> <Input ID="MyFile" Type="File" RunAt="Server" Size="40"> <BR><BR> <BR><BR> <Input Type="Submit" Value="Upload" OnServerclick="Upload_Click" RunAt="Server"><BR> <input type="button" value="Submit" onClick="javascript:window.close();"><BR><BR> <P><BR> <Div ID="UploadDetails" Visible="False" RunAt="Server"><BR> File Name: <Span ID="FileName" RunAt="Server"/> <BR><BR> File Content: <Span ID="FileContent" RunAt="Server"/> <BR><BR> <BR><BR> File Size: <Span ID="FileSize" RunAt="Server"/>bytes<BR> <BR></Div><BR> <Span ID="Span1" Style="Color:Red" RunAt="Server"/><BR> <Span ID="Span2" Style="Color:Red" RunAt="Server"/><BR> <BR> </Form><BR><BR><HR Size="2" Color=Black><BR><BR> </span></span></span></span></span></b><BR><BR> </Body><BR></html>
 
Back
Top