CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2003
    Posts
    38

    CreateTextFile Permission Denied??

    I am using CreateTextFile and if the file doesn't already exist, it creates it OK. If it does exist I can delete it using FSO.DeleteFile and it gets deleted but then the CreateTextFile fails with permission denied. What's really bugging me is I had this working for a while and now I can't get around it.

    Any clues?

  2. #2
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Make it wait for a bit before you do the createfile - its probably still deleting. Also, you might want to consider using "Kill" rather than 'deleteFile'
    Be nice to Harley riders...

  3. #3
    Join Date
    Dec 2003
    Posts
    38
    I changed to Kill and I added this timer (from MSDN) before attempting the CreateTextFile :

    Dim PauseTime, Start, Finish, TotalTime
    If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5 ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Loop
    Finish = Timer ' Set end time.
    TotalTime = Finish - Start ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
    Else
    End
    End If

    I tried it at 5 seconds and then at 10 seconds. I still get permission denied if the file existed.

    I have a filList that has that file in it. I thought maybe that was causing the problem but I even stripped that code & the box out and I still fail. I'm stumped.

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    As it seems a bit odds, I tested with this code on Windows 2000
    server, and had no matter at all - I am logged in as
    administrator... - I put a breakpoint on the delete, and execute a
    step at a time going to see if file was correctly deleted and
    recreated. Then I try without any breakpoint and it went on fine.
    This seems to indicate your is not a scripting problem per se...:

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim Fso As Scripting.FileSystemObject
        Dim Counter As Integer
        Set Fso = New Scripting.FileSystemObject
        For Counter = 0 To 2
            If Fso.FileExists("c:\test.txt") Then
                Fso.DeleteFile "c:\test.txt", True
            End If
            Dim tStream As TextStream
            Set tStream = Fso.CreateTextFile("c:\test.txt", True)
            tStream.Write "Hello World"
            tStream.Close
        Next
    End Sub
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Dec 2003
    Posts
    38
    I'm thinking it has more to do with the file extension I am working with. I should have explained better that I reading an existing .txt file and creating a .smi file (MediaPlayer captioning file). If the .smi doesn't pre exist FSO CreateTextFile creates it OK. If it did exist even though I deleted it, the create fails. I even tried to create it as a TEMP.txt then do a FSO COPYFILE thinking that the CreateTextFile had a problem with the .smi extension but the copyfile also fails with permision denied. I even tried deleteing the original .txt file before doing the create. Same results.

    This is stange but there's got to be a way around it.

    Is there a file rename capability?

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Know nothing about .smmi files, but - unless they are immediately opened, they should not make Fso behave differently - is mediaplayer opened while you're doing this operation?
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Dec 2003
    Posts
    38
    .smi files are just txt files with a special format to control the captions that are displayed below the video in MediaPlayer. You can code them up as .txt in notepad and then rename them to .smi which is essentially what the utility I am writing is trying to accomplish.

    Good thought about MediaPlayer maybe holding onto it. That may be the culprit. This timing is bizarre though. I changed it to do the delete of the .smi before ever starting Mediaplayer and I put a msgbox in after the delete. I went to the directoy at that point and the file is still in there. It wouldn't let me rename it at that point. So the delete isn't actually taking place when I execute the FSO delete.

    I need to play around some more.

  8. #8
    Join Date
    Dec 2003
    Posts
    38
    Found it!

    I had to move the delete up prior to having set MediaPlayer.FileName = ....mp3 file that is going to be played. Even though I hadn't started the player yet it had hold of the associated .smi file if it existed.

    Thanks for the tips.

  9. #9
    Join Date
    Oct 2008
    Posts
    2

    Re: CreateTextFile Permission Denied??

    Hi all,
    I am using this simple code

    <%
    dim fs,tfile
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    set tfile=fs.CreateTextFile("somefile.txt",true)
    tfile.WriteLine("Hello World!")
    %>

    it gives me a permission denied at this line

    fs.CreateTextFile("somefile.txt",true)

    can any one help me out please.
    been stuck with this for quite some time.

    thank you

  10. #10
    Join Date
    Oct 2008
    Posts
    2

    Re: CreateTextFile Permission Denied??

    another thing...
    i have all the permissions on the server
    and the exact message is as follows

    Microsoft VBScript runtime error '800a0046'

    Permission denied

    /enlacefl/blogforms.asp, line 36

    thanks again

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: CreateTextFile Permission Denied??

    5ms is way too slow, even for windows. It needs 15-20ms to process anything. Change it to 500, or 1000, and try it again.

    Also, you are declaring this as OBJECTS which isn't optimal.
    Code:
    Option Explicit
    '
    ' Add a reference to Microsoft Scripting Runtime
    
    Sub GetFileInfo(strPath As String)
    '  On Error Resume Next
      Dim s1 As String
      Dim f As Object, fso As New FileSystemObject
      Set f = fso.GetFolder(strPath)
      s1 = f
      MsgBox s1 & vbCrLf & _
        "Date Created " & f.DateCreated & vbCrLf & _
        "Last Modified " & f.DateLastModified & vbCrLf & _
        "Last Accessed  " & f.DateLastAccessed
    End Sub
    
    Private Sub Form_Activate()
      GetFileInfo "c:\windows\"
    End Sub
    Last edited by dglienna; October 31st, 2008 at 03:35 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jul 2008
    Posts
    45

    Re: CreateTextFile Permission Denied??

    Just simply try,

    dim fso as filesystemobject

    private sub load ()

    if fso.fileexist(directoryofchoice) = false then
    open (directoryofchoice)for output as #1
    close #1
    end if



    open (directoryofchoice)for output as #1

    doevents

    close #1



    end sub

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured