Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp renaming file

Status
Not open for further replies.

AGNEW2PRG

Technical User
Aug 5, 2003
98
AE
Hi All,

I am trying to rename a file through an web page. I currently use a form with a file input. On submit the code bbelow is called as a external asp page. I would like to rename the file or copy the file to a new location on the webserver with a prefix "a" or "1" etc. Is this possibble with the code below.

example file name
myfile.txt -> 1myfile.txt

<%

set fso = Server.CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile(Request.Form("file1"))
f1.name = "1"&f1.name
Set f1 = Nothing
set fso = Nothing

%>

any help will be much appreciated. Thanks in advance
 
Are you uploading the file from through he web page or just submitting the filename?

Is the script in the same folder as the files you want to rename?
 
hi travisbrown.

I would like to copy the selected file in the form to a windows folder. Is this possible?

eg., c:\data

thanks for your reply.

regards
 
Code:
sourcefile = Request("yourfile") 'i.e. c:\source\somefile.txt
dim fso, demofile
set fso= CreateObject ("Scripting.FileSystemObject")
set yourfile = filesys.GetFile(sourcefile)
yourfile.Copy("c:\destination\someotherfile.txt")

There is a bunch you can do...check for existing, overwrite, etc. Google FSO and ASP and you'll find some pretty good references out there.
 
aganthony,
Is the file you are looking to work with already on the server? Or are you selecting a file on the local PC and moving it TO the server?
The methods would be very different depending on which you are doing.

Since you said you are using a form with a file input I assume you are browsing local computer files and want to move one to the server. This requires a bit of code in and of itself to accomplish. Renaming the file as it goes is a cinch though.

Paranoid? ME?? WHO WANTS TO KNOW????
 
This is why I asked if he was uploading the file or not. From his answer, I assumed the files are both local. So, yeah, uploading is a different matter. Uploading in ASP is a PITA and slow, unless you use a COM.

If you are uploading rather than copying, and this is an internet app, check and see what components your webhost has. They are much easier to work with and much more efficient than doing it in pure asp.

t.
 
Yes his response did sound like it was local but I helped him out with script for doing multiple file uploads a while back which makes me think this is likely related.

aganthony, the first thing I would ask is under what circumstances are you going to end up with filenames that are the same when the file itself is not? How are you going to be able to distinguish one file from another later on?
Is this being used by different people? Should different people be able to see ALL the files uploaded or just their own?

You would need code that reads in filenames in the designated folder looking to see if there is a match for the particular filename you want to upload so that you will know to alter the filename in the first place. Then you need to determine what to alter the filename TO.
For instance, if you decided to append a number on the filename then you would have to read to see if a number existed on the file so that you would know what to increment it to.
This leads to problems because the filename is generally something the uploader is familiar with and may contain numbers already and appending or incrementing numbers would throw off what they expect it to be. If you simply append to the filename then each time another is uploaded of the same name the filename gets longer.

It might be easier and cleaner to just check if the proposed filename exists already and prompt the user to give a new name to save it under so that they know what it was set to.
And if each person should only have access to their own files then you can always create sub-folders for each logon ID doing uploads and use that logon ID to generate the path to store the file.

Sorry, I know I introduce more questions but these are the problems you are about to run into so it's better to address them now.


Paranoid? ME?? WHO WANTS TO KNOW????
 
guys,

firstly thanks for all the asistance you are giving me, and apologies for the late response as i was very busy over the last couple of days.

Thenightowl, your right. the issues is related to the multiple file uploads. let me give you some background info. I have designed some pages that creates a text file based on user input. the last page allows the user to select image files that they need. once the last step is completed the textfile and the images(local to each users machine) are uploaded to a central folder. In this case \images. this is achieved using the multiple file using upload script you suggestd.

Once the files arrive at a central location, it is then picked up by an application, reads its content, fetches the images from the central location and prints a document using a pre-defined template based on the users input and selected images.

however, in this case all of the stages work. I have now foundout that the images are all renamed the same. EG:
\image1\abcde.jpg, \image2\abcde.jpg, \image3\abcde.jpg and so on. So, when the script copies the files over the images overwrite each othere as they are all named the same. Hence when the document is printed all of the images are the same.

my thoughts was to rename the file either before or after the upload to the central location. and prefix it with\image1\img1_abcde.jpg, \image2\img2_abcde.jpg, \image3\img3_abcde.jpg to get around this problem. also the text file generated needs to reflect the new file name.

I have even thought of removing the upload script and using a standard vbscript to copy and rename the image files on submit as all of the above is running on the Local Lan.

as you can see i am a novice at this and looking for the best way to achieve the above.

Once again thanks for all your help..

many thanks
 
It has been a while but I think the file naming problem is just a glitch in the script and can be changed.
I had the same problem with a database upload script.
I will have to look over the code to see why they all get the same name but that can be fixed rather than having to go through all the workarounds.

Have you considered uploading into a database rather than to server folders? The code is nearly identical and once the files are in the database you have no problems with duplicated filenames, deciding who sees what, etc.

Paranoid? ME?? WHO WANTS TO KNOW????
 
sorry, may be i explained this incorrectly.. the files that are being uploaded to the server do have the same name. but are located in different folders. So I dont think the script is the problem.

Loading the images into a database will not work for as the application that uses the images is unable to connect to the database. Hence it would have problems picking up the files.

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top