PDA

View Full Version : Cannot move files or change folder/file permissions in VBScript



SparceMatrix
03-15-2006, 10:46 AM
I am getting "Permission Denied" errors when I try to move files suing the move method of a file object:


' VB Script Document

Option explicit

Dim objFSO, NewFile01, NewFile20, NewFile00, NewFile21
Dim TestFile, TestFolder, TestFolderAtt

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set NewFile01 = objFSO.OpenTextFile("TestFolder/Text01.txt", 8, TRUE )
Set NewFile20 = objFSO.OpenTextFile("TestFolder/Text20.txt", 2, FALSE )
Set NewFile00 = objFSO.OpenTextFile("TestFolder/Text00.txt", 8, FALSE )
Set NewFile21 = objFSO.OpenTextFile("TestFolder/Text21.txt", 2, TRUE )

Set TestFolder = objFSO.GetFolder("/")
'TestFolder.attributes = TestFolderAtt And (Not ReadOnly)
Set TestFile = objFSO.GetFile("TestFolder/Text21.txt")
TestFile.move("/")


Set objFSO = nothing
Set NewFile01 = nothing
Set NewFile20 = nothing
Set NewFile00 = nothing
Set NewFile21 = nothing

A quick search of the internet turned up some similar complaints with some discussion of the "Read Only" attributes of the Folders. I found that I could not change the "Read Only" permissions on my folders. I did a quick search of the MS Knowledge Base and found the following article: 326549 (http://support.microsoft.com/kb/326549/?FR=1). The workaround of setting attributes with "attrib" from the command line does not work for me. Trying to set the attributes from the Folder object attibutes property as suggested in my O'Reilly pocket reference (TestFolder.attributes = TestFolderAtt And (Not ReadOnly)) gives me "Variable Undefined" errors. I also work from a user with non-administrative privileges.

Anybody have any idea what I should do to enable VBS to move a file? Am I missing something?

Killian
03-16-2006, 02:39 AM
Hi there,

If files/folders are set as r/o and you don't have sufficient permission to change them with your account, then running a script that attempts it will result in an error.
Have you tried testing the code in an area where you have write permission?

SparceMatrix
03-16-2006, 02:02 PM
I just tried that and find that with a user that has adminstrative privileges, I get no "Permission Denied" errors. But, the file that is "moved" has disappeared. It is not in the expected location. This also occurs when I also try "copy".

Assuming this is working, why should I have to have administrative privileges to use scripts to move around in directories created by my user using scripts created by my user?

Killian
03-17-2006, 08:20 AM
why should I have to have administrative privileges to use scripts to move around in directories created by my user using scripts created by my user?Normally, this would not be a problem. However, you may want to talk to your network admin to make sure that there are no restrictions on use of scripts or some of their methods.

I'm not sure the relative pathing you've used in your example is valid (?)
Try test script and see if you get any permission errors
(You'll need a folder called "C:\TEMP" containing a file called "test.txt"Option Explicit

Dim fso, fldrSource, fldrTarget, f, tempf
Dim strSourcePath, strTargetPath

Set fso = CreateObject("Scripting.FileSystemObject")
'set up fso and source folder
'Set fso = New Scripting.FileSystemObject
strSourcePath = "c:\temp\"
strTargetPath = strSourcePath & "tempfolder\"

Set fldrSource = fso.GetFolder(strSourcePath)

'get/create target folder
If fso.FolderExists(strTargetPath) Then
Set fldrTarget = fso.GetFolder(strTargetPath)
Else
Set fldrTarget = fso.CreateFolder(strTargetPath)
End If

'get/create test file
If fso.FileExists(strSourcePath & "temp.txt") Then
Set f = fso.GetFile(strSourcePath & "temp.txt")
Else
Msgbox "No file found"
End If

'copy/move the test file
'f.Copy strTargetPath & "temp.txt", True
f.Move strTargetPath

SparceMatrix
06-29-2006, 12:27 PM
I'm back with the same problem to see what happened the last time I confronted it.

I tried the above script and it was very, very handy. It performed not as I expected. I was able to move the test file into the created "tempfolder" with no problems and the temp folder was owned by another administrative level user.

After some careful examination I have decided that the problem is that I omitted a "\". If you change the script and leave out the slash for "tempfolder\" so that you have the line below ...

strTargetPath = strSourcePath & "tempfolder"
you do not get permission errors, you get a "file already exists" error for the last line in the code, (line 31).
I fixed my slash and everything appears to work.