5 Replies - 5980 Views - Last Post: 02 October 2009 - 12:39 AM Rate Topic: -----

#1 suetlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 09

Rename of existing file

Posted 01 October 2009 - 10:09 PM

protected void Button1_Click(object sender, EventArgs e)
	{
		// Before attempting to save the file, verify   
		// that the FileUpload control contains a file.   
		if (FileUpload1.HasFile) 
   
	   // Call a helper method routine to save the file.   
		  SaveFile(FileUpload1.PostedFile);   
		else  
		  // Notify the user that a file was not uploaded.   
		  UploadStatusLabel.Text = "You did not specify a file to upload.";   

	}

	void SaveFile(HttpPostedFile file)
	{
		// Specify the path to save the uploaded file to.   
		string savePath = "~/images/";

		// Get the name of the file to upload.   
		string fileName = FileUpload1.FileName;

		// Create the path and file name to check for duplicates.   
		string pathToCheck = savePath + fileName;

		// Create a temporary file name to use for checking duplicates.   
		string tempfileName = "";

		// Check to see if a file already exists with the   
		// same name as the file to upload.   
		if (System.IO.File.Exists(pathToCheck))
		{
			int counter = 2;
			while (System.IO.File.Exists(pathToCheck))
			{
				// if a file with this name already exists,   
				// prefix the filename with a number.   
				tempfileName = counter.ToString() + fileName;
				pathToCheck = savePath + tempfileName;
				counter++;
			}

			fileName = tempfileName;

			// Notify the user that the file name was changed.   
			UploadStatusLabel.Text = "A file with the same name already exists." +
				"<br />Your file was saved as " + fileName;
		}
		else
		{
			// Notify the user that the file was saved successfully.   
			UploadStatusLabel.Text = "Your file was uploaded successfully.";
		}

		// Append the name of the file to upload to the path.   
		savePath += fileName;

		// Call the SaveAs method to save the uploaded   
		// file to the specified directory.   
		FileUpload1.SaveAs(Server.MapPath(savePath));
	}




Hi all, would need some help. i wants to rename the filename that was being uploaded by user if the file name had alrdy existed in e folder. i google and found this coding online and have change e file path to a relative path (savePath). it only works if i use a absolute path but not a relative path. how can i solve it using a relative path?

thanks.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Rename of existing file

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Rename of existing file

Posted 01 October 2009 - 10:19 PM

So basically you're wanting someone here to modify some code you found to meet your needs because you're unwilling to take the time to try it yourself? Dream.In.Code is not a "Hey, I found this code but it doesn't do what I need, can you modify it for me" type of community, we are a collection of programmers who are here to help those who are willing to take the time to help themselves :)

Have you made any attempts to alter this code to fit your needs? If so then show us what you've tried, if not then you're going to have to at least give it a try first :)
Was This Post Helpful? 0
  • +
  • -

#3 suetlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 09

Re: Rename of existing file

Posted 01 October 2009 - 11:48 PM

View PostPsychoCoder, on 1 Oct, 2009 - 09:19 PM, said:

So basically you're wanting someone here to modify some code you found to meet your needs because you're unwilling to take the time to try it yourself? Dream.In.Code is not a "Hey, I found this code but it doesn't do what I need, can you modify it for me" type of community, we are a collection of programmers who are here to help those who are willing to take the time to help themselves :)

Have you made any attempts to alter this code to fit your needs? If so then show us what you've tried, if not then you're going to have to at least give it a try first :)


yes i did. its out of wits end that i actually decided to post in forum. before you even have any idea what i have tried and all, you shouldn't even be judging. if the forum is like what u claim, then what are you even doing here making all these comments?
Was This Post Helpful? 0
  • +
  • -

#4 suetlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 09

Re: Rename of existing file

Posted 01 October 2009 - 11:54 PM

View Postsuetlah, on 1 Oct, 2009 - 10:48 PM, said:

View PostPsychoCoder, on 1 Oct, 2009 - 09:19 PM, said:

So basically you're wanting someone here to modify some code you found to meet your needs because you're unwilling to take the time to try it yourself? Dream.In.Code is not a "Hey, I found this code but it doesn't do what I need, can you modify it for me" type of community, we are a collection of programmers who are here to help those who are willing to take the time to help themselves :)

Have you made any attempts to alter this code to fit your needs? If so then show us what you've tried, if not then you're going to have to at least give it a try first :)


yes i did. i don't even have the basic foundation for C#. Yet, im researching and all. Tried all modification/alteration i can/came across. its only out of wits end that i actually decided to post in forum, seeking for some guidance/help. before you even have any idea what i have tried and all, you shouldn't even be judging. if the forum is like what u claim, then what are you even doing here making all these comments?

This post has been edited by suetlah: 01 October 2009 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Rename of existing file

Posted 02 October 2009 - 12:17 AM

This site probably helps you with your relative path problem:

http://msdn.microsof...y/ms178116.aspx
Was This Post Helpful? 0
  • +
  • -

#6 suetlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 09

Re: Rename of existing file

Posted 02 October 2009 - 12:39 AM

View Postjanne_panne, on 1 Oct, 2009 - 11:17 PM, said:

This site probably helps you with your relative path problem:

http://msdn.microsof...y/ms178116.aspx




hey, thanks alot. i have read thru the URL u given and tried to amend the coding but it still doesnt work. i have no idea whether did i amend the code correctly? it doesnt goes thru the coding at all but thanks for the effort. Really appreciate it. (:

This is what i have amended. im totally clueless. it says i do not have sufficient privilege to access.

	void SaveFile(HttpPostedFile file)
	{
		// Specify the path to save the uploaded file to.   
		[b]string savePath = "/images/";[/b]

		// Get the name of the file to upload.   
		string fileName = FileUpload1.FileName;

		// Create the path and file name to check for duplicates.   
		string pathToCheck = savePath + fileName;

		// Create a temporary file name to use for checking duplicates.   
		string tempfileName = "";

		// Check to see if a file already exists with the   
		// same name as the file to upload.   

		outputLbl.Text = System.IO.Path.GetFullPath(pathToCheck).ToString();


		if (System.IO.File.Exists(pathToCheck))
		{
			int counter = 2;
			while (System.IO.File.Exists(pathToCheck))
			{
				// if a file with this name already exists,   
				// prefix the filename with a number.   
				tempfileName = counter.ToString() + fileName;
				pathToCheck = savePath + tempfileName;
				counter++;
			}

			fileName = tempfileName;

			// Notify the user that the file name was changed.   
			UploadStatusLabel.Text = "A file with the same name already exists." +
				"<br />Your file was saved as " + fileName;
		}
		else
		{
			// Notify the user that the file was saved successfully.   
			UploadStatusLabel.Text = "Your file was uploaded successfully.";
		}

		// Append the name of the file to upload to the path.   
		savePath += fileName;

		// Call the SaveAs method to save the uploaded   
		// file to the specified directory.   
[b]		FileUpload1.SaveAs(Server.MapPath("~"));[/b]


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1