Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

rename a file using FileSystemObject

since there is no rename method in FileSystemObject, I use the following code to rename the file.

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

objFSO.MoveFile Server.MapPath("/someDir/Foo.asp"), _
                Server.MapPath("/someDir/Bar.asp")

Set objFSO = Nothing

sometimes it works fine, but sometimes I got an error messge  said:


"Server object error 'ASP 0177 : 800a003a'

Server.CreateObject Failed

upload.asp, line 267

The operation completed successfully.
"


0
apig
Asked:
apig
  • 9
  • 6
  • 4
  • +3
1 Solution
 
jitgangulyCommented:
I think its more of a network problem whiel finding the file.
Use fileexists and then use move
Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists(filespec)) Then
      msg = filespec & " exists."
   Else
      msg = filespec & " doesn't exist."
   End If
   
0
 
apigAuthor Commented:
thanks, but I did use fileexists before use move...I just found out I did not set another server object to nothing before I use this command...I just fixed that, I'm still testing. hope that helps.

any other suggestion? Thanks
0
 
jitgangulyCommented:
ALso chcek the security. Make sure IUSR_{yourservername} user has proper permissions
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ASPGuruCommented:
you can rename files by just setting the name property to a new value...
maybe this works better....


Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(Server.MapPath("/someDir/Foo.asp"))
f.Name = "Bar.asp"
Set f = Nothing
Set fso = Nothing


ASPGuru
0
 
apigAuthor Commented:
cool...I can try that....and let you know the result
0
 
apigAuthor Commented:
I checked the permission for the Iuser, it has full control...but I don't know why when I test once the permission denied error occured.
0
 
apigAuthor Commented:
I checked the permission for the Iuser, it has full control...but I don't know why when I test once the permission denied error occured.
0
 
jitgangulyCommented:
Do you have
enctype="multipart/form-data ?
like
<form method="POST" action="Upload.asp" enctype="multipart/form-data" name="updform" >
0
 
apigAuthor Commented:
yes I do have enctype="multipart/form-data"...so what should I do?
0
 
jitgangulyCommented:
Does the uploaded file has permission to IUSR_{yourservername} user ?
0
 
apigAuthor Commented:
I still can not understand the permission denied error...Is it because of some user is using that file? In my program, the file I want to rename is a jpg file, maybe it is still in cache? If so, how to avoid this?
0
 
jitgangulyCommented:
Close your browser where it has already opened or close it from the software which opens it
0
 
apigAuthor Commented:
I have to do it in ASP code...or how to just clear the cache?
0
 
jitgangulyCommented:
Your code should be something like this

Set fso = CreateObject("Scripting.FileSystemObject")
'some stuff...
'....
'....
Set fso = nothing

Now if somebody already opens that jpeg anywhere, you can not control that , meaning you can n ot force him/her to close it.
In your ASP page, you can put the following lines at the top

<%
Response.expires= 0
Response.Expires=-1
Response.Expiresabsolute = Now() - 1
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>

But its not 100% full proof not to cache ASP pages
   
0
 
apigAuthor Commented:
thanks...but I already have to same code in my program...I am still testing, it seems the permission denied error still happens once a while :(
0
 
Michel SakrCommented:
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CopyFile ("C:\..users\blabla.doc","C:\..users\blabla44.doc")
fso.DeleteFile "C:\..users\blabla.doc"


check that the file is not opened yet.. it seems the file needs some time to get unlocked before deleting..etc,..  try to use aspwait from www.serverobjects.com
0
 
AlfaNoMoreCommented:
Set FSO = Server.CreateObject("Scripting.FileSystemObject")

Should be:

Set FSO = CreateObject("Scripting.FileSystemObject")

i.e. Lose the "Server." command.
0
 
ASPGuruCommented:
why?
0
 
AlfaNoMoreCommented:
Because as far as I can tell, it's not a server object. I don't think the Scripting class is a COM object or within MTS, but is a system resource, or something?

Have a look in MSDN, you'll always see the FSO or Dictionary objects being created with CreateObject, and not with a Server.CreateObject.
0
 
ASPGuruCommented:
create each COM object you use in ASP with server.createobject...

the arguments you present aren't relevant.

You always see it just with createobject in MSDN, because they show WSH examples and wsh-scripts don't have a server object.

ASPGuru
0
 
AlfaNoMoreCommented:
OK, so my initial observation was wrong. Thought I'd just throw it in there as I never create an FSO object in that manner.

However, I got this from Microsoft:

SYMPTOMS
When you use the FileSystemObject object from an Active Server Pages (ASP) page, you may receive one of the following error messages:

Server object error 'ASP 0177 : 800a0035'
Server.CreateObject Failed
filename.asp, line xx
The operation completed successfully.
-or-
Microsoft VBScript runtime (0x800A0035)
File not found
filename.asp, line xx



CAUSE
This error occurs because FileSystemObject cannot find the file that is being accessed (through the OpenTextFile , DeleteFile , or CopyFile methods, for example). This typically occurs for two reasons:


The file does not exist.


FileSystemObject is not looking for the file in the folder that you had expected. By default, if you do not specify a path, FileSystemObject searches the WinNT\System32 folder.
0
 
robbertCommented:
>sometimes it works fine, but

Simply forget the above.

> upload.asp

From my experience, I can tell that a file that _was_ uploaded (using SoftArtisans' FileUp), and _can_ be seen by Windows Explorer, _may not_ be recognized by the FileSystemObject, instantly, though. There may be an intermediate time (i.e., one second) where the newly created file is visible in Windows Explorer, but pointing the FileSystemObject to it throws an error.

My colleague showed this to me and I found that there was surely no mistake of him, but apparently, a delay with the FileSystemObject.

Unfortunatly, I don't know which solution he took.

Maybe, he had looped until the file appeared (until there was no more error), but this is not perfect solution.

- Hope this helps in recognizing the problem, at least.
0
 
apigAuthor Commented:
do you know what is the problem?

I always got that error because the file (an image file) I wanted to rename is being used by my another web page. That means that image file is in use, so I got "permission denied" error sometimes. What I can not understand is why this error just happened SOMETIMES?
0
 
ASPGuruCommented:
i'm not sure, if that solves something, but have you tried givin the IWAM_* user rights?

ASPGuru
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now