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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting File System Object question on movefile 1

Status
Not open for further replies.

UnsolvedCoding

Technical User
Jul 20, 2011
424
US

Several of us have been beating our heads against a wall on this one so I was hoping someone would know the answer.

When the first section of code is run - the Copyfile syntax - everything works fine.

'Copy the original database to the destination file
SFSO.CopyFile sFoldername & "\" & sFilename, dFoldername & "\" & dFilename, False


When the movefile synatx is used either it doesn't run or we get permissions denied.

' Move file to new location
SFSO.MoveFile sFoldername & sFilename, dFoldername


The network administrator was brought in to see if there were issues with the permissions on the network and all of us have more permission then is needed but we all have the same problem.

Question - why does the movefile code give a permissions problem but the copyfile code does not?
 


hi,

Have you read the HELP on this method, completely?

Have you looked at the Move method?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Perhaps this ?
SFSO.MoveFile sFoldername [!]& "\" [/!]& sFilename, dFoldername

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV - & "\" & gave the same errors.

Here is the complete sub. sFoldername and sFilename are definded in a different sub using Application.FileDialog(msoFileDialogFolderPicker).

Option Explicit

Sub Move_Original()

On Error GoTo Error_Handler

Dim SFSO As Scripting.FileSystemObject

' Set up the objects
Set SFSO = New Scripting.FileSystemObject

' Set the desitnation folder
dFoldername = "K:\Comments\Archive\"

' The destination file name remains the same as the source file name
dFilename = sFilename

'Copy the original database to the destination file
SFSO.CopyFile sFoldername & "\" & sFilename, dFoldername & "\" & dFilename, False

'SFSO.MoveFile sFoldername & "\" & sFilename, dFoldername

' Empty the variables
Set oFile = Nothing
Set oFolder = Nothing
Set SFSO = Nothing

Exit Sub

Error_Handler:

MsgBox Err.Description

End Sub
 
Did you have the permission to delete a file in sFoldername ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Yes. The network admin verified this before lunch just to be sure.

We tried 4 different drives and the network admin himself tried to run the program from his computer and everyone got the same message.

We also tried SFSO.MoveFile sFoldername & sFilename, dFoldername
and SFSO.MoveFile sFoldername & "\" & sFilename, dFoldername in all locations.

 
Pretty sure you get a compile error if that is indeed the "complete Sub". You have Option Explicit and your Sub has either undefined variables and/or or no legal variables being passed in as arguments.
Code:
Option Explicit

Sub Move_Original()

On Error GoTo Error_Handler

Dim SFSO As Scripting.FileSystemObject

' Set up the objects
Set SFSO = New Scripting.FileSystemObject

' Set the desitnation folder
dFoldername = "K:\Comments\Archive\"

' The destination file name remains the same as the source file name
dFilename = sFilename

'Copy the original database to the destination file
SFSO.CopyFile sFoldername & "\" & sFilename, dFoldername & "\" & dFilename, False

'SFSO.MoveFile sFoldername & "\" & sFilename, dFoldername

' Empty the variables
Set oFile = Nothing
Set oFolder = Nothing
Set SFSO = Nothing

Exit Sub

Error_Handler:

MsgBox Err.Description

End Sub

 
the move method fails if the destination file already exists in the folder (it says here!!!)

have you checked that? poss use folderexists and/or fileexists methods to check the destination

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 


...which is why I previously asked my question...
SkipVought said:
Have you read the HELP on this method, completely?


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The folder is empty and if there were other items in the folder it shouldn't matter as a datea and time stamp is placed on the end of each new file.

And yes I read the help on this method several times.
 
Isn't sFilename open by some app ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sfilename is assigned in a different sub.

The mso folder picker is used to select the folder that holds the original files. When the folder is selected a different sub loops through each file in the folder, during this process the file name is assigned to sFilename.

The file is opened, it gets prepped, data put into an array and its supposed to be moved to a new folder. The part of moving the original file to a new folder is what the sub listed above is supposed to do.
 
The file is opened
So, are you sure the file is closed before calling the sub supposed to rename it ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What is the code exactly, because as stated the posted code is incomplete? The varaibles do not seem to be passed in. Plus, what is the error exactly, precisely. This is not stated.

 
It works now. dFilename is a string.

' Calls the sub to close the workbook
Close_Workbook

' Move the file
SFSO.MoveFile sFoldername & "\" & sFilename, dFoldername

' Give the new location to the variable tha uses the foldername
sFoldername = dFoldername

' Calls the sub that opens the workbook.
Open_Workbook

' Empty the variables
Set oFile = Nothing
Set oFolder = Nothing
Set SFSO = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top