Consulting

Results 1 to 5 of 5

Thread: Cannot move files or change folder/file permissions in VBScript

  1. #1

    Cannot move files or change folder/file permissions in VBScript

    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. 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?

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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?
    K :-)

  3. #3
    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?

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Quote Originally Posted by SparceMatrix
    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"[VBA]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[/VBA]
    K :-)

  5. #5
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •