Creating File & Writing Data Causes a Permissions Error
I get a "permission denied" error while running the following code.
vb6 Code:
Public Function DeleteLines(ByVal from_name As String, ByVal to_name As String, ByVal target As String) As Long
Dim strlen As Integer
Dim from_file As Integer
Dim to_file As Integer
Dim one_line As String
Dim deleted As Integer
' Open the input file.
from_file = FreeFile
Open from_name For Input As from_file
' Open the output file.
to_file = FreeFile
Open to_name For Output As to_file
' Copy the file skipping lines containing the
' target.
deleted = 0
Do While Not EOF(from_file)
Line Input #from_file, one_line
If InStr(one_line, target) = 0 Then
Print #to_file, one_line
Else
deleted = deleted + 1
End If
Loop
' Close the files.
Close from_file
Close to_file
Kill (from_name)
Name to_name As from_name
DeleteLines = deleted
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
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
Re: Creating File & Writing Data Causes a Permissions Error
Quote:
Originally Posted by
couttsj
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.
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
Re: Creating File & Writing Data Causes a Permissions Error
Quote:
Originally Posted by
weirddemon
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
Re: Creating File & Writing Data Causes a Permissions Error
Quote:
Originally Posted by
couttsj
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.
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.
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.