?
Solved

Change file attributes?

Posted on 2004-09-22
6
Medium Priority
?
3,252 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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