Solved

Change file attributes?

Posted on 2004-09-22
6
3,246 Views
Last Modified: 2011-09-20
I have a text file that I add entries to, through my code.
Unfortunately, the file is stored in a folder where it 'might' get deleted.  I'd like to make it RO, but that would prohibit my code from opening it for append.  How can I remove the RO and then readd it in VB6?
0
Comment
Question by:sirbounty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12122080
There is SetFileAttributes API

Const READONLY = &H1
Const HIDDEN = &H2
Const SYSTEM = &H4
Const ARCHIVE = &H20
Const NORMAL = &H80
Private Declare Function SetFileAttributes Lib "kernel32.dll" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long

Private Sub Form_Load()
Dim attr As Long
Dim rval As Long
attr = READONLY + SYSTEM + HIDDEN
rval = SetFileAttributes(YoorFile, attr)
End Sub
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 125 total points
ID: 12122101
You can try to do this:

SetAttr "TESTFILE", vbNormal

SetAttr "TESTFILE", vbReadOnly     ' Set read-only
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12122108
SetAttr Statement

Description - Sets attribute information for a file.
Syntax - SetAttr pathname,attributes

The SetAttr statement syntax has these named-argument parts:
Part - Description
pathname -  String expression that specifies a file name — may include directory or folder, and drive.
attributes - Constant or numeric expression, the sum of which specifies file attributes.

Constants and values for attributes are:
vbNormal - 0 - Normal (default).
vbReadOnly - 1 Read-only.
vbHidden - 2 - Hidden.
vbSystem - 4 - System — not available on the Macintosh.
vbArchive - 32 - File has changed since last backup — not available on the Macintosh.

Note

These constants are specified by Visual Basic. As a result, the names can be used anywhere in your code in place of the actual values.

Remarks
A run-time error occurs if you try to set the attributes of an open file.

See Also
FileAttr Function, GetAttr Function.
Example
This example uses the SetAttr statement to set attributes for a file.
SetAttr "TESTFILE", vbHidden    ' Set hidden attribute.
SetAttr "TESTFILE", vbHidden + vbReadOnly    ' Set hidden and Read-only
    ' attributes.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 67

Author Comment

by:sirbounty
ID: 12123504
Dhaest - love the simple solution!
However, I'm curious is it a complete change?
For instance, if I have RASH - and I use vbNormal - is that now 'no' attribs?
If it's none and I wanted to specify HS - is it just A + B (or 6)?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 12169349
"Ping"
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12169381
About your question, I'm not sure. I guess the easiest way to find out is to test it. I just needed like the cases above.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question