file-uploadvbscriptuploadasp-classicfilesystemobject

FileSystemObject MoveFile seems inconsistent and works when moving some files while not others


I am getting an error when using MoveFile trying to move a ppt file in place of a pptx file, but not the other way around. In this sample code I am moving a file from files_temp folder to a files folder (folder creation code not included).

Set theForm = Server.CreateObject("ABCUpload4.XForm")
theForm.MaxUploadSize = 5242880
theForm.Overwrite = True
Set theField = theForm.Files("filefield")

f_name = Trim(UCase(theField.FileName))
f_type = Trim(UCase(theField.FileType))

'=================================================================
' Overwrite ppt/pptx file if exists else move from temp to save folder
'=================================================================
Dim file_type_array1 = Array("pptx", "ppt")
Dim file_type_exists1 = false

temp_file_path = Trim(Request.ServerVariables("APPL_PHYSICAL_PATH")) & "files_temp\" & f_name & "." & f_type
save_file_path = Trim(Request.ServerVariables("APPL_PHYSICAL_PATH")) & "files\" & "new_file_name." & f_type

'save_file_path w/o extension (used to loop through all extensions)
save_file_path_ne = Trim(Request.ServerVariables("APPL_PHYSICAL_PATH")) & "files\" & "new_file_name."

Set fsobject = Server.CreateObject("Scripting.FileSystemObject")
For x = 0 To UBound(file_type_array1)
    If fsobject.FileExists(save_file_path_ne & file_type_array1(x)) then
        fsobject.DeleteFile(save_file_path_ne & file_type_array1(x))
        fsobject.MoveFile temp_file_path, save_file_path
        file_type_exists1 = true
    End If
Next
If file_type_exists1 = false Then
    fsobject.MoveFile temp_file_path, save_file_path
End If
set fsobject = nothing
file_type_exists1 = false

Cases when this code works:

  1. 1st upload of a file (pptx OR ppt) - properly moved from "files_temp" to "files" folder
  2. ppt file is overwritten by pptx file - ppt file is properly deleted and pptx file is moved from "files_temp" to "files" folder

Case when this code does NOT work:

  1. pptx file is attempted to be overwritten by ppt file - pptx file is properly deleted and a file is taken from "files_temp" folder then Error on the MoveFile function:

Microsoft VBScript runtime error '800a0035' File not found

I have not included all the code, but should be enough to solve this problem. Let me know if you need additional code provided. Also rewritten my existing code for this example, so there's a small chance of Syntax Errors.


Solution

  • I did not figure out the exact reason why I was having these issues, but I do have a workaround. Instead of using MoveFile to move and simultaneously rename the temp file, I had to break it down into two steps. I had to first copy the file using CopyFile from temp_file_path to save_file_path then change the file name inside the save_file_path.

    Revised Code:

    'save_file_path but with original file name
    save_file_path_o = Trim(Request.ServerVariables("APPL_PHYSICAL_PATH")) & "files\" & f_name & "." & f_type
    
    Set fsobject = Server.CreateObject("Scripting.FileSystemObject")
    For x = 0 To UBound(file_type_array1)
    If fsobject.FileExists(save_file_path_ne & file_type_array1(x)) then
        fsobject.DeleteFile(save_file_path_ne & file_type_array1(x))
        fsobject.CopyFile temp_file_path, save_file_path_o
        Set objFile = fsobject.GetFile(save_file_path_o)
        objFile.Name = save_file_name
        file_type_exists1 = true
    End If
    Next
    If file_type_exists1 = false Then
        fsobject.MoveFile temp_file_path, save_file_path
    End If
    set fsobject = nothing
    set objFile = nothing
    file_type_exists1 = false