Results 1 to 8 of 8

Thread: Creating File & Writing Data Causes a Permissions Error

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Creating File & Writing Data Causes a Permissions Error

    I get a "permission denied" error while running the following code.

    vb6 Code:
    1. Public Function DeleteLines(ByVal from_name As String, ByVal to_name As String, ByVal target As String) As Long
    2.     Dim strlen As Integer
    3.     Dim from_file As Integer
    4.     Dim to_file As Integer
    5.     Dim one_line As String
    6.     Dim deleted As Integer
    7.  
    8.     ' Open the input file.
    9.     from_file = FreeFile
    10.     Open from_name For Input As from_file
    11.  
    12.     ' Open the output file.
    13.     to_file = FreeFile
    14.     Open to_name For Output As to_file
    15.  
    16.     ' Copy the file skipping lines containing the
    17.     ' target.
    18.     deleted = 0
    19.    
    20.     Do While Not EOF(from_file)
    21.         Line Input #from_file, one_line
    22.         If InStr(one_line, target) = 0 Then
    23.             Print #to_file, one_line
    24.         Else
    25.             deleted = deleted + 1
    26.         End If
    27.     Loop
    28.  
    29.     ' Close the files.
    30.     Close from_file
    31.     Close to_file
    32.    
    33.     Kill (from_name)
    34.     Name to_name As from_name
    35.    
    36.     DeleteLines = deleted
    37. End Function

    It happens on Print #to_file, one_line. Something must have changed, because it was working fine before. But I don't see anything that could've affected this. So just for clarifiction: the code creates a new file from target. This is working correctly. Then it's supposed to copy the lines from the old file (from_name) to the new file and then deletes the old file. The copying part is where it's failing.

    Thanks
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,476

    Re: Creating File & Writing Data Causes a Permissions Error

    A file permission error may have nothing to do with your code, but rather where you are trying to write the file. On modern Windows operating systems, many directories have read-only permissions. For example, by default the "Program Files" directory does not have write permissions. The individual users directories however, do have write permission. This is all part of a deliberate effort on the part of Microsoft to protect one user from another.

    J.A. Coutts

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Creating File & Writing Data Causes a Permissions Error

    Quote Originally Posted by couttsj View Post
    A file permission error may have nothing to do with your code, but rather where you are trying to write the file. On modern Windows operating systems, many directories have read-only permissions. For example, by default the "Program Files" directory does not have write permissions. The individual users directories however, do have write permission. This is all part of a deliberate effort on the part of Microsoft to protect one user from another.

    J.A. Coutts
    UAC is off and it's running from a My Documents. it also worked on Friday.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,476

    Re: Creating File & Writing Data Causes a Permissions Error

    A file permission error may have nothing to do with your code, but rather where you are trying to write the file. On modern Windows operating systems, many directories have read-only permissions. For example, by default the "Program Files" directory does not have write permissions. The individual users directories however, do have write permission. This is all part of a deliberate effort on the part of Microsoft to protect one user from another.

    J.A. Coutts

  5. #5
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,476

    Re: Creating File & Writing Data Causes a Permissions Error

    Quote Originally Posted by weirddemon View Post
    UAC is off and it's running from a My Documents. it also worked on Friday.
    On windows Vista or better, "My Documents" does not actually exist. Go to the command prompt and enter the following command in your user directory:
    C:\Users\UserID>dir /a
    There you will see "My Documents" listed as a junction:
    01/12/08 08:27 PM <JUNCTION> My Documents [C:\Users\UserID\Documents]
    The real name of the directory is listed as just "Documents".

    J.A. Coutts

  6. #6

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Creating File & Writing Data Causes a Permissions Error

    Quote Originally Posted by couttsj View Post
    On windows Vista or better, "My Documents" does not actually exist. Go to the command prompt and enter the following command in your user directory:
    C:\Users\UserID>dir /a
    There you will see "My Documents" listed as a junction:
    01/12/08 08:27 PM <JUNCTION> My Documents [C:\Users\UserID\Documents]
    The real name of the directory is listed as just "Documents".

    J.A. Coutts
    I would appreciate it if you could provide any information that is relevant. This has nothing to do with what I'm asking.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Creating File & Writing Data Causes a Permissions Error

    I was mistaken on when the error occurs. It seems as if it executes Do While Not, Line Input #from_file, one_line, and If InStr(one_line, target) = 0 Then just fine. Then it loops back up to Do While Not EOF(from_file) and errors out there.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Creating File & Writing Data Causes a Permissions Error

    Ugh... this whole thing is a mess. For some reason, the line where I compare the string (Instr) was changed from > 0 to = 0. I changed it back so that's working again. But it looks like it is failing on where I'm trying to delete from name via Kill (from_name). I'm closing the file prior to that point, so I'm not sure why I'm being denied.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

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