Solved

Change file attributes?

Posted on 2004-09-22
6
3,240 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
  • 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
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!

 
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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

756 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