Solved

Change file attributes?

Posted on 2004-09-22
6
3,229 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
Comment Utility
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
Comment Utility
You can try to do this:

SetAttr "TESTFILE", vbNormal

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

Expert Comment

by:Dhaest
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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
Comment Utility
"Ping"
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now